学习Android的第十一天

这篇具有很好参考价值的文章主要介绍了学习Android的第十一天。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

Android ProgressBar 进度条

ProgressBar

ProgressBar 外观

ProgressBar 属性

ProgressBar 方法

Android ProgressBar 动图替代圆形进度条

范例

Android ProgressBar 自定义圆环进度条

例子:

参考文档


Android ProgressBar 进度条

在Android中,ProgressBar(进度条)是用于显示任务进度的UI组件,通常用于耗时操作的过程中以及需要显示加载等待的情况下。ProgressBar可以显示水平或者圆形的样式,具体取决于所选择的样式。

ProgressBar

在Android中,ProgressBar类继承自View类,因此它是一个视图(View),用于在用户界面中显示任务的进度。由于ProgressBar继承自View类,因此它具有所有View类的属性和方法,同时也拥有专门用于控制进度条外观和行为的属性和方法。

ProgressBar 外观

在Android中,ProgressBar具有四种外观模式,这四种模式的组合由android:indeterminate和style属性确定。这里列出了这些属性及其取值的说明:

android:indeterminate属性

  • 说明: 这个属性用于确定进度条是否显示在不确定模式下。
  • 默认值: true,表示显示不确定模式。

style属性

1、说明: 这个属性用于确定ProgressBar的外观样式。

2、可选值:

  • Widget.ProgressBar.Horizontal:水平条形模式。
  • Widget.ProgressBar.Small:小型条形模式。
  • Widget.ProgressBar.Large:大型条形模式。
  • Widget.ProgressBar.Inverse:反向条形模式。
  • Widget.ProgressBar.Small.Inverse:小型反向条形模式。
  • Widget.ProgressBar.Large.Inverse:大型反向条形模式。

四种组合

1、水平条形模式:确定性进度

  • android:indeterminate="false"
  • style="@android:style/Widget.ProgressBar.Horizontal"

2、水平条形模式:不确定性进度

  • android:indeterminate="true"
  • style="@android:style/Widget.ProgressBar.Horizontal"

3、圆形模式:确定性进度

  • android:indeterminate="false"
  • style="@android:style/Widget.ProgressBar.Large"

4、圆形模式:不确定性进度

  • android:indeterminate="true"
  • style="@android:style/Widget.ProgressBar.Large"

实例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="?android:attr/progressBarStyleHorizontal"
            android:max="100"
            android:progress="50" />

        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_marginTop="16dp"
            android:layout_width="match_parent"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_height="wrap_content"
            android:indeterminate = "false"
            android:progress="25"
            android:max="100" />

        <ProgressBar
            android:layout_width="match_parent"
            style="@android:style/Widget.ProgressBar.Inverse"
            android:layout_height="wrap_content" />
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ProgressBar progressBar = findViewById(R.id.progressBar);
        progressBar.setProgress(75); // 更新进度为75
        ProgressBar progressBar1 = findViewById(R.id.progressBar1);
        progressBar1.setProgress(75); // 更新进度为75
    }
}

ProgressBar 属性

android:max:

  • 说明: 进度条的最大值。通常情况下,进度条的进度范围从0到这个值。
  • 默认值: 100

android:progress:

  • 说明: 进度条已完成的进度值。
  • 默认值: 0
  • 注意: 进度值应在0到android:max之间。

android:progressDrawable:

  • 说明: 设置轨道对应的Drawable对象。通过这个属性,可以自定义进度条的外观。
  • 默认值: 系统提供的进度条样式。

android:indeterminate:

  • 说明: 如果设置成true,则进度条不精确显示进度。当进度无法确定时(例如,执行的任务没有确定的结束时间),通常会使用不确定性进度条。
  • 默认值: false

android:indeterminateDrawable:

  • 说明: 设置不显示进度的进度条的Drawable对象。这个属性可以用于定义不确定性进度条的外观。
  • 默认值: 系统提供的不确定性进度条样式。

android:indeterminateDuration:

  • 说明: 设置不精确显示进度的持续时间。这个属性用于控制不确定性进度条的动画持续时间。
  • 默认值: 4000毫秒(4秒)

android:secondaryProgress:

  • 说明: 二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过android:progress属性进行设置。
  • 默认值: 0
  • 注意: 这个属性的值也应在0到android:max之间。

ProgressBar 方法

getMax():

  • 说明: 返回这个进度条的范围的上限。
  • 返回类型: int

getProgress():

  • 说明: 返回进度条的当前进度。
  • 返回类型: int

getSecondaryProgress():

  • 说明: 返回进度条的次要进度,通常用于显示缓冲进度等。
  • 返回类型: int

incrementProgressBy(int diff):

  • 说明: 增加当前进度。可以通过传递正数或负数来增加或减少进度。
  • 参数: diff - 增加的进度值。

isIndeterminate():

  • 说明: 指示进度条是否在不确定模式下。
  • 返回类型: boolean

setIndeterminate(boolean indeterminate):

  • 说明: 设置进度条是否在不确定模式下。
  • 参数: indeterminate - true表示进度条处于不确定模式,false表示进度条处于确定模式。

Android ProgressBar 动图替代圆形进度条

在Android中,可以通过使用帧动画来实现一个类似圆形进度条的效果。可以准备一组连续的图片,并将它们放置在一个ImageView中,然后使用帧动画来展示这些图片,从而创建一个类似于圆形进度条的效果。

范例

1、在 res/drawable 目录下新建一个 anim_pgbar.xml 的资源文件

<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot="false" >

    <item  
        android:drawable="@drawable/loading_01"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_02"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_03"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_04"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_05"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_06"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_07"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_08"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_09"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_10"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_11"  
        android:duration="200"/>  
    <item  
        android:drawable="@drawable/loading_12"  
        android:duration="200"/>

</animation-list>

2、修改 activity_main.xml 添加一个 ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

        <Button android:id="@+id/btn_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示动画" />

        <Button android:id="@+id/btn_hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="隐藏动画" />

        <ImageView
            android:id="@+id/progressImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/anim_pgbar"
            android:visibility="invisible" />

</LinearLayout>

3、修改 MainActivity.java

package com.example.myapplication;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ImageView progressImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressImageView = findViewById(R.id.progressImageView);

        Button btnShow = findViewById(R.id.btn_show);
        Button btnHide = findViewById(R.id.btn_hide);

        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressAnimation();
            }
        });

        btnHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideProgressAnimation();
            }
        });
    }

    private void showProgressAnimation() {
        progressImageView.setVisibility(View.VISIBLE);
        AnimationDrawable progressAnimation = (AnimationDrawable) progressImageView.getDrawable();
        progressAnimation.start();
    }

    private void hideProgressAnimation() {
        progressImageView.setVisibility(View.INVISIBLE);
        AnimationDrawable progressAnimation = (AnimationDrawable) progressImageView.getDrawable();
        progressAnimation.stop();
    }
}

Android ProgressBar 自定义圆环进度条

在Android中,可以通过自定义绘制来实现一个圆环进度条。这可以通过自定义View来实现。

例子:

1、自定义一个 View 类 RingProgressBar.java

package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class RingProgressBar extends View {

    private Paint mBackPaint;
    private Paint mFrontPaint;
    private Paint mTextPaint;
    private float mStrokeWidth = 50;
    private float mRadius = 200;
    private RectF mRect;
    private int mProgress = 0;
    private int mTargetProgress = 90;
    private int mMax = 100;
    private int mWidth;
    private int mHeight;

    public RingProgressBar(Context context) {
        super(context);
        init();
    }

    public RingProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RingProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBackPaint = new Paint();
        mBackPaint.setColor(Color.WHITE);
        mBackPaint.setAntiAlias(true);
        mBackPaint.setStyle(Paint.Style.STROKE);
        mBackPaint.setStrokeWidth(mStrokeWidth);

        mFrontPaint = new Paint();
        mFrontPaint.setColor(Color.GREEN);
        mFrontPaint.setAntiAlias(true);
        mFrontPaint.setStyle(Paint.Style.STROKE);
        mFrontPaint.setStrokeWidth(mStrokeWidth);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.GREEN);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(80);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        mWidth = resolveSizeAndState(widthSize, widthMeasureSpec, 0);
        mHeight = resolveSizeAndState(heightSize, heightMeasureSpec, 0);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float angle = mProgress / (float) mMax * 360;
        canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mBackPaint);
        canvas.drawArc(getRect(), -90, angle, false, mFrontPaint);
        canvas.drawText(mProgress + "%", mWidth / 2, mHeight / 2 + mStrokeWidth / 4, mTextPaint);
        if (mProgress < mTargetProgress) {
            mProgress += 1;
            invalidate();
        }
    }

    private RectF getRect() {
        if (mRect == null) {
            mRect = new RectF();
            int viewSize = (int) (mRadius * 2);
            int left = (mWidth - viewSize) / 2;
            int top = (mHeight - viewSize) / 2;
            int right = left + viewSize;
            int bottom = top + viewSize;
            mRect.set(left, top, right, bottom);
        }
        return mRect;
    }
}

2、然后修改 activity_main.xml 添加我们的 RingProgressBar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center">
        <com.example.myapplication.RingProgressBar
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</LinearLayout>

参考文档

官方 API 文档: ProgressBar文章来源地址https://www.toymoban.com/news/detail-825003.html

到了这里,关于学习Android的第十一天的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【80天学习完《深入理解计算机系统》】第十一天 3.5 过程(函数调用)

    专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客,如有问题交流,欢迎评论区留言,一定尽快回复!(大家可以去看我的专栏,是所有文章的目录) 文章字体风格: 红色文字表示:重难点★✔ 蓝色文字表示:思路以及想法★✔ 如果大家觉得有帮助的话,感谢大家帮

    2024年02月11日
    浏览(31)
  • 【80天学习完《深入理解计算机系统》】第十一天 3.4 跳转指令

    专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客,如有问题交流,欢迎评论区留言,一定尽快回复!(大家可以去看我的专栏,是所有文章的目录) 文章字体风格: 红色文字表示:重难点★✔ 蓝色文字表示:思路以及想法★✔ 如果大家觉得有帮助的话,感谢大家帮

    2024年02月11日
    浏览(30)
  • 第七十一天学习记录:对陈正冲编著《C 语言深度解剖》中关于1、2、4,5章作者留下部分问题的学习

    问:有如下代码。 i 和 j 的值分别是什么,为什么? 答: 在这份代码中,变量 i 和 j 分别是函数 fun1 和 fun2 的静态局部变量。静态局部变量在函数内部声明,但在整个程序生命周期内都存在,且只初始化一次(第一次进入函数时初始化)。 在 main 函数中,fun1 和 fun2 分别被调

    2024年02月08日
    浏览(30)
  • (第十一天)初识SpringMVC SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录

    今天我们要来学习一下SSM框架的最后一个框架SpringMVC 一、初认SpringMVC 基本概念: ​ Spring MVC(Model-View-Controller)是一个用于构建Java Web应用程序的开源框架,它提供了一种基于MVC架构的方式来开发Web应用 。 ​ SpringMVC是Spring Framework的一部分,它是一种基于模型-视图-控制器(

    2024年02月07日
    浏览(53)
  • 第五十一天打卡

    中等 1.5K company 微软 Microsoft 给定一个整数数组prices,其中第 prices[i] 表示第 i 天的股票价格 。​ 设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。 注意

    2023年04月09日
    浏览(25)
  • C语言(第三十一天)

    6. 调试举例1 求1!+2!+3!+4!+...10!的和,请看下面的代码: 调试找一下问题。 7. 调试举例2 在VS2019、X86、Debug 的环境下,编译器不做任何优化的话,下面代码执行的结果是啥? 程序运行,死循环了,调试看看为什么? 调试可以上面程序的内存布局如下:  1. 栈区内存的使用习惯

    2024年02月11日
    浏览(30)
  • node 第二十一天 webpack 初见

    为什么需要学习(了解)webpack webpack是前端工程化的基石,webpack又是基于node进行文件打包bundle,所以作为前端起手学习node服务端开发,同时学习webpack是很有必要的。 随着vite的出现,vue这一脉可能也许不再需要学习webpack了,但是需要知道的是, 打包一定是前端工程化绕不

    2024年01月16日
    浏览(32)
  • 代码随想录第二十一天

    题目链接 : 二叉搜索树的最小绝对差 自己的思路 :和验证二叉搜索树一样的思路!可以求每个相邻节点的差值的绝对值,然后和之前的差值的绝对值进行比较,取最小的为新的res;递归三部曲:1、传入参数:当前节点;2、终止条件:如果当前节点为空,直接返回;3、单层递

    2024年02月16日
    浏览(40)
  • 谷粒商城第十一天-完善商品分组(主要添上关联属性)

    目录 一、总述 二、前端部分 2.1 改良前端获取分组列表接口及其调用 2.2 添加关联的一整套逻辑 三、后端部分 四、总结 前端部分和之前的商品品牌添加分类差不多。 也是修改一下前端的分页获取列表的接口,还有就是加上关联的那一套逻辑,包括基本构件的引入、数据域的

    2024年02月13日
    浏览(30)
  • 从零开始的力扣刷题记录-第五十一天

    题目描述: 给你一棵二叉搜索树的 root ,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。 题解: 中序遍历存储节点后按顺序连接即可 代码(Go): 题目描述: 小扣在秋日市集发

    2024年02月08日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包