View 的绘制流程(一)

来源:互联网 发布:如何开淘宝童装店 编辑:程序博客网 时间:2024/06/08 08:04

每个View 绘制过程中,基本上都经历 3 个主要步骤:

1、onMeasure()— 测量 ( 宽度高度 )
2、onLayout()— 布局 ( 安排在哪个位置 )
3、onDraw()— 显示内容

自定义View的引用
1、可以从View中继承,自定义一个视图

public class MyView extends View {    /**     * java的动态布局     * 一般用于java中直接new的方式创建视图     *     * @param context     */    public MyView(Context context) {        super(context);        setBackgroundColor(0x55ff0000);    }    /**     * 一般用于xml布局中     *     * @param context     * @param attrs     */    public MyView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }}

2、在Java中也可以直接new创建视图对象

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    MyView view = new MyView(this);    setContentView(view);}

如果要在xml中引用自定义的视图,需要使用视图的包名,类名的方式引入,在layout布局中编写

<com.kang.view.MyView    android:layout_width="200dp"    android:layout_height="300dp"    android:background="#550000ff" />

Paint(画笔)以及Canvas(画布)

Paint是可以将颜色信息画到Canvas上的画笔对象

// 设置消除锯齿paint.setAntiAlias(true);// 设置颜色paint.setColor(0xffff0000);// 设置字体大小paint.setTextSize(15);setStyle: 设置画笔风格,空心 / 实心setStrokeWidth: 空心的画笔边框宽度

Color

Color类提供了颜色的基本操作

常见颜色常量Color.REDColor.BLUEColor.BLACK...// 根据颜色值调色 , 每个值取值范围 0x00-0xff 十进制就是 0-255Color.argb(200,255,255,0)int color=Color.rgb(255,255,0);颜色转换int color = Color.parseColor("#9A32CD");int color = Color.parseColor("red");

视图刷新

主线程刷新:invalidate()方法

public void changeColor(){    if(index < colors.length-1) {    index++;    } else {    index = 0;    }    int color = colors[index];    paint.setColor(color);    // 刷新视图 , 导致视图重新绘制(重新执行 onDraw )    invalidate();}...Activity 中调用...MyView mView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mView = (MyView) findViewById(R.id.m_view);    findViewById(R.id.btn_change).setOnClickListener(this);}@Overridepublic void onClick(View v) {    mView.changeColor();}

子线程刷新:postInvalidate,等效于Handler+invalidate

private Thread mThread;public void startChange() {        mThread = new Thread() {            @Override            public void run() {                try {                    while (true) {                        Thread.sleep(200);                        if (index < colors.length - 1) {                            index++;                        } else {                            index = 0;                        }                        int color = colors[index];                        paint.setColor(color);                        //子线程中更新                        postInvalidate();                    }                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        };        mThread.start(); }

文本绘制

// 第二个参数表示基准线起始点 x 坐标// 第三个参数表示基准线起始点的 y 坐标canvas.drawText("abgf",0,50,paint);// 获取文本所占的区域Rect textRect = new Rect();// 获取文本区域paint.getTextBounds(text,0,text.length(),textRect);

获取屏幕宽度

Point p = new Point();// 获取窗口管理器WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);wm.getDefaultDisplay().getSize(p);screenWidth = p.x; // 屏幕宽度
原创粉丝点击