zoukankan      html  css  js  c++  java
  • Canvas、Paint、的简单使用及辅助类(Path、Shader、简介)

    1.Canvas类

    作用:1.绘制各种图形、图片、字等。2.提供各种方法进行坐标变换(rotate、scale、skew、translate)

    获取Canvas:一般是子类继承View并重写onDraw()方法

    2.Paint类

    作用:用于设置绘制风格,例如:画笔颜色、画笔笔触粗细、连接风格等。

    获取Paint类:直接 new Paint();

    3.Path类

    作用:代表任意多条直线连接而成的任意图形。Canvas根据Path绘制时,可以绘制出任意形状。

    子类:用来绘制各种各样的路径效果。(查询,不展开)参考资料:http://blog.csdn.net/x605940745/article/details/17141987

    简单使用(参考资料):http://blog.sina.com.cn/s/blog_4d9c3fec0102vyhs.html

    4.示例

    /**
     *创建View
    */
    public class TestView extends View {
        public TestView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            /*利用Canvas画板画出长方形*/
            Paint paint1 = new Paint();
            paint1.setColor(Color.RED);
            canvas.drawColor(Color.WHITE);
            canvas.drawRect(80,80,120,120,paint1);
    
            /*利用Canvas类绘制椭圆形*/
            RectF rectF = new RectF(80,150,130,200);
            canvas.drawOval(rectF,paint1);
    
            /*利用Path类画出三角形*/
            Paint paint2 = new Paint();
            paint2.setAlpha(40);
            paint2.setAntiAlias(true);
            paint2.setStyle(Paint.Style.STROKE);
            Path path = new Path();
            path.moveTo(10,340);
            path.lineTo(70, 340);
            path.lineTo(40, 290);
            path.close();
            canvas.drawPath(path,paint2);
    
    
        }
    }
    View Code
    <!--在res/layout/activity_main.xml中-->
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.chen.android.test.MainActivity">
    
        <com.chen.android.test.TestView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

    5.Shader类

    作用:类似<graient>标签,用来进行渐变变换的

    Shader.TileMode有3种参数:1.CLAMP的作用是如果渲染器超出原始边界范围,则会复制边缘颜色对超出范围的区域进行着色。

                                                           2.REPEAT的作用是在横向和纵向上以平铺的形式重复渲染位图。

                                                           3.MIRROR的作用是在横向和纵向上以镜像的方式重复渲染位图。

    子类:LinearGradient(线性渲染)、ComposeShader(混合渲染)、RadialGradient(环形渲染)、SweepGradient(梯度渲染)

    现在还不理解怎么使用(参考资料):http://www.cnblogs.com/menlsh/archive/2012/12/09/2810372.html

  • 相关阅读:
    leetcode[68] Climbing Stairs
    leetcode[67] Plus One
    17_8_16 接口实例化的方法
    17_8_15 lambda 表达式
    17_8_14 回调函数
    17_8_11 Spring Jdbc+Dbcp
    17_8_10 eclipse 中复制网上代码 出现 报错 问题(0)
    17_8_10 PostgreSql Mac
    17_8_10 项目开发流程
    17_8_9 html 不常用的标签
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5320962.html
Copyright © 2011-2022 走看看