android进度条竖向,Android-竖型进度条动画布局xml:
android:id="@+id/dot_progress"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"/>
⾃定义布局View代码:
package com.xinyan.personal.ui.view;
t.Context;
t.res.Resources;
t.res.TypedArray;
aphics.Canvas;
canvas动画aphics.Color;
aphics.Paint;
aphics.Rect;
aphics.RectF;
TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import com.xinyan.personal.R;
/**
* Created by zst on 17/7/18.
*/
public class VerticalProgressBar extends View {
private Paint paint;// 画笔
private int progress;// 进度值
private int width;// 宽度值
private int height;// ⾼度值
public VerticalProgressBar(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public VerticalProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VerticalProgressBar(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Measure(widthMeasureSpec, heightMeasureSpec);
//画布的⾼度和宽度
width = getMeasuredWidth() - 1;// 得到布局设置的宽度值
height = getMeasuredHeight() - 1;// 得到布局设置的⾼度值
}
@Override
protected void onDraw(Canvas canvas) {
//画背景
paint.setColor(getResources().lor.line_e6e6e6));// 设置背景画笔颜⾊canvas.drawRect(0, 0, width, height, paint);// 画背景矩形
//画进度
paint.setColor(getResources().lor.line_feae1b));// 设置进度画笔颜⾊canvas.drawRect(0, height - progress / 100f * height, width, height, paint);// 画矩形//画四边
//canvas.drawLine(0, 0, width, 0, paint);// 画顶边
//canvas.drawLine(0, 0, 0, height, paint);// 画左边
//canvas.drawLine(width, 0, width, height, paint);// 画右边
//canvas.drawLine(0, height, width, height, paint);// 画底边
//设置中间字体
//paint.setColor(Color.RED);// 设置画笔颜⾊为红⾊
//paint.setTextSize(width / 3);// 设置⽂字⼤⼩
//canvas.drawText(progress + "%",(width - getTextWidth(progress + "%")) / 2, height / 2, paint);// 画⽂字Draw(canvas);
}
/**
* 拿到⽂字宽度
*
* @param str 传进来的字符串
* return 宽度
*/
private int getTextWidth(String str) {
// 计算⽂字所在矩形,可以得到宽⾼
Rect rect = new Rect();
return rect.width();
}
/**
* 设置progressbar进度
*/
public void setProgress(int progress) {
this.progress = progress;
postInvalidate();
}
}
调⽤代码:
VerticalProgressBar vpProgressBar = (VerticalProgressBar) findViewById(R.id.vp_progress);
new Thread(){
public void run() {
try {
for (int i= 0;i<=100;i++) {
Thread.sleep(50);//休息50毫秒
vpProgressBar.setProgress(i);//更新进度条进度
}
} catch (InterruptedException e) {
e.printStackTrace(); }
};
}.start();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论