美文网首页
【Android 开发】自定义View(下)——仪表盘

【Android 开发】自定义View(下)——仪表盘

作者: 榆野铃爱 | 来源:发表于2019-11-03 21:12 被阅读0次

    内容简概

    一、仪表盘

    具体内容

    一、仪表盘

    在一个View中可以使用多个画笔绘制,从而形成一个组件。这里的仪表盘可以拆分为三个画笔画的东西,一是圆弧灰色进度槽,二是粉紫色进度条,三是变化的文字。

    (一)MeterView
    public class MeterView extends View {
        private Paint bgPaint; //背景画笔
        private Paint progressPaint; //进度画笔
        private Paint textPaint; //文字画笔
        private float progress; //进度
    
        public MeterView(Context context) {
            super(context);
        }
    
        public MeterView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        private void init(){
            //进度槽(背景)画笔
            bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            bgPaint.setColor(Color.BLACK);
            bgPaint.setStyle(Paint.Style.STROKE);
            bgPaint.setStrokeWidth(40);
            bgPaint.setStrokeCap(Paint.Cap.ROUND);//设置两端的类型
    
            // 进度条画笔
            progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            progressPaint.setColor(Color.MAGENTA);
            progressPaint.setStyle(Paint.Style.STROKE);
            progressPaint.setStrokeWidth(40);
            progressPaint.setStrokeCap(Paint.Cap.ROUND);//设置两端的类型
            // 文本画笔
            textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            textPaint.setColor(Color.BLACK);
            textPaint.setTextSize(100);
        }
        /**
         * 固定不变的 就不要放在onDraw方法里面
         * 这个方法可能会被调用多次
         * @param canvas
         */
        @Override
        protected void onDraw(Canvas canvas) {
            //画初始圆
            //确定矩形区域
            RectF frame = new RectF(50,100, getWidth()-50,getWidth()-50);
    
            //画一个弧
            canvas.drawArc(frame,120,300, false,bgPaint);
    
            //计算进度对应的角度
            int angle = (int) (progress*300);
            canvas.drawArc(frame,120,angle,false,progressPaint);
    
            // 文本内容
            String text = (int)(progress*100)+"%";
    
            //计算文字的宽度
            int width = (int) textPaint.measureText(text);
    
            //计算文字的矩阵 FontMetrics
            Paint.FontMetricsInt fo = textPaint.getFontMetricsInt();
    
            //文字的高度
            int height = fo.bottom - fo.top;
    
            //计算向下移动的距离  Ascent/2 注意Ascent为负数
            int space = -fo.ascent /2 ;
    
            //画文字
            canvas.drawText(text,getWidth()/2-width/2,
                    getWidth()/2+space,textPaint);
        }
    
        public float getProgress() {
            return progress;
        }
    
        // 进度值不断改变,需要用setget方法改变和获取新的进度值
        public void setProgress(float progress) {
            this.progress = progress;
    
            //刷新 重绘
            if (progress < 1.0001) {
                invalidate();
            }
        }
    }
    
    (二)xml
        <com.example.a13_drawview.MeterView
            android:id="@+id/meter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
    (三)MainActivity
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        // 每次点击进度都加5%
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            MeterView mv = findViewById(R.id.meter);
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                mv.setProgress((float) (mv.getProgress()+ 0.05));
            }
            return true;
        }
    }
    
    (四)运行效果

    用手点击屏幕,进度条加5%,否则进度条无变化


    相关文章

      网友评论

          本文标题:【Android 开发】自定义View(下)——仪表盘

          本文链接:https://www.haomeiwen.com/subject/wcslbctx.html