目录
Android ProgressBar 进度条
ProgressBar
ProgressBar 外观
ProgressBar 属性
ProgressBar 方法
Android ProgressBar 动图替代圆形进度条
范例
Android ProgressBar 自定义圆环进度条
例子:
参考文档
在Android中,ProgressBar(进度条)是用于显示任务进度的UI组件,通常用于耗时操作的过程中以及需要显示加载等待的情况下。ProgressBar可以显示水平或者圆形的样式,具体取决于所选择的样式。
在Android中,ProgressBar类继承自View类,因此它是一个视图(View),用于在用户界面中显示任务的进度。由于ProgressBar继承自View类,因此它具有所有View类的属性和方法,同时也拥有专门用于控制进度条外观和行为的属性和方法。
在Android中,ProgressBar具有四种外观模式,这四种模式的组合由android:indeterminate和style属性确定。这里列出了这些属性及其取值的说明:
android:indeterminate属性
style属性
1、说明: 这个属性用于确定ProgressBar的外观样式。
2、可选值:
四种组合
1、水平条形模式:确定性进度
2、水平条形模式:不确定性进度
3、圆形模式:确定性进度
4、圆形模式:不确定性进度
实例
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
}
}
android:max:
android:progress:
android:progressDrawable:
android:indeterminate:
android:indeterminateDrawable:
android:indeterminateDuration:
android:secondaryProgress:
getMax():
getProgress():
getSecondaryProgress():
incrementProgressBy(int diff):
isIndeterminate():
setIndeterminate(boolean indeterminate):
在Android中,可以通过使用帧动画来实现一个类似圆形进度条的效果。可以准备一组连续的图片,并将它们放置在一个ImageView中,然后使用帧动画来展示这些图片,从而创建一个类似于圆形进度条的效果。
1、在 res/drawable 目录下新建一个 anim_pgbar.xml 的资源文件
2、修改 activity_main.xml 添加一个 ImageView
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中,可以通过自定义绘制来实现一个圆环进度条。这可以通过自定义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
官方 API 文档: ProgressBar