自定义控件重要的两个知识点:1.View的绘制流程 2.事件分发
View的绘制流程:
首先分清是ViewGroup还是View
View (没有孩子):
measure(onMeasure)----draw(onDraw)(其中会先调用onMeasure方法,然后间接调用measure(),其中括号中的方法类似)
ViewGroup(有孩子):
measure(onMeasure)(设置自己大小,给孩子测量)-----layout(onLayout给孩子进行布局)---->draw(但是很少会draw)
首先来看看案例的效果图
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.b.MainActivity">
<com.example.administrator.b.view.SlidingButton
android:id="@+id/slidingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:sb_check="true"
app:sb_bg="@drawable/slide_switch_background"
app:sb_btn="@drawable/slide_button_background"
android:layout_centerInParent="true"/>
</RelativeLayout>
讲背景开关图片和按钮图片在界面上显示
设置大小并调用onDrawer()方法讲其画出来显示
//给自己设置大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(mSbBg.getWidth(),mSbBg.getHeight());
}
//设置布局:长得怎么样
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mSbBg,0,0,new Paint());
// mLeftOffset=20;
canvas.drawBitmap(mSbBtn,mLeftOffset,0,new Paint());
}
对外提供接口
public OnSwitchListener mOnSwitchListener;
public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
this.mOnSwitchListener = onSwitchListener;
}
public interface OnSwitchListener {
void onSwitch(boolean swich);
}
触摸事件使开关可以滑动,设置左右边界和超过一半的处理,和一些细节的处理
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mDownX = event.getX();
break;
case MotionEvent.ACTION_MOVE:
mMoveX = event.getX();
int dx = (int) (mMoveX - mDownX+0.5f);
mLeftOffset+=dx;
// 处理左右边界
if (mLeftOffset<0){
mLeftOffset=0;
}else if (mLeftOffset>mMaxOffset){
mLeftOffset=mMaxOffset;
}
invalidate();//会去调用onDraw();只能在ui线程中调用,如果非要在非ui线程中调用,就直接调用postInvalidate()
mDownX=mMoveX;
break;
case MotionEvent.ACTION_UP:
boolean isPathHalf=mLeftOffset>mMaxOffset/2;
if (isPathHalf){
mLeftOffset=mMaxOffset;
if (mOnSwitchListener!=null&&mCheck!=isPathHalf){
mOnSwitchListener.onSwitch(true);
mCheck=true;
}
}else {
mLeftOffset=0;
if (mOnSwitchListener!=null&&mCheck!=isPathHalf){
mOnSwitchListener.onSwitch(false);
mCheck=false;
}
}
invalidate();
break;
}
return true;//此处涉及到事件分发的知识点,只有返回true,执行完ACTION_DOWN后,ACTION_MOVE与ACTION_UP才能执行,否则这两个将不会执行到
}
public class MainActivity extends AppCompatActivity {
private SlidingButton mSb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSb = (SlidingButton) findViewById(R.id.slidingButton);
mSb.setOnSwitchListener(new SlidingButton.OnSwitchListener() {
@Override
public void onSwitch(boolean swich) {
if (swich){
Toast.makeText(MainActivity.this, "open", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "close", Toast.LENGTH_SHORT).show();
}
}
});
}
}