版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Android自定义控件-下载进度条
效果展示
源码
- 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_progress"
android:textColor="#f00"
android:textSize="15sp"
android:text="下载进度:"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<...DownLoadProgressbar
android:layout_centerHorizontal="true"
android:layout_below="@id/tv_progress"
android:id="@+id/dlp_current"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="100dp"
android:layout_below="@id/tv_progress"
android:onClick="start"
android:text="开始"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
- 控件
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import com.blankj.utilcode.util.SizeUtils;
/**
* 进度条
*/
public class DownLoadProgressbar extends View {
private Paint paint = new Paint(); // 绘制背景灰色线条画笔
private Paint paintText = new Paint(); // 绘制下载进度画笔
private float offset = 0f; // 下载偏移量
private float maxvalue = 0f; // 进度的总大小
private float currentValue = 0f; // 当前进度
private Rect mBound = new Rect(); // 获取百分比数字的长宽
private String percentValue = "0%"; // 要显示的现在百分比
private float offsetRight = 0f; // 灰色线条距离右边的距离
private int textSize = SizeUtils.sp2px(25); // 百分比的文字大小
private float offsetTop = SizeUtils.dp2px(18); // 距离顶部的偏移量
public DownLoadProgressbar(Context context) {
this(context, null);
}
public DownLoadProgressbar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DownLoadProgressbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getTextWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制底色
paint.setColor(Color.parseColor("#cccccc"));
paint.setStrokeWidth(SizeUtils.dp2px(1));
canvas.drawLine(0, offsetTop, getWidth(), offsetTop, paint);
// 绘制进度条颜色
paint.setColor(Color.parseColor("#ff0000"));
paint.setStrokeWidth(SizeUtils.dp2px(2));
canvas.drawLine(0, offsetTop, offset, offsetTop, paint);
paint.setColor(Color.parseColor("#ffffff"));
paint.setStrokeWidth(SizeUtils.dp2px(1));
paintText.setColor(Color.parseColor("#ff0000"));
paintText.setTextSize(textSize);
paintText.setAntiAlias(true);
paintText.getTextBounds(percentValue, 0, percentValue.length(), mBound);
canvas.drawLine(offset, offsetTop, offset + mBound.width() + SizeUtils.dp2px(4), offsetTop, paint);
canvas.drawText(percentValue, offset, offsetTop + mBound.height() / 2 - SizeUtils.dp2px(2), paintText);
}
public void setCurrentValue(float currentValue) {
this.currentValue = currentValue;
int value = (int) (currentValue * 100 / maxvalue);
if (value < 100 && value > 0) {
percentValue = value + "%";
} else if (value <= 0) {
percentValue = "0%";
} else {
percentValue = "100%";
}
calc();
invalidate();
}
private void calc() {
if (currentValue < maxvalue) {
offset = (getWidth() - offsetRight) * currentValue / maxvalue;
} else {
offset = getWidth() - offsetRight;
}
}
/**
* 设置最大值
*
* @param maxValue
*/
public void setMaxvalue(int maxValue) {
this.maxvalue = maxValue;
}
/**
* 获取“100%”的宽度
*/
public void getTextWidth() {
Paint paint = new Paint();
Rect rect = new Rect();
paint.setTextSize(textSize);
paint.setAntiAlias(true);
paint.getTextBounds("100%", 0, "100%".length(), rect);
offsetRight = rect.width() + SizeUtils.dp2px(5);;
}
}
- Activity
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import ...R;
public class DownLoadProgressbarActivity extends AppCompatActivity {
private Handler mHandler = new Handler(Looper.getMainLooper());
private int current = 0;
private int max = 100;
private DownLoadProgressbar dlp_current;
private TextView tv_progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down_load_progressbar);
dlp_current = findViewById(R.id.dlp_current);
tv_progress = findViewById(R.id.tv_progress);
dlp_current.setMaxvalue(max);
}
public static void start(Context context) {
context.startActivity(new Intent(context, DownLoadProgressbarActivity.class));
}
public void start(final View view) {
if (current <= max) {
dlp_current.setCurrentValue(current);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
current++;
tv_progress.setText("下载进度:" + current + "/" + max);
start(view);
}
}, 100);
} else {
current = 100;
tv_progress.setText("下载进度:" + current + "/" + max);
mHandler.removeCallbacksAndMessages(null);
current = 0;
}
}
}