怎么在android中通过自定义view实现一个数字进度条?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
package com.tuya;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class DownLoadProgressView extends View {
private Paint paint;//绘制进度条画笔
private Paint textPaint;//绘制文字画笔
private Paint dottePaint;//绘制灰色线画笔
private int width;
private int height;
private int padding =5;
private int value = 0;
public DownLoadProgressView(Context context) {
this(context,null);
}
public DownLoadProgressView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public DownLoadProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
/**
* 初始化画笔
*/
private void initPaint() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setStrokeWidth(3);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.BLUE);
textPaint.setTextSize(12);
dottePaint = new Paint();
dottePaint.setAntiAlias(true);
dottePaint.setStrokeWidth(2);
dottePaint.setStyle(Paint.Style.FILL);
dottePaint.setColor(Color.parseColor("#e5e5e5"));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
String str = value+"%";
float strWidth = textPaint.measureText(value+"%")+padding;//绘制文字的宽度 +padding是为了防止在进度条加载完毕的时候文字绘制出现被切掉情况
Rect rect = new Rect();
textPaint.getTextBounds(str,0,str.length(),rect);
canvas.drawLine(0,height/2,value*((width-strWidth)/100),height/2,paint);//绘制进度
canvas.drawText(value+"%",value*((width-strWidth)/100)+padding,(height-rect.height())/2+2*padding,textPaint);//绘制进度文字 这个高度+2*padding是因为drawText是根据基线计算的,要准确的话要去求基线
canvas.drawLine(value*((width-strWidth)/100)+strWidth+padding,height/2,width,height/2,dottePaint);//绘制灰色进度表示剩余多少
postDelayed(new Runnable() {
@Override
public void run() {
if(value<100){
value++;
postInvalidate();
}
}
},100);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7EC0EE">
<com.tuya.DownLoadProgressView
android:id="@+id/dpv"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="60dp"
></com.tuya.DownLoadProgressView>
</RelativeLayout>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。