自定义view和viewGroup。
这个就是约定的几个方法,onMeasure,onLayout,onDraw,measure,layout,draw
同时注意写方法的调用时机以及区别,比如onMeasure是个模版方法是在measure下调用的,这个方法在viewGroup里面,
自定义的这些就已经足够了,如果还扩展一些更新问题,子线程能不能更新,是可以的,在ViewRootImpl没创建之前可以更新,不同sdk这个类可能名字不一样。
1.自定义属性
2.onLayout(Viewgroup)
3.onMesure
4.onDraw
5.交互:onIntercepterTouchEvent()onTouchEvent()
对于自定义view 一直是云里雾里的。这两天有时间来写了2个demo。
1、带删除按钮的edittext,看下面代码
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
Drawable mClearDrawable;
public ClearEditText(Context context) {
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.editTextStyle);
}
public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mClearDrawable = getResources().getDrawable(R.drawable.clear_bg);
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicHeight(), mClearDrawable.getIntrinsicWidth());
setClearVisible(false);
setOnFocusChangeListener(this);
addTextChangedListener(this);
}
private void setClearVisible(boolean b) {
Drawable drawable = b ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], drawable, getCompoundDrawables()[3]);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
boolean isDelete = event.getX()>(getWidth()-getTotalPaddingRight())&&(event.getX()< (getWidth() - getPaddingRight()));
if (isDelete){
this.setText(" ");
}
}
return super.onTouchEvent(event);
}
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
setClearVisible(b);
} else {
setClearVisible(b);
}
}
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
setClearVisible(true);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
}
效果图如下:2、自定义的textview(圆形背景)
代码如下:
public class CircleTextView extends TextView {
private Paint mPaint ;
public CircleTextView(Context context) {
super(context);
}
public CircleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mPaint.setColor(Color.YELLOW);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);//抗锯齿
mPaint.setDither(true);//防抖动
}
public CircleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
//
canvas.drawCircle(getWidth()/2,getHeight()/2,Math.max(getWidth(),getHeight())/2,mPaint);
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measursWidth = getMeasuredWidth();
int measursHeight =getMeasuredHeight();
int max =Math.max(measursWidth,measursHeight);
setMeasuredDimension(max,max);
}
}
需要注意的是:onDraw();中父类的方法在最后调用,这样先绘制子类的,在绘制父类的。效果图如下:
总结,不管什么自定义的,多写多练明白原理很重要。