Android自定义进度条实现步骤
更新:2023-05-14 00:47
一、了解自定义进度条
Android提供了ProgressBar控件,可以用于显示进度条,在进行长时间操作或加载资源时,可以通过进度条让用户感知到操作的进展。但是ProgressBar本身的样式有限,不能满足特殊的设计需求,此时可以考虑自定义进度条。 自定义进度条可以灵活控制进度的样式、颜色、大小等,同时也可以进行动态的渲染和交互。自定义进度条一般使用Canvas和Paint来绘制进度效果,通过实现自定义View或继承ProgressBar来实现。
二、自定义View实现进度条
1、新建一个自定义View,继承View类,并实现onDraw()方法。onDraw()方法是用来绘制自定义View的主要方法。这里我们使用Canvas和Paint来进行绘制,绘制的内容是一个矩形,矩形的长度和宽度都是根据当前进度值来计算得出。
public class CustomProgressBar extends View {
private Paint mPaint;
private RectF mRectF;
private int mProgressBarColor;
private int mProgressBarHeight;
private int mMaxValue;
private int mCurrentValue;
public CustomProgressBar(Context context) {
super(context);
initView();
}
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
parseAttrs(attrs);
}
public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
parseAttrs(attrs);
}
private void initView() {
mProgressBarColor = Color.BLUE;
mProgressBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
mMaxValue = 100;
mCurrentValue = 0;
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mRectF = new RectF();
}
private void parseAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
mProgressBarColor = typedArray.getColor(R.styleable.CustomProgressBar_progressBarColor, Color.BLUE);
mProgressBarHeight = typedArray.getDimensionPixelSize(R.styleable.CustomProgressBar_progressBarHeight, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()));
mMaxValue = typedArray.getInt(R.styleable.CustomProgressBar_maxValue, 100);
mCurrentValue = typedArray.getInt(R.styleable.CustomProgressBar_currentValue, 0);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(mProgressBarColor);
mRectF.set(0, 0, getWidth() * mCurrentValue / mMaxValue, mProgressBarHeight);
canvas.drawRect(mRectF, mPaint);
}
public void setMaxValue(int maxValue) {
this.mMaxValue = maxValue;
}
public void setCurrentValue(int currentValue) {
this.mCurrentValue = currentValue;
invalidate();
}
}
2、在attrs.xml文件中定义自定义属性,这些属性将用于在XML布局文件中设置进度条的样式和属性。
<resources>
<declare-styleable name="CustomProgressBar">
<attr name="progressBarColor" format="color" />
<attr name="progressBarHeight" format="dimension" />
<attr name="maxValue" format="integer" />
<attr name="currentValue" format="integer" />
</declare-styleable>
</resources>
3、在XML布局文件中使用自定义进度条。
<com.example.customprogressbar.customprogressbar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:progressBarColor="@color/progress_bar_color"
app:progressBarHeight="5dp"
app:maxValue="100"
app:currentValue="0" />
4、在Activity中设置自定义进度条的最大值和当前值,调用setCurrentValue()方法更新进度条的进度。
CustomProgressBar progressBar = findViewById(R.id.progress_bar);
progressBar.setMaxValue(100);
progressBar.setCurrentValue(50);
三、继承ProgressBar实现进度条
1、新建一个自定义ProgressBar,继承ProgressBar类,重写onDraw()方法。onDraw()方法是用来绘制进度条的主要方法。这里我们使用Canvas和Paint来进行绘制,绘制的内容是一个矩形。
public class CustomProgressBar extends ProgressBar {
private Paint mPaint;
private RectF mRectF;
private int mProgressBarColor;
private int mProgressBarHeight;
public CustomProgressBar(Context context) {
super(context);
initView();
}
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
parseAttrs(attrs);
}
public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
parseAttrs(attrs);
}
private void initView() {
mProgressBarColor = Color.BLUE;
mProgressBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mRectF = new RectF();
}
private void parseAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
mProgressBarColor = typedArray.getColor(R.styleable.CustomProgressBar_progressBarColor, Color.BLUE);
mProgressBarHeight = typedArray.getDimensionPixelSize(R.styleable.CustomProgressBar_progressBarHeight, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()));
typedArray.recycle();
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(mProgressBarColor);
mRectF.set(getPaddingLeft(), getHeight() - mProgressBarHeight - getPaddingBottom(),
getWidth() * getProgress() / getMax() - getPaddingRight(),
getHeight() - getPaddingBottom());
canvas.drawRect(mRectF, mPaint);
}
}
2、在attrs.xml文件中定义自定义属性,这些属性将用于在XML布局文件中设置进度条的样式和属性。
<resources>
<declare-styleable name="CustomProgressBar">
<attr name="progressBarColor" format="color" />
<attr name="progressBarHeight" format="dimension" />
</declare-styleable>
</resources>
3、在XML布局文件中使用自定义进度条。
<com.example.customprogressbar.customprogressbar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
app:progressBarColor="@color/progress_bar_color"
app:progressBarHeight="5dp" />
4、在Activity中设置自定义进度条的最大值和当前值,调用setProgress()方法更新进度条的进度。
CustomProgressBar progressBar = findViewById(R.id.progress_bar);
progressBar.setMax(100);
progressBar.setProgress(50);
综上所述,通过自定义View或继承ProgressBar可以实现自定义进度条,可以根据需求来选择实现方式,并进行样式和属性的自定义。