美文网首页
自定义View项目实战(二):Canvas绘制几何图形

自定义View项目实战(二):Canvas绘制几何图形

作者: bug音音 | 来源:发表于2020-12-16 14:47 被阅读0次

    android.graphics为我们提供了一些列的类,我们可以应用这些类来绘制2D向量图形。一些常见的类如下:

    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.LinearGradient;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.RectF;
    import android.graphics.Shader;
    

    其中在图形绘制上最重要的类是:Canvas和Paint。
    Canvas相当于一张图纸,所有的图形都在它的上面绘制出来;
    Paint相当于画笔,可以设置不同的颜色 绘制不同的图形
    接下来我们就以简单的实例来说明几何图形的绘制方法:
    首先:自定义继承View的MyView,并设置当前activity的contentView为MyView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            MyView myView = new MyView(this);
            setContentView(myView);
    }
    private class MyView extends View{
    
            public MyView(Context context){
    
                super(context);
            }
    }
    

    覆盖MyView类的onDraw方法:

    protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
    
                //设置背景为白色
                canvas.drawColor(Color.WHITE);
    
                Paint paint = new Paint();
    
                //去锯齿
                paint.setAntiAlias(true);
    
                //设置paint颜色
                paint.setColor(Color.RED);
    }
    

    接下来就是具体的绘制过程了,绘制代码在onDraw方法中实现:
    绘制空心圆形 空心正方形 空心长方形 空心椭圆形 空心三角型 和空心梯形

            /**
             *         FILL,
             *         FILL_AND_STROKE,
             *         STROKE;
             */
            //设置paint的style为空心
            paint.setStyle(Paint.Style.STROKE);
    
    
            //设置paint的外框宽度
            paint.setStrokeWidth(3);
    
            //画一个空心圆形
            canvas.drawCircle(40,40,30,paint);
    
            //画一个空心正方形
            canvas.drawRect(10,90,70,150,paint);
    
            //画一个空心长方形
            canvas.drawRect(10,170,70,200,paint);
    
    
            //画一个空心椭圆型
            RectF rectF = new RectF(10,220,70,250);
            canvas.drawOval(rectF,paint);
    
    
            //画一个空心三角形
            Path path = new Path();
            path.moveTo(10,300);
            path.lineTo(70,330);
            path.lineTo(40,270);
            path.close();
            canvas.drawPath(path,paint);
    
    
            ////画一个空心梯形
            Path path1 = new Path();
            path1.moveTo(10,410);
            path1.lineTo(70,410);
            path1.lineTo(55,350);
            path1.lineTo(25,350);
            path1.close();
            canvas.drawPath(path1,paint);
    

    设置实心是样式和颜色,然后分别绘制实心圆形 实心正方形 实心长方形 实心椭圆型 实心三角形 实心梯形

            //设置paint的style为FILL
            paint.setStyle(Paint.Style.FILL);
    
             //设置paint的颜色
            paint.setColor(Color.BLUE);
    
            //画一个实心圆形
            canvas.drawCircle(120,40,30,paint);
    
            //画一个实心正方形
            canvas.drawRect(90,90,150,150,paint);
    
            //画一个实心长方形
            canvas.drawRect(90,170,150,200,paint);
    
            //画一个实心椭圆
            RectF rectF1 = new RectF(90,220,150,250);
            canvas.drawOval(rectF1,paint);
    
            //画一个实心三角形
            Path path2 = new Path();
            path2.moveTo(90,330);
            path2.lineTo(150,330);
            path2.lineTo(120,270);
            path2.close();
            canvas.drawPath(path2,paint);
    
            //画一个实心梯形
            Path path3 = new Path();
            path3.moveTo(90,410);
            path3.lineTo(150,410);
            path3.lineTo(135,350);
            path3.lineTo(105,350);
            path3.close();
            canvas.drawPath(path3,paint);
    

    设置渐变色的样式和颜色,并分别绘制对应的几何图形

            //设置渐变色
            Shader shader = new LinearGradient(0,0,100,100,new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},null,Shader.TileMode.REPEAT);
    
            //画一个渐变色的圆形
            paint.setShader(shader);
    
            //画一个渐变色的正方形
            canvas.drawCircle(200,40,30,paint);
    
            //画一个渐变色的正方形
            canvas.drawRect(170,90,230,150,paint);
    
            //画一个渐变色的长方形
            canvas.drawRect(170,170,230,200,paint);
    
            //画一个渐变色的椭圆形
            RectF rectF2 = new RectF(170,220,230,250);
            canvas.drawOval(rectF2,paint);
    
    
    
            //渐变色的三角形
            Path path4 = new Path();
            path4.moveTo(170,330);
            path4.lineTo(230,330);
            path4.lineTo(200,270);
            path4.close();
            canvas.drawPath(path4,paint);
    
    
            //渐变色的梯形
            Path path5 = new Path();
            path5.moveTo(170,410);
            path5.lineTo(230,410);
            path5.lineTo(215,350);
            path5.lineTo(185,350);
            path5.close();
            canvas.drawPath(path5,paint);
    

    我们除了可以利用以上方法绘制几何图形以外,我们还可以canvas的drawText方法来绘制文字,使用实例如下

            //绘制文字
            paint.setTextSize(24);
            canvas.drawText(getResources().getString(R.string.str_text1),240,50,paint);
            canvas.drawText(getResources().getString(R.string.str_text2),240,120,paint);
            canvas.drawText(getResources().getString(R.string.str_text3),240,190,paint);
            canvas.drawText(getResources().getString(R.string.str_text4),240,250,paint);
            canvas.drawText(getResources().getString(R.string.str_text5),240,320,paint);
            canvas.drawText(getResources().getString(R.string.str_text6),240,390,paint);
    

    相关文章

      网友评论

          本文标题:自定义View项目实战(二):Canvas绘制几何图形

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