自定义view基础,onMeasure,onDraw,onTouchEvent,自定义属性

这里自定义一个TextView extends View

private String mText;
private int mColor = Color.BLACK;
private  int mTextSize = 15;

1,构造方法

  

 
 
// 初始化 new 的时候使用
// TextView tv = new TextView(this); public TextView(Context context) { super(context); } //在布局layout中使用(调用) /* <com.example.administrator.mytextview.TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> */ public TextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } /* 在布局layout中使用(调用),但是会用style * <com.example.administrator.mytextview.TextView style="@style/define_style" android:text="Hello World!" /> * * */ public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }

2,复写自定义view的测量方法 onMeasure();
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //布局的宽高,用这个方法去指定
    //控件(TextView)的宽高,需要测量

    //获取宽高模式.wrap_content,match_parent,100dp
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);   //2位的测量模式模式
    int HeightMode = MeasureSpec.getMode(heightMeasureSpec);

    int widthsize = MeasureSpec.getSize(widthMeasureSpec);   //30位。某种测量模式下的规格大小

    //获取模式后,if判断
    if (widthMode == MeasureSpec.AT_MOST){ //MeasureSpec.AT_MOST在布局中指定的是wrap_content,将获取它



    }
    else  if (widthMode == MeasureSpec.EXACTLY){//MeasureSpec.EXACTLY在布局中指定的是 确切的值 或者 match_parent,将获取它



    }
    else { //widthMode == MeasureSpec.UNSPECIFIED (尽可能的大),很少能用到. listView,scrollview,在测量子布局的时候会用。



    }
}

3,复写onDraw方法
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

  //  canvas.drawBitmap();
   //画文本
  //  canvas.drawCircle();

    //画文本
  //  canvas.drawText();

    //画弧
  //  canvas.drawArc();
}

4,onTouchEvent方法,处理和用户的交互
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){

            case MotionEvent.ACTION_UP:

               // 抬起
                break;


            case MotionEvent.ACTION_DOWN:

                //按下
                break;

            case MotionEvent.ACTION_MOVE:

                //移动
                break;



        }

        return super.onTouchEvent(event);  //交给父类处理
    }
}

5,自定义属性:
res/values/new--->ValuesResourceFile/attrs.xml
在attrs.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--name = 自定义view的名字-->

 <declare-styleable name="TextView">

     <!--name = 自定义属性的名称(任意) format 格式-->
     <attr name="text" format="string"/>

     <attr name="textColor" format="color"/>

     <!--dimension:尺寸; 用于 宽高,字体大小....-->

     <attr name="textSize" format="dimension"/>
     
     <attr name="maxLength" format="integer"/>

     <!--reference:资源(drawable-->

     <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>

</resources>
2)java中,具体实现。
获取属性:
1>先修改这几个构造函数:
public TextView(Context context) {
    this(context,null);
}
public TextView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,0);
}
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}
使得每次都调用第三个构造函数.
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
       //获取自定义的属性
      TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TextView);
      mText = array.getString(R.styleable.TextView_text);
      mColor = array.getColor(R.styleable.TextView_textColor,mColor);
      mTextSize = array.getInteger(R.styleable.TextView_textSize,mTextSize);
     //回收释放
    array.recycle();
    }
2>
 
3)布局中使用时先引入命名空间,但是一般都默认引用了。
使用:app:text = "hello world!"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值