美文网首页
Android自定义View(1) 《自定义View重要方法及参

Android自定义View(1) 《自定义View重要方法及参

作者: 非典型程序猿 | 来源:发表于2021-08-22 13:54 被阅读0次

概述

在Android中,我们经常会需要去绘制一些自己需要的控件,所以继承自View的自定义View就产生了。这篇文章主要介绍View中的几个重要方法及参数。

创建一个自定义View

首先我们先简单写一个自定义View,为了降低学习门槛这里仍使用java书写此示例

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * create by xu.tian
 *
 * @date 2021/8/22
 */
public class MyView extends View {

    public static final String TAG = MyView.class.getSimpleName();

    private Paint paint = new Paint();

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        Log.d(TAG,"onDraw");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        Log.d(TAG,"onMeasure");
        Log.d(TAG,"width ---> "+MeasureSpec.getSize(widthMeasureSpec)+"\n"+
                "height ---> "+MeasureSpec.getSize(heightMeasureSpec));

    }

    @Override
    protected void onLayout(boolean changed , int left ,int top ,int right , int bottom){
        super.onLayout(changed, left, top, right, bottom);
        Log.d(TAG,"onLayout");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w,h,oldw,oldh);
        Log.d(TAG,"onSizeChanged width ---> "+w+"\n"+
                "height ---> "+h+"\n"+
                "oldw ---> "+oldw+"\n"+
                "oldh ---> "+oldh);
    }
 @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.d(TAG,"onDetachedFromWindow");
    }
}

上述简单的View没有进行任何绘制,运行后的日志输出如下

2021-08-22 13:00:45.415 27174-27174/com.tx.camera D/MyView: onMeasure
2021-08-22 13:00:45.415 27174-27174/com.tx.camera D/MyView: width ---> 1080
    height ---> 2304
2021-08-22 13:00:45.423 27174-27174/com.tx.camera D/MyView: onMeasure
2021-08-22 13:00:45.423 27174-27174/com.tx.camera D/MyView: width ---> 1080
    height ---> 2304
2021-08-22 13:00:45.424 27174-27174/com.tx.camera D/MyView: onSizeChanged width ---> 1080
    height ---> 2304
    oldw ---> 0
    oldh ---> 0
2021-08-22 13:00:45.424 27174-27174/com.tx.camera D/MyView: onLayout
2021-08-22 13:00:45.434 27174-27174/com.tx.camera D/MyView: onDraw

可以明显的看到依次执行
onMeasure > onSizeChanged > onLayout > onDraw
关闭应用后,日志打印

2021-08-22 13:26:51.777 2491-2491/com.tx.camera D/MyView: onDetachedFromWindow

方法及参数说明

  1. onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    根据该方法主要在View测量完宽高后回调
  • widthMeasureSpec 水平方向上的距离
  • heightMeasureSpec 垂直方向上的距离
  1. onSizeChanged(int w, int h, int oldw, int oldh)
  • w 当前水平方向的距离
  • h 当前垂直方向的距离
  • oldw 变化之前的水平方向的距离
  • oldh 变化之前的垂直方向的距离
  1. onLayout(boolean changed , int left ,int top ,int right , int bottom)
    当view在父控件中的位置测量固定后回调,参数分别为
  • changed 位置是否为新位置,是否发生了变化
  • left view的左边界
  • top view的上边界
  • right view的右边界
  • bottom view的下边界
  1. onDraw
    当view绘制自身内容后回调
  • canvas 图层对象,用来绘制新的内容,在自定义的开发中,我们主要的绘制工作也在这里进行
  1. onDetachedFromWindow
    当view被window移除时回调

总结

自定义View在绘制时主要进行了如下几步

    1. 测量本身的大小,完成后回调onMeasure方法,如果大小改变时还会回调onSizeChanged方法
    1. 测量在父控件中的位置,完成后回调onLayout方法
    1. 开始绘制自身内容,回调onDraw方法
    1. 离开界面时调用onDetachedFromWindow方法
  • 在下篇文章我们开始介绍如何绘制简单的图形

相关文章

网友评论

      本文标题:Android自定义View(1) 《自定义View重要方法及参

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