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

 

转载于:https://www.cnblogs.com/rookiechen/p/5320962.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值