android中的Paint用法

为啥写这个呢?因为网上翻了很多大神的帖子,也没看懂到底怎么使用,打什么只写Paint的使用中的各种参数特征,却没人写怎么使用,翻了好多帖子竟然是重复的,尼玛博客也抄袭,有意思吗?

关于Paint的用法(小白思考的问题),我摸索出两种,在这个过程中也加深了我对XML添加各种控件的理解。

调用Paint需要继承View方法,重写onDraw(Canvas canvas)方法,为啥不能直接new一个Paint,new一个Canvas我也不能理解,可以直接在MainActivity中直接使用这种方式画图的大神请教一下我,如果不能麻烦告诉一下原因。

刚学习android的可能不清楚Paint和Canvas是个啥子。按照我目前的理解Paint相当于颜料盒和颜料笔,可以在颜料盒中设置你画笔的各种特性,Canvas相当于工具,模板或者执笔的人,你可以直接调用里面的方法画一些基本图形。那现在唯一缺的也就是画布了,我猜想不能直接两个new就画图的原因是没有画布。

1.怎么调用Paint,方法一

在XML以控件的方式调用

在XML中调用画图实际上是以控件的形式调用的,这种方式比较死板,就像你放置按键一样放置一个特定图形。

具体方法如下,

1.新建一个类,实现画出矩形

   public class RectView extends View {
        Paint paint;
        //创建构造函数
//        public RectView(Context context) {
//            super(context);
//        }
        public RectView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            setBackgroundResource(R.color.colorPrimary);//设置背景色

            //----绘制矩形------------
            paint = new Paint();// 定义画笔
            paint.setStyle(Paint.Style.FILL);//FILL填充内部;STROKE不填充,硬笔线;FILL_AND_STROKE和FILL没有看出明显不同
            paint.setAntiAlias(true);// 消除锯齿
            paint.setColor(Color.RED);//设置画笔颜色
            paint.setStrokeWidth(1);// 设置paint的外框宽度
            canvas.drawRect(200, 400, 800, 220, paint);//绘制矩形
        }
    }

2.在XML中,添加你刚才创建的类,要精度到包到类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.stepviewtest.RectView
        android:id="@+id/viewTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </com.example.stepviewtest.RectView>

</LinearLayout>

至此就完成了,这个矩形就以控件的方式添加到屏幕上了。

在创建这个类的时候遇到一个问题,程序运行就崩溃,导致这个问题的愿意是没有实现

public RectView(Context context, AttributeSet attrs)

这个构造方法,一直没想通,百度了一下View的这个构造方法大概明白了,在XML中调用的控件要有自己的属性

AttributeSet对应的就是设置的属性值集合。

具体的大家可以看这个帖子再了解一下https://blog.csdn.net/hp910315/article/details/48972335

2.怎么调用Paint,方法二

已布局的方式调用

这种方法有点复杂,但是应该也是项目中常用的方法

1.这种方式实际上是以Fragment的方式调用的,在继承View的基础上在加一个继承Fragment的类,如下(我用的内部类)

public class DrawRectTest extends Fragment
{
//    @Nullable
//    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return new RectView(container.getContext());
    }

    public class RectView extends View {
        Paint paint;
        //创建构造函数
        public RectView(Context context) {
            super(context);
        }
        public RectView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            setBackgroundResource(R.color.colorPrimary);//设置背景色

            //----绘制矩形------------
            paint = new Paint();// 定义画笔
            paint.setStyle(Paint.Style.FILL);//FILL填充内部;STROKE不填充,硬笔线;FILL_AND_STROKE和FILL没有看出明显不同
            paint.setAntiAlias(true);// 消除锯齿
            paint.setColor(Color.RED);//设置画笔颜色
            paint.setStrokeWidth(1);// 设置paint的外框宽度
            canvas.drawRect(200, 0, 800, 220, paint);//绘制矩形
        }
    }
}
这种方式必须实现下面这个构造函数
 public RectView(Context context)

2.需要在XML中加入Fragment,这是调入布局常用的方法,各位大神应该比较清楚,不明白的小白可以看《第一行代码》中第四章——碎片,刚才XML中的代码变成了这个

<FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

3.在MainActivity中调用这个布局

 protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      DrawRectTest mDrawRect = new DrawRectTest();
      getFragmentManager().beginTransaction().replace(R.id.container, mDrawRect).commit();
}
 

只需要添加上面两行代码就OK拉!


这篇文章希望对大家有帮助,下班回家吃饭啦!!!!




猜你喜欢

转载自blog.csdn.net/jxwzh/article/details/81019865