博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓自定义控件--垂直进度条
阅读量:7247 次
发布时间:2019-06-29

本文共 3067 字,大约阅读时间需要 10 分钟。

安卓只给我们提供了水平的进度条和环形进度条,没有垂直的进度条,下面我们来开发个垂直的进度条。

效果图如下:

 

一、工作原理

其实就是画一个矩形,改变矩形的高度就能模拟进度的变化。当进度变化时,改变矩形的高度,然后重绘即可。

二、代码如下

1.VerticalProgressBar.java:

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) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        width = getMeasuredWidth() - 1;// 宽度值        height = getMeasuredHeight() - 1;// 高度值    }    @Override    protected void onDraw(Canvas canvas) {        paint.setColor(Color.rgb(55, 200, 255));// 设置画笔颜色        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);// 画文字        super.onDraw(canvas);    }    /**     * 拿到文字宽度     * @param str 传进来的字符串     * return 宽度     */    private int getTextWidth(String str) {        // 计算文字所在矩形,可以得到宽高        Rect rect = new Rect();        paint.getTextBounds(str, 0, str.length(), rect);        return rect.width();    }    /** 设置progressbar进度 */    public void setProgress(int progress) {        this.progress = progress;        postInvalidate();    }}

2.MainActivity.java:

public class MainActivity extends ActionBarActivity {    private VerticalProgressBar vpProgressBar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    /**初始化控件 */    private void initView() {        vpProgressBar = (VerticalProgressBar) findViewById(R.id.vp_progress);        run();    }    /**测试progressbar*/    private void run() {        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();    }}

3.布局文件activity_main.xml:

 源码地址:

转载于:https://www.cnblogs.com/johnsonwei/p/5845970.html

你可能感兴趣的文章
我的友情链接
查看>>
配置WebLogic数据源
查看>>
something about kali
查看>>
rsync数据同步工具(三)
查看>>
定义图形模板
查看>>
php生成随机数:自定义函数 randstr($length)
查看>>
python学习--random和列表
查看>>
IPV4网段划分
查看>>
解决Oracle 解决Oracle ORA-12505, TNS:listener does not currently know of SID
查看>>
详解Linux下安装配置Nginx
查看>>
Maven使用笔记一
查看>>
STL deque
查看>>
Words For Today [2011-07-08]
查看>>
图像像素的获取和操作(第三天)
查看>>
(转)Array.prototype.slice.call自解
查看>>
测试1
查看>>
使用CSS3的appearance属性改变元素的外观
查看>>
inotify + rsync 实现 linux 文件实时同步
查看>>
RecyclerView的使用,打造一个通用的Adapter
查看>>
MySQL • 源码分析 • mysql认证阶段漫游
查看>>