从Android绘制View小例子中深入理解自定义View

Posted zhangmingcsdn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Android绘制View小例子中深入理解自定义View相关的知识,希望对你有一定的参考价值。

平时开发界面时,android系统为我们提供了各种各样的View组件,TextView、ImageView、Button、LinearLayout、ScrollView、ListView等等,这些也基本满足了平时的开发要求。有时候开发也会遇到比较刁钻的需求,自定义View多多少少就会用到。

彻底搞懂自定义View并不容易,因为牵扯到尺寸计算、Location(定位)、Canvas、矩阵计算等等,内容还是挺多的。记得刚入门Android时很快搞定了一个自定义View,后面才发现这还远远不够。

1,首先要配置View的尺寸和位置

1)比如实现一个正方形的View,保持高度和宽度一致

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

2)配置View在父View中的位置,使用onLayout

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

2,在绘制内容前,你要准备好画笔

        prPaint = new Paint();
        //防锯齿
        prPaint.setAntiAlias(true);
        //防抖动
        prPaint.setDither(true);
        prPaint.setStrokeCap(Paint.Cap.ROUND);
        prPaint.setStyle(Paint.Style.STROKE);
        prPaint.setStrokeWidth(PR_STROKE_WIDTH);

        dot = new Path();
        //指定顺时针方向(CW)
        dot.addCircle(0, 0, DOTTED_LINE_WIDTH / 2, Path.Direction.CW);

        dotPaint = new Paint();
        dotPaint.setAntiAlias(true);
        dotPaint.setDither(true);
        dotPaint.setStyle(Paint.Style.STROKE);
        dotPaint.setColor(Color.WHITE);

        innerCircle = new Paint();
        innerCircle.setAntiAlias(true);
        innerCircle.setDither(true);
        innerCircle.setStyle(Paint.Style.STROKE);
        innerCircle.setStrokeWidth(INNER_LINE);
        innerCircle.setColor(themeColor);

        effects_line = new DashPathEffect(new float[]{30, 10}, 1);

3,将内容通过画布绘制出来

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        dotDrawRect.set(dotRect);
        final float centerX = dotDrawRect.centerX();
        final float centerY = dotDrawRect.centerY();
        float radius = dotDrawRect.width() / 2;

        initPathEffect();

        dotPaint.setPathEffect(effects1);
        canvas.drawCircle(centerX, centerY, radius, dotPaint);

        radius = radius - DOTTED_LINE_PADDING;
        dotPaint.setPathEffect(effects2);
        canvas.drawCircle(centerX, centerY, radius, dotPaint);

        radius = radius - DOTTED_LINE_PADDING;
        dotPaint.setPathEffect(effects3);
        canvas.drawCircle(centerX, centerY, radius, dotPaint);

        radius = radius - DOTTED_LINE_PADDING;
        dotPaint.setPathEffect(effects4);
        canvas.drawCircle(centerX, centerY, radius, dotPaint);

        radius = radius - DOTTED_LINE_PADDING;
        innerCircle.setPathEffect(null);
        innerCircle.setColor(Color.WHITE);
        canvas.drawCircle(centerX, centerY, radius, innerCircle);

        radius = (float) (radius - DOTTED_LINE_PADDING * 1.5);
        innerCircle.setPathEffect(effects_line);
        innerCircle.setColor(themeColor);
        canvas.drawCircle(centerX, centerY, radius, innerCircle);

        radius = radius - PR_STROKE_WIDTH / 2 - DOTTED_LINE_PADDING / 2;

        if (powInterRadius == 0) {
            powInterRadius = Math.pow(radius, 2);
        }
        //进度条绘制
        dotDrawRect.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        prPaint.setColor(Color.GRAY);
        canvas.drawArc(dotDrawRect, 120, 300, false, prPaint);

        prPaint.setColor(Color.WHITE);
        canvas.drawArc(dotDrawRect, 120, progress * 3, false, prPaint);

    }

4,如果需要和用户交互,还需要处理触摸、点击这些事件(以后再仔细分析)

源码:http://git.oschina.net/hanbingsheshou/SimpleDraw



以上是关于从Android绘制View小例子中深入理解自定义View的主要内容,如果未能解决你的问题,请参考以下文章

深入理解 Android 之 View 的绘制流程

深入理解Android之View的绘制流程

Android自定义View(3) 《Canvas绘制简单的图形》

Android 面试题总结之Android 进阶

Android自定义View实战教程5??---Canvas详解及代码绘制安卓机器人

Android 面试题总结之Android 进阶