com.audionote.widget.SlideSwitch.java Source code

Java tutorial

Introduction

Here is the source code for com.audionote.widget.SlideSwitch.java

Source

/*
 * Copyright (C) 2015 Quinn Chen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.audionote.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.audionote.R;
import com.audionote.util.ScreenUtils;

public class SlideSwitch extends View {

    public static final int SHAPE_RECT = 1;
    public static final int SHAPE_CIRCLE = 2;
    //    private static final int RIM_SIZE = 3;
    private static final int DEFAULT_COLOR_THEME = Color.parseColor("#ff4cd964");

    // 4 attributes
    private int border_color;
    private int color_theme;
    private int color_back_theme;
    private boolean isOpen;
    private int shape;

    // varials of drawing
    private Paint paint;
    private Rect backBorderRect;
    private Rect backRect;
    private Rect frontRect;
    private Rect frontBorderRect;
    private RectF frontCircleRect;
    private RectF frontBorderCircleRect;
    private RectF backCircleRect;
    private RectF backBorderCircleRect;
    private int alpha;
    private int max_left;
    private int min_left;
    private int frontRect_left;
    private int frontRect_left_begin;
    private int eventStartX;
    private int eventLastX;
    private int diffX = 0;
    private boolean slideable = true;
    private SlideListener listener;

    int borderWidth;

    public interface SlideListener {
        public void open();

        public void close();
    }

    public SlideSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        listener = null;
        paint = new Paint();
        paint.setAntiAlias(true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.slideswitch);
        color_theme = a.getColor(R.styleable.slideswitch_themeColor, DEFAULT_COLOR_THEME);
        color_back_theme = a.getColor(R.styleable.slideswitch_backColor, Color.WHITE);
        isOpen = true;
        shape = SHAPE_CIRCLE;
        a.recycle();

        borderWidth = ScreenUtils.getDipValue(context, 1);
        border_color = Color.parseColor("#ffcccccc");
    }

    public SlideSwitch(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // int width = measureDimension(100, widthMeasureSpec);
        // int height = measureDimension(70, heightMeasureSpec);
        // if (shape == SHAPE_CIRCLE) {
        // if (width < height)
        // width = height * 2;
        // }
        // setMeasuredDimension(width, height);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        initDrawingVal();
    }

    public void initDrawingVal() {
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        backCircleRect = new RectF();
        backBorderCircleRect = new RectF();
        frontCircleRect = new RectF();
        frontBorderCircleRect = new RectF();
        frontRect = new Rect();
        frontBorderRect = new Rect();
        backRect = new Rect(borderWidth, borderWidth, width - borderWidth, height - borderWidth);
        backBorderRect = new Rect(0, 0, width, height);
        min_left = 0;
        if (shape == SHAPE_RECT)
            max_left = width / 2;
        else
            max_left = width - (height - 2 * borderWidth) - 2 * borderWidth;
        if (isOpen) {
            frontRect_left = max_left;
            alpha = 255;
        } else {
            frontRect_left = 0;
            alpha = 0;
        }
        frontRect_left_begin = frontRect_left;
    }

    public int measureDimension(int defaultSize, int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = defaultSize; // UNSPECIFIED
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (shape == SHAPE_RECT) {
            paint.setColor(color_back_theme);
            canvas.drawRect(backRect, paint);
            paint.setColor(color_theme);
            paint.setAlpha(alpha);
            canvas.drawRect(backRect, paint);
            frontRect.set(frontRect_left, borderWidth, frontRect_left + backRect.height() - 2 * borderWidth,
                    backRect.height() - borderWidth);
            paint.setColor(Color.WHITE);
            canvas.drawRect(frontRect, paint);
        } else {
            // draw circle
            int backBorderRadius = backBorderRect.height() / 2;
            int backRadius = backBorderRadius - borderWidth;

            // 
            paint.setColor(border_color);
            backBorderCircleRect.set(backBorderRect);
            canvas.drawRoundRect(backBorderCircleRect, backBorderRadius, backBorderRadius, paint);

            // 
            paint.setColor(color_back_theme);
            backCircleRect.set(backRect);
            canvas.drawRoundRect(backCircleRect, backRadius, backRadius, paint);

            // ??
            paint.setColor(color_theme);
            paint.setAlpha(alpha);
            canvas.drawRoundRect(backBorderCircleRect, backBorderRadius, backBorderRadius, paint);

            // ?
            frontBorderRect.set(frontRect_left, 0, frontRect_left + backBorderRect.height(),
                    backBorderRect.height());
            frontBorderCircleRect.set(frontBorderRect);
            paint.setColor(border_color);
            canvas.drawRoundRect(frontBorderCircleRect, backBorderRadius, backBorderRadius, paint);

            // ?
            frontRect.set(frontRect_left + borderWidth, borderWidth,
                    frontRect_left + backBorderRect.height() - borderWidth, backBorderRect.height() - borderWidth);
            frontCircleRect.set(frontRect);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(frontCircleRect, backRadius, backRadius, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (slideable == false)
            return super.onTouchEvent(event);
        int action = MotionEventCompat.getActionMasked(event);
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            eventStartX = (int) event.getRawX();
            break;
        case MotionEvent.ACTION_MOVE:
            eventLastX = (int) event.getRawX();
            diffX = eventLastX - eventStartX;
            int tempX = diffX + frontRect_left_begin;
            tempX = (tempX > max_left ? max_left : tempX);
            tempX = (tempX < min_left ? min_left : tempX);
            if (tempX >= min_left && tempX <= max_left) {
                frontRect_left = tempX;
                alpha = (int) (255 * (float) tempX / (float) max_left);
                invalidateView();
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            int wholeX = (int) (event.getRawX() - eventStartX);
            frontRect_left_begin = frontRect_left;
            boolean toRight;
            toRight = (frontRect_left_begin > max_left / 2 ? true : false);
            if (Math.abs(wholeX) < 3) {
                toRight = !toRight;
            }
            moveToDest(toRight);
            break;
        default:
            Log.d("motion", action + "");
            break;
        }
        return true;
    }

    /**
     * draw again
     */
    private void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }

    public void setSlideListener(SlideListener listener) {
        this.listener = listener;
    }

    public void moveToDest(final boolean toRight) {
        ValueAnimator toDestAnim = ValueAnimator.ofInt(frontRect_left, toRight ? max_left : min_left);
        toDestAnim.setDuration(200);
        toDestAnim.setInterpolator(new AccelerateDecelerateInterpolator());
        toDestAnim.start();
        toDestAnim.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                frontRect_left = (Integer) animation.getAnimatedValue();
                alpha = (int) (255 * (float) frontRect_left / (float) max_left);
                invalidateView();
            }
        });
        toDestAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (toRight) {
                    isOpen = true;
                    if (listener != null)
                        listener.open();
                    frontRect_left_begin = max_left;
                } else {
                    isOpen = false;
                    if (listener != null)
                        listener.close();
                    frontRect_left_begin = min_left;
                }
            }
        });
    }

    public void setState(boolean isOpen) {
        this.isOpen = isOpen;
        initDrawingVal();
        invalidateView();
        if (listener != null)
            if (isOpen == true) {
                listener.open();
            } else {
                listener.close();
            }
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setShapeType(int shapeType) {
        this.shape = shapeType;
    }

    public void setSlideable(boolean slideable) {
        this.slideable = slideable;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            this.isOpen = bundle.getBoolean("isOpen");
            state = bundle.getParcelable("instanceState");
        }
        super.onRestoreInstanceState(state);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable("instanceState", super.onSaveInstanceState());
        bundle.putBoolean("isOpen", this.isOpen);
        return bundle;
    }
}