1

前言

在Android开发中经常会遇到特殊的需求要求开发者实现独特的视图UI。所以对于开发者来说自定义View可以实现多种多样的需求效果。那这次我们就来说说关于自定义View的实现。

View绘制原理

View的绘制是在方法onDraw中实现的。通过获取View的画布canvas,使用Panit在canvas上绘制内容。

 public class CustomerView extends View {
  @Override
  protected void onDraw(Canvas canvas) {
  
  }
}

画笔Paint

就和我们实际画画一样,绘制需要一支画笔才行。Paint就是我们绘制所需要的画笔。实例化Paint并设置绘制所需要的参数就能绘制出自己所需要的内容。Android开发文档中Paint实现方法很多,这里我们介绍几个常用基本的方法。

setAlpha(bookean a) //设置抗锯齿
setColor(int color) //设置画笔颜色
setShader(Shader shader)//设置画笔渐变颜色
setStyle(Paint.Style style)//设置画笔模式(例如实心/空心)
setXfermode(Xfermode xfermode)//设置画笔绘制模式
setStrokeWidth(float width) //设置画笔粗细

画布Canvas

有了画笔,现在就是要用画笔在画布上画画了。想要绘制怎么样的图形是通过Canvas的方法实现的。我们也介绍几个常用基本的绘制方法。只要理科学的好人人都是现代派画家O(∩_∩)O~。

drawARGB(int a, int r, int g, int b)//设置画布背景颜色
drawColor(int color)//设置画布背景颜色
drawRect(float left, float top, float right, float bottom, Paint paint)//绘制矩形
drawPath(Path path, Paint paint)//绘制自定义图形
drawCircle(float cx, float cy, float radius, Paint paint)//绘制原型
drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)//绘制弧以及圆
drawLine(float startX, float startY, float stopX, float stopY, Paint paint)//绘制线条
drawText(String text, int start, int end, float x, float y, Paint paint)//绘制文字
drawPoint(float x, float y, Paint paint)//绘制点
drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)//绘制圆角矩形
drawOval(RectF oval, Paint paint)//绘制圆
drawBitmap(Bitmap bitmap, float left, float top, Paint paint) //绘制图片

画起来

知道了画笔Paint和画布Canavs的基本方法,最后我们实践画一画为成为画家迈出第一步。

public class NormalView extends BaseView {
    Path triangle2;
    Path all;
    private float[] mTickRightStartPosition = new float[2];
    public NormalView(Context context) {
        super(context);
    }

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

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

    void initView(){
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCanVas = canvas;
        //绘制正方形
        mCanVas.drawRect(20,40,60,80,mPaint);
        //绘制矩形
        mCanVas.drawRect(20,100,60,160,mPaint);
        //绘制三角形
        Path path = new Path();
        path.moveTo(60, 200);// 此点为多边形的起点
        path.lineTo(40, 250);
        path.lineTo(80, 250);
        path.close(); // 使这些点构成封闭的多边形
        mCanVas.drawPath(path,mPaint);
        //画圆
        mCanVas.drawCircle(40,400,40,mPaint);
        //画文字
        mPaint.setTextSize(50);
        mCanVas.drawText("Hello world!",0,12,40,500,mPaint);
        //画图片
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);
        mCanVas.drawBitmap(bitmap,40,600,mPaint);
        //画背景色
        mCanVas.drawARGB(100,100,100,100);
        //画弧以及画椭圆
        RectF rectF = new RectF();
        Rect rect = new Rect(40,750,150,800);
        rectF.set(rect);
        mCanVas.drawArc(rectF,0,-180,true,mPaint);
        //画线
        mCanVas.drawLine(40,850,100,900,mPaint);
        //画点
        mCanVas.drawPoint(40,950,mPaint);
        //画圆角矩形
        RectF roundRectF = new RectF();
        Rect roundRect = new Rect(40,1000,400,1050);
        roundRectF.set(roundRect);
        mCanVas.drawRoundRect(roundRectF,20,5,mPaint);
        //画圆
        RectF OvalRectF = new RectF();
        Rect OvalRect = new Rect(40,1100,60,1200);
        OvalRectF.set(OvalRect);
        mCanVas.drawOval(OvalRectF,mPaint);

        Path hexagon = new Path();
        hexagon.moveTo(40,1400);
        hexagon.lineTo(90,1350);
        hexagon.lineTo(140,1350);
        hexagon.lineTo(190,1400);
        hexagon.lineTo(140,1450);
        hexagon.lineTo(90,1450);
        hexagon.lineTo(40,1400);
        hexagon.close();
        mCanVas.drawPath(hexagon,mPaint);

        Path triangle1 = new Path();
        triangle1.moveTo(500,100);
        triangle1.lineTo(500,300);
        triangle1.lineTo(700,300);
        triangle1.lineTo(500,100);
        triangle1.close();

        triangle2 = new Path();
        triangle2.moveTo(700,100);
        triangle2.lineTo(900,100);
        triangle2.lineTo(900,300);
        triangle2.lineTo(700,100);
        triangle2.close();

        all = new Path();
        all.addPath(triangle1);
        all.addPath(triangle2);
        mCanVas.drawPath(all,mPaint);
    }
}

图片描述
实践成功!我们接下来还要继续努力,成为画家这还是刚开始呢。下一次我们继续学习关于View的内容。Fighting!


一个无
110 声望6 粉丝