首页 > 代码库 > Android之自定义View学习(一)

Android之自定义View学习(一)

Android之自定义View学习(一)

Canvas常用方法:

技术分享

                                   图片来源

技术分享

 

public class BaseViewDraw extends View {
    private Paint mPaint1;
    private Paint mPaint2;
    private Paint mPaint3;

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

    public BaseViewDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();//初始化画笔
    }

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

    private void initPaint() {
        mPaint1 = new Paint();
        mPaint1.setColor(Color.BLUE);//设置画笔颜色
        mPaint1.setStrokeWidth(10f);//设置画笔宽度
        mPaint1.setStyle(Paint.Style.FILL);//设置画笔填充模式

        mPaint2 = new Paint();
        mPaint2.setColor(Color.RED);
        mPaint2.setStrokeWidth(20f);
        mPaint2.setStyle(Paint.Style.FILL_AND_STROKE);

        mPaint3 = new Paint();
        mPaint3.setColor(Color.BLACK);
        mPaint3.setStrokeWidth(15f);
        mPaint3.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**
         * 画颜色
         * 了解更多canvas前往官方文档:https://developer.android.com/reference/android/graphics/Canvas.html
         */
        canvas.drawColor(Color.GRAY);
        /**
         * 画点
         */
        canvas.drawPoint(200, 200, mPaint1);//画一个点----200,200分别代表在想,x,y轴上的坐标
        canvas.drawPoints(
                new float[]{//画多个点
                        300, 300,
                        300, 400,
                        300, 500
                }, mPaint1);
        /**
         * 画线条
         */
        canvas.drawLine(10, 10, 200, 500, mPaint1);//10,10代表起点,200,500代表终点
        canvas.drawLines(new float[]{//画多条线段
                20, 20, 600, 20,
                50, 200, 50, 600}, mPaint1);

        /**
         * 画矩形,三种写法
         * 了解更多 Rect前往官方文档:https://developer.android.com/reference/android/graphics/Rect.html
         * 了解更多 RectF前往官方文档:http://developer.android.com/reference/android/graphics/RectF.html
         */
        //第一种
        canvas.drawRect(500, 100, 800, 400, mPaint1);
        //第二种
        final Rect rect = new Rect(500, 500, 800, 800);
        canvas.drawRect(rect, mPaint1);
        //第三种
        final RectF rectF = new RectF(500, 900, 800, 1200);
        canvas.drawRect(rectF, mPaint1);
        /**
         * 绘制圆角矩形
         */
        //方法一
        final RectF rectF1 = new RectF(100, 900, 400, 1200);
        //10,30分别代表圆弧的半径
        canvas.drawRoundRect(rectF1, 10, 30, mPaint2);
        //方法二
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //这种写法仅支持API21+
            canvas.drawRoundRect(100, 900, 400, 1200, 10, 30, mPaint2);
        }

        /**
         * 画椭圆
         * 350为矩形x轴长度的一半
         * 150为矩形y轴长度的一半
         */
        final RectF rectF2 = new RectF(100, 500, 800, 800);
        canvas.drawRoundRect(rectF2, 350, 150, mPaint3);

        /**
         * 画圆
         * 350为矩形x轴长度的一半
         * 350为矩形y轴长度的一半
         * 当矩形的长和宽相等时,即为圆
         */
        //方法一:利用矩形绘制椭圆技巧
        final RectF rectF3 = new RectF(100, 500, 800, 1200);
        canvas.drawRoundRect(rectF3, 350, 350, mPaint3);
        //方法二:550,800代表圆心位置,400代表圆半径
        canvas.drawCircle(550, 800, 400, mPaint3);

    }
}

github地址:https://github.com/SiberiaDante/DrawView

Android之自定义View学习(一)