package com.test.basd.smartjunkcleaner.activity.photocompress.photo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;



public class SlideView extends View {

    private int mLastX;
    private int mMoveX;

    public SlideView(Context context) {
        super(context);
    }

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

    public SlideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastX = (int) event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                int x = (int) event.getRawX();
                int dx = x - mLastX;
                mMoveX += dx;
                mLastX = x;
                layout(getLeft() + dx, getTop(), getRight() + dx, getBottom());
                break;
            case MotionEvent.ACTION_UP:
                if (mMoveX < 0) {
                    animate().translationX(-mMoveX).setDuration(300).start();
                } else {
                    animate().translationX(0).setDuration(300).start();
                }
                mMoveX = 0;
                break;
        }
        return true;
    }
}