安卓自定义一个水平进度条,根据后台返回的数据显示进度,详细源码
时间: 2024-03-23 12:37:47 浏览: 21
好的,下面是一个简单的自定义水平进度条的代码实现,根据后台返回的数据显示进度:
1. 首先,在res/layout文件夹下新建一个xml文件,例如progress_bar_horizontal.xml,添加如下代码:
```xml
<com.example.myapplication.HorizontalProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:max="100"
app:progress="0"
app:progress_color="@color/colorAccent"
app:secondary_progress_color="@color/colorPrimary"
app:background_color="@color/colorGray"
app:text_color="@color/colorBlack"
app:text_size="14sp"
app:text_visible="true" />
```
其中,自定义的HorizontalProgressBar组件继承自ProgressBar,所以在xml中使用时需要写全类名。
2. 在res/values文件夹下新建一个attrs.xml文件,添加自定义属性:
```xml
<resources>
<declare-styleable name="HorizontalProgressBar">
<attr name="progress_color" format="color" />
<attr name="secondary_progress_color" format="color" />
<attr name="background_color" format="color" />
<attr name="text_color" format="color" />
<attr name="text_size" format="dimension" />
<attr name="text_visible" format="boolean" />
<attr name="max" format="integer" />
<attr name="progress" format="integer" />
</declare-styleable>
</resources>
```
3. 新建一个HorizontalProgressBar.java文件,添加如下代码:
```java
public class HorizontalProgressBar extends ProgressBar {
private int mTextColor;
private float mTextSize;
private boolean mTextVisible;
private int mProgressColor;
private int mSecondaryProgressColor;
private int mBackgroundColor;
private Paint mPaint;
private Rect mRect;
public HorizontalProgressBar(Context context) {
super(context);
init(null);
}
public HorizontalProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制背景
mPaint.setColor(mBackgroundColor);
canvas.drawRect(mRect, mPaint);
// 绘制进度
float progressRatio = (float)getProgress() / (float)getMax();
int progressWidth = (int)(progressRatio * getWidth());
mPaint.setColor(mProgressColor);
canvas.drawRect(0, 0, progressWidth, getHeight(), mPaint);
// 绘制次进度
float secondaryProgressRatio = (float)getSecondaryProgress() / (float)getMax();
int secondaryProgressWidth = (int)(secondaryProgressRatio * getWidth());
mPaint.setColor(mSecondaryProgressColor);
canvas.drawRect(0, 0, secondaryProgressWidth, getHeight(), mPaint);
// 绘制文字
if (mTextVisible) {
mPaint.setColor(mTextColor);
mPaint.setTextSize(mTextSize);
String progressText = getProgress() + "%";
float textWidth = mPaint.measureText(progressText);
float textX = (getWidth() - textWidth) / 2;
float textY = (getHeight() + mTextSize) / 2 - mPaint.descent();
canvas.drawText(progressText, textX, textY, mPaint);
}
}
private void init(AttributeSet attrs) {
// 初始化属性
if (attrs != null) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar);
mTextColor = ta.getColor(R.styleable.HorizontalProgressBar_text_color, Color.BLACK);
mTextSize = ta.getDimension(R.styleable.HorizontalProgressBar_text_size, 14);
mTextVisible = ta.getBoolean(R.styleable.HorizontalProgressBar_text_visible, true);
mProgressColor = ta.getColor(R.styleable.HorizontalProgressBar_progress_color, Color.BLUE);
mSecondaryProgressColor = ta.getColor(R.styleable.HorizontalProgressBar_secondary_progress_color, Color.GRAY);
mBackgroundColor = ta.getColor(R.styleable.HorizontalProgressBar_background_color, Color.TRANSPARENT);
ta.recycle();
} else {
mTextColor = Color.BLACK;
mTextSize = 14;
mTextVisible = true;
mProgressColor = Color.BLUE;
mSecondaryProgressColor = Color.GRAY;
mBackgroundColor = Color.TRANSPARENT;
}
// 初始化画笔
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRect = new Rect(0, 0, getWidth(), getHeight());
}
}
```
4. 在Activity中获取ProgressBar组件,并根据后台返回的数据设置进度,例如:
```java
HorizontalProgressBar progressBar = findViewById(R.id.progress_bar);
int progress = (int)(data/current * 100); // data为当前值,current为总值
progressBar.setProgress(progress);
```
这样就完成了自定义水平进度条的实现。