Android onMeasure与onDraw及自定义属性使用示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android onMeasure与onDraw及自定义属性使用示例

weixin_43912367   2023-03-21 我要评论

1.自定义View简介

自定义view可以被认为是继承自View,系统没有的效果(ImageView,TextView,Button),extents View,extents ViewGrop

2.构造方法

继承View。View有四个构造方法,下面讲述四个构造方法什么时候调用:

第一个构造方法会在代码中new的时候调用

TextView textView = new TextView(this);

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

第二个构造方法在布局layout中使用(调用)

<com.zrc.view_java_demo_01.TextView android:layout_width="match_parent" android:layout_height="match_parent"/>

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

第三个构造方法在布局layout中使用(调用),但是会有style

调用 <com.zrc.view_java_demo_01.TextView style="@style/defualt"/>

<style name="defualt" > <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">@color/colorAccent</item> </style>
 public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

还有第四个构造方法,在用到时候,再做说明,在这里就不再展开。

3.onMeasure()

获取宽高的模式

int widthSize = MeasureSpec.getMode(widthMeasureSpec); //获取前两位
int heightSize = MeasureSpec.getMode(heightMeasureSpec);

获取宽高的值,指定控件的宽高

 int widthSize = MeasureSpec.getSize(widthMeasureSpec); //获取后面30位
 int heightSize = MeasureSpec.getSize(heightMeasureSpec);

MeasureSpec.AT_MOST:在布局中指定了wrap_content

MeasureSpec.EXACTLY: 在布局中特定的值 100dp match_parent fill_parent

MeasureSpec.UNSPECIFIED:尽可能的大,很少用到。listview,Scrollview 在测量子布局时候会用UNSPECIFIED

Scrollview+ListView会出现显示不全的现象?

widthMeasureSpec widthMeasureSpec : 会包含两个信息是一个32位的值,第一个信息是模式:2位 值:30位

4.onDraw()

 /**
     * 用于绘制
     * */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画文本
        canvas.drawText();
        //画弧
        canvas.drawArc();
        //画圆
        canvas.drawCircle();
    }

5.onTouch()

    /**
     * 处理用户交互的,手指触摸等等(事件分发事件拦截)
     * */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //手指按下
                Log.e("TAG","手指按下");
                break;
            case MotionEvent.ACTION_MOVE:
                //手指移动
                Log.e("TAG","手指移动");
                break;
            case MotionEvent.ACTION_UP:
                //手指抬起
                Log.e("TAG","手指抬起");
                break;
        }
        return super.onTouchEvent(event);
    }

6.自定义属性

自定义属性就是用来配置的,android:text = "Darren"是系统自定义属性

6.1在res下的values下面新建attrs.xml

<!-- name 属性名称
         format 格式: string 文字  color:颜色
                       dimension 宽高 字体大小 integer 数字
                       reference 资源(drawable)
          -->
        <attr name="text" format="string"/>
        <attr name="textColor" format="color"/>
        <attr name="textSize" format="dimension"/>
        <attr name="maxLength" format="integer"/>
        <attr name="background" format="reference|color"/>
        <!-- 枚举 -->
        <attr name="inputType">
            <enum name="number" value="1"/>
            <enum name="text" value="2"/>
            <enum name="password" value="3"/>
        </attr>
    </declare-styleable>

6.2在布局中使用

声明命名空间,然后在自己的自定义View中使用

xmlns:app="http://schemas.android.com/apk/res-auto"
<com.zrc.view_java_demo_01.TextView
        app:text="Darren"
        app:textColor="@color/colorAccent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

6.3在自定义View中获取属性

 // 获取自定义属性
 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextView);
 mText = array.getString(R.styleable.TextView_text);
 mTextColor = array.getColor(R.styleable.TextView_textColor,mTextColor);
  mTextSize = array.getDimensionPixelSize(R.styleable.TextView_textSize,mTextSize);
 // 回收
 array.recycle();

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们