Android自定义控件----继承View实现开关的滑动

本篇文章是对https://blog.csdn.net/zhaihaohao1/article/details/78286464
的补充,实现按钮的滑动开关
效果图:这里写图片描述
自定义控件MyToggleButton中的实现代码:

package com.zhh.mybutton;

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;
/**
 * 自定义左右滑动的按钮
 */
public class MyToggleButton extends View {
//  背景图片
    private Bitmap backgroundBitmap;
//  按钮
    private Bitmap slidingBitmap;
//  画笔对象
    private Paint paint;
//  靠左边的距离
    private int slideLeft;
//  靠左边的最大距离
    private int slidLeftMax;
    //  是否打开
    private boolean isOpen = false;
    //  记录按下的坐标
    private float startX;

    /**
     * 构造方法
     * @param context
     * @param attrs
     */
    public MyToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    /**
     * 初始化对象
     */
    private void initView(){
        paint=new Paint();
        paint.setAntiAlias(true);
        backgroundBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);
        slidingBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.slide_button);
        //      距左边的距离
        slidLeftMax = backgroundBitmap.getWidth() - slidingBitmap.getWidth();

    }

    /**
     * 测量
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
    }

    /**
     * 绘制
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(backgroundBitmap, 0, 0, paint);
        //      上面的滑动按钮
        canvas.drawBitmap(slidingBitmap, slideLeft, 0, paint);
    }

    /**
     * 设置靠左边的距离
     */
    private void flushView() {
        if(isOpen){
            slideLeft = slidLeftMax;
        }else{
            slideLeft = 0;
        }
        //会导致onDraw()执行,重新绘制图像
        invalidate();
    }

    /**
     * 触摸事件
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case  MotionEvent.ACTION_DOWN:
                startX = (int) event.getX();
                break;
            case  MotionEvent.ACTION_MOVE:
//              记录移动后的坐标
                float endX= event.getX();
                float distanceX = endX-startX;
                slideLeft+=distanceX;
                if(slideLeft <0){
                    slideLeft = 0;
                }else if(slideLeft>slidLeftMax){
                    slideLeft = slidLeftMax;
                }
                //5.刷新,就只重新绘制
                invalidate();
                //6.数据还原(执行了很多周期的初始值向223.11,224.22......)
                startX = (int) event.getX();
                break;
            case  MotionEvent.ACTION_UP:
//              判断是滑动事件时执行
                    if (slideLeft > slidLeftMax / 2) {
                        //显示按钮开
                        isOpen = true;
                    } else {
                        isOpen = false;
                    }
                    flushView();

                break;
        }
        return true;
    }



}

activity_main.xml中使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.zhh.mybutton.MyToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </com.zhh.mybutton.MyToggleButton>


</LinearLayout>

猜你喜欢

转载自blog.csdn.net/zhaihaohao1/article/details/80772866