自定义View-4-重写onDraw

效果图

这里写图片描述

页面代码

public class SouthView extends View {
    private Paint mPaint;
    private int mRadius;
    private int mCirclrRadius;
    private float mDegrees=0;

    public SouthView(Context context) {
        super(context);
    }

    public SouthView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SouthView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        int width = canvas.getWidth();
        mRadius = width / 2;
        mCirclrRadius = mRadius - 15;

        canvas.translate(width / 2, width / 2);//移动画布原点

        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.bg_black));//设置画笔颜色
        mPaint.setStyle(Paint.Style.FILL);//设置实心
        mPaint.setAntiAlias(true);//抗锯齿
        canvas.drawCircle(0, 0, mRadius, mPaint);//以原点为圆心画圆

        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);

        RectF rectF = new RectF(-mCirclrRadius, -mCirclrRadius, mCirclrRadius, mCirclrRadius);
        for (int i = 0; i < 4; i++) {
            canvas.drawArc(rectF, 10, 70, false, mPaint);
            canvas.rotate(90);
        }

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_south);
        Matrix matrix = new Matrix();
        float f = (float) width / bitmap.getWidth();
        matrix.postScale(f, f);//缩小至相应比例
        Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        canvas.rotate(mDegrees++);
        canvas.drawBitmap(dstbmp, -bitmap.getWidth() * f / 2, -bitmap.getWidth() * f / 2, mPaint);
        canvas.rotate(-mDegrees*5);

    }

    public void rotateBmp(){
        while (true){
            postInvalidate();//子线程更新UI
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

使用方法

southView=(SouthView)view.findViewById(R.id.view_south);
new Thread(new Runnable() {
   @Override
   public void run() {
       southView.rotateBmp();
   }
}).start();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

如果你喜欢我的风格,还可以看下这篇文章canvas和paint画劳力士表盘

posted @ 2017-04-22 18:23  天涯海角路  阅读(142)  评论(0)    收藏  举报