美文网首页
二十二、Android绘图基础Canvas、Paint等

二十二、Android绘图基础Canvas、Paint等

作者: 清梦星河哈 | 来源:发表于2019-07-30 18:07 被阅读0次
    Canvas

    重新onDraw(Canvas canvas)方法时涉及绘图API :Canvas
    Canvas绘制方法

    drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint
    //绘制弧
    
    drawBitmap(Bitmap bitmap,Rect src, Rect dst,Paint paint)
    //在指定点绘制从源位图中“挖取”的一块
    
    drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
    //在指定点绘制位图
    
    drawCircle(float cx, float cy, float radius, Paint paint)
    //在指定点绘制一个圆
    
    drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
    //绘制一条线
    
    drawLines(float[] pts, int offset, int count, Paint paint)
    //绘制多条线
    
    drawOval(RectF oval, Paint paint)
    //绘制椭圆
    
    drawPath(Path path, Paint paint)
    //沿着指定Path绘制任意形状
    
    drawPoint(float x, float y, Paint paint)
    //绘制一个点
    
    drawPoints(float[] pts, int offset, int count, Paint paint)
    //绘制多个点
    
    drawRect(float left, float top, float right, float bottom, Paint paint)
    //绘制一个矩形
    
    drawRoundRect(RectF rect, float rx, float ry, Paint paint)
    //绘制一个圆角矩形
    
    drawText(String text, float x, float y, Paint paint)
    //绘制字符串
    
    drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)
    //沿着路径绘制字符串
    
    clipRect(float left, float top, float right, float bottom)
    //剪切一个矩形区域
    

    除上表定义的方法以外,Canvas还提供如下方法:
    1.rotate(float degrees, float px, float py)对Canvas旋转
    2.scale(float sx, float sy, float px, float py)对Canvas放缩
    3.translate(float dx, float dy)对Canvas移动。dx为X轴方向移动,dy为Y轴方向移动。

    Paint 画笔
    setARGB();/setColor();//设置颜色
    
    setAlpha(int a) //设置透明度
    
    setAntiAlias(boolean aa) //设置是否抗锯齿
    
    setColor(int color)//设置颜色
    
    setPathEffect(PathEffect effect)//设置绘制路径是路径效果
    
    setShader(Shader shader)//设置画笔填充效果
    
    setShadowLayer(float radius, float dx, float dy, int shadowColor)//设置阴影
    
    setStrokeWidth(float width)//设置画笔触宽度
    
    setStrokeJoin(Paint.Join join)//设置画笔转弯处的连接风格
    
    setStyle(Paint.Style style)//设置Paint的填充风格
    
    setTextAlign(Paint.Align align)//设置绘制文本时的文字对齐方式
    
    setTextSize(float textSize)//设置绘制文本时的文字大小
    
    PathEffect 类

    PathEffect为路径提供不同效果,包含如下子类:
    1.ComposePathEffect
    2.CornerPathEffect
    3.DashPathEffect
    4.DiscretePathEffect
    5.PathDashPathEffect
    6.SumPathEffect

    例子:

        float phase;
    
        PathEffect[] effects = new PathEffect[7];
    
        int[] colors;
    
        private Paint paint;
    
        Path path;
    
        public MyView(Context context) {
            super(context);
            init();
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init(){
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(4);
            //创建Path
            path = new Path();
            path.moveTo(0,0);
            for(int i = 1; i <= 15; i++){
                path.lineTo(i*20, (float)(Math.random() * 100));
            }
            colors = new int[]{
                    Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA,
                    Color.RED, Color.YELLOW
            };
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);//白色背景
            //七种路径效果
            effects[0] = null;
            effects[1] = new CornerPathEffect(10);
            effects[2] = new DiscretePathEffect(3.0f, 5.0f);
            effects[3] = new DashPathEffect(new float[]{20,10,5,10}, phase);
            Path p = new Path();
            p.addRect(0, 0, 8, 8, Path.Direction.CCW);
            effects[4] = new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Style.ROTATE);
            effects[5] = new ComposePathEffect(effects[2], effects[4]);
            effects[6] = new SumPathEffect(effects[4], effects[3]);
            canvas.translate(8,8);
            for(int i = 0; i < effects.length; i++){
                paint.setPathEffect(effects[i]);
                paint.setColor(colors[i]);
                canvas.drawPath(path, paint);
                canvas.translate(0,100);
            }
            phase += 1;
            invalidate();
        }
    

    效果如图:


    相关文章

      网友评论

          本文标题:二十二、Android绘图基础Canvas、Paint等

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