自定义view实现开关效果并使用接口回调

自定义view实现开关,并且使用接口回调



public class MyTaggButton extends View implements View.OnClickListener,View.OnTouchListener{

    private Paint paint;
    private Bitmap switch_background;
    private Bitmap silde_button;
    //记录当前按钮是开还是关
    boolean togglestate;
    //判断是否是点击或者滑动
    boolean isDrop;
    //用户滑动的开始位置
    private int firstX;
    //用户滑动的终点位置
    private int lastX;

    private int sildleft=0;
    private android.graphics.Canvas canvas;
    private int sildeleftMax;

    //定义一个接口对象
    private CallBacks callBacks;
    //获得接口对象的方法。
    public void CallBacks(CallBacks callBacks){
        this.callBacks=callBacks;
    }
    //定义一个接口
    public interface CallBacks{
        public void onCallBacks(boolean a);
    }

    public MyTaggButton(Context context) {
        this(context,null);
    }

    public MyTaggButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    private void initview() {
        //创建画笔实例
        paint = new Paint();
        //先得到图片\+
        switch_background = BitmapFactory.decodeResource(getResources(), R.drawable.beijing);
        silde_button =BitmapFactory.decodeResource(getResources(),R.drawable.ttu);
        sildeleftMax = switch_background.getWidth() - silde_button.getWidth();
        setOnClickListener(this);
        setOnTouchListener(this);
    }



    //定义控件的高和款
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       setMeasuredDimension(switch_background.getWidth(),switch_background.getHeight());
    }
    //绘制图片
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(switch_background,0,0,paint);
        canvas.drawBitmap(silde_button,sildleft,0,paint);


    }


    @Override
    public void onClick(View view) {
        //判断是点击还是滑动
        if(isDrop==true){
            if(togglestate){
                sildleft=0;
            }else{
                //如果是开,则到背景的最大宽度
                sildleft=sildeleftMax;
            }
            togglestate=!togglestate;
            callBacks.onCallBacks(togglestate);
        }else{

        }
        //重新绘制
        invalidate();
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
                //因为只是X轴的变化所以只得到X就可以
                firstX=lastX=(int) motionEvent.getX();
                isDrop=true;

                break;
            case MotionEvent.ACTION_MOVE:
                isDrop=false;
                //滑动的时候得到一个新的坐标
                int newX = (int) motionEvent.getX();
                //还要计算出本次移动时间和上次移动时间的距离
                int offx = newX -lastX;
                sildleft=sildleft+offx;
                //最后新的x坐标就成了最终的位置
                lastX=newX;
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        //重新绘制
        newhuizhi();
        return false;
    }

    private void newhuizhi() {
        //防止滑块滑出做出的判断
        if(sildleft<0){
            sildleft=0;
        }else if(sildleft>sildeleftMax){
            sildleft=sildeleftMax;
        }
        invalidate();
    }



}
XML文件中
<com.example.a11561.day03.MyTaggButton
    android:id="@+id/mybtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

MainActivity

package com.example.a11561.day03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private MyTaggButton.CallBacks callBacks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //对应类找到控件ID
        MyTaggButton myTaggButton = findViewById(R.id.mybtn);
        //实现接口
        myTaggButton.CallBacks(new MyTaggButton.CallBacks() {
            @Override
            public void onCallBacks(boolean a) {
                Toast.makeText(MainActivity.this,a+"啊啊", Toast.LENGTH_SHORT).show();
            }
        });


    }
}

你可能感兴趣的:(自定义view实现开关效果并使用接口回调)