Custom Imitation IPhone Switch Control

Posted by robotta1530 on Tue, 08 Oct 2019 10:41:16 +0200

Strongly Recommend Articles: Welcome to Collection
Android dry goods sharing

Read for five minutes, ten o'clock a day, and study with you for life. Here's Android, a programmer.

This article mainly introduces some knowledge points in Android development. By reading this article, you will gain the following contents:

  1. Custom View Class Implementation
  2. Custom View Layout
  3. Custom View Material
  4. Activity uses custom View

Custom ItemToggleView is often used in Settings, which mainly controls the opening and closing of switches.

The effect of custom ItemToggleView implementation is as follows:

1. Implementation of custom View class

public class SwitchControlView extends View implements OnTouchListener {
    private Bitmap bg_on, bg_off, slipper_btn;
    private float downX, nowX;

    private boolean onSlip = false;

    private boolean nowStatus = false;

    private OnChangedListener listener;

    public SwitchControlView(Context context) {
        super(context);
        init();
    }

    public SwitchControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.on_btn);
        bg_off = BitmapFactory.decodeResource(getResources(),
                R.drawable.off_btn);
        slipper_btn = BitmapFactory.decodeResource(getResources(),
                R.drawable.white_btn);
        setOnTouchListener(this);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Matrix matrix = new Matrix();
        Paint paint = new Paint();
        float x = 0;
        if (bg_on != null && bg_off != null) {
            if (nowX < (bg_on.getWidth() / 2)) {
                canvas.drawBitmap(bg_off, matrix, paint);
            } else {
                canvas.drawBitmap(bg_on, matrix, paint);
            }
        }

        if (onSlip) {
            if (nowX >= bg_on.getWidth())
                x = bg_on.getWidth() - slipper_btn.getWidth() / 2;
            else
                x = nowX - slipper_btn.getWidth() / 2;
        } else {
            if (nowStatus) {
                x = bg_on.getWidth() - slipper_btn.getWidth();
            } else {
                x = 0;
            }
        }

        if (x < 0) {
            x = 0;
        } else if (x > bg_on.getWidth() - slipper_btn.getWidth()) {
            x = bg_on.getWidth() - slipper_btn.getWidth();
        }

        canvas.drawBitmap(slipper_btn, x, 0, paint);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            if (event.getX() > bg_off.getWidth()
                    || event.getY() > bg_off.getHeight()) {
                return false;
            } else {
                onSlip = true;
                downX = event.getX();
                nowX = downX;
            }
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            nowX = event.getX();
            break;
        }
        case MotionEvent.ACTION_UP: {
            onSlip = false;
            if (event.getX() >= (bg_on.getWidth() / 2)) {
                nowStatus = true;
                nowX = bg_on.getWidth() - slipper_btn.getWidth();
            } else {
                nowStatus = false;
                nowX = 0;
            }

            if (listener != null) {
                listener.OnChanged(SwitchControlView.this, nowStatus);
            }
            break;
        }
        }
        invalidate();
        return true;
    }

    public void setOnChangedListener(OnChangedListener listener) {
        this.listener = listener;
    }

    public void setChecked(boolean checked) {
        if (checked) {
            nowX = bg_off.getWidth();
        } else {
            nowX = 0;
        }
        nowStatus = checked;
    }

    public interface OnChangedListener {
        public void OnChanged(SwitchControlView wiperSwitch, boolean checkState);
    }

}

2. Custom View Layout

    <com.programandroid.CustomView.SwitchControlView
        android:id="@+id/switch_control_view"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical" />

3. Custom View Material


4. Activity uses custom View

        /**
     * InitSwitchView Implementation of custom sliding switch
     */
    private void InitSwitchView() {
        // TODO Auto-generated method stub
        SwitchControlView wiperSwitch = (SwitchControlView) findViewById(R.id.switch_control_view);
        wiperSwitch.setChecked(true);
        wiperSwitch.setOnChangedListener(new OnChangedListener() {

            @Override
            public void OnChanged(SwitchControlView wiperSwitch,
                    boolean checkState) {
                // TODO Auto-generated method stub

                if (checkState) {
                    Toast.makeText(CustomViewMethods.this, "open", 1).show();

                } else {
                    Toast.makeText(CustomViewMethods.this, "Close", 1).show();
                }
            }
        });
    }

So far, this article is over. If there are any mistakes, you are welcome to make suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

Topics: Android