android开发自定义滑动按钮

1,首先下年一个类CustomButton继承自View


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class CustomButotn extends View implements View.OnClickListener {

    private Bitmap backgroundImage;
    private Bitmap topImage;
    private int slideLeft;
    private Paint paint;
    private int slideMaxLeft;
    private float lastX;//原始值,只要一改变就表示滑动

    private boolean isOpening=true;
    private boolean isEnableClick=false;//true表示点击事件生效,否则滑动事件不生效

    public CustomButotn(Context context,AttributeSet attrs) {
        super(context, attrs);

        initView();
    }
    public void initView(){
        paint=new Paint();
        paint.setAntiAlias(true);
        backgroundImage=BitmapFactory.decodeResource(getResources(),R.drawable.bottom);
        topImage=BitmapFactory.decodeResource(getResources(),R.drawable.top);
        slideMaxLeft=backgroundImage.getWidth()-topImage.getWidth();

       /* 先把点击事件注释掉,接下来实现滑动
       this.setOnClickListener(this);*/
        this.setOnClickListener(this);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(backgroundImage.getWidth(),backgroundImage.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);这是空的方法可以注释掉

        canvas.drawBitmap(backgroundImage,0,0,paint);
        canvas.drawBitmap(topImage,slideLeft,0,paint);

    }

    //滑动事件

    private float startX;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastX=event.getX();
                startX=event.getX();
                isEnableClick=true;
                break;
            case MotionEvent.ACTION_MOVE:
                float endX=event.getX();
                float distanceX=endX-startX;
                slideLeft += distanceX;
                if (slideLeft>slideMaxLeft){
                    slideLeft=slideMaxLeft;
                }else if(slideLeft<0){
                    slideLeft=0;
                }
                invalidate();
                //还原数据
                startX=event.getX();

                if (Math.abs(endX-lastX)>5){
                    isEnableClick=false;//滑动事件生效
                }
                break;
            case MotionEvent.ACTION_UP:

                if (!isEnableClick){
                    if (slideLeft>slideMaxLeft/2){
                        isOpening=true;
                    }else {
                        isOpening=false;
                    }
                    if (isOpening){
                        slideLeft=slideMaxLeft;
                    }else {
                        slideLeft=0;
                    }
                    invalidate();//导致onDraw()方法执行
                }

                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        isOpening=!isOpening;
        if (isOpening){
            slideLeft=slideMaxLeft;
        }else {
            slideLeft=0;
        }
        invalidate();//导致onDraw()方法执行
    }
}

2,直接在布局文件中使用这个自定义View就可以了




    

 

你可能感兴趣的:(Android,实例小Demo)