Created
July 14, 2017 03:46
-
-
Save suweya/7b6e5220abc5f02b6a2c8e5552fb7bc6 to your computer and use it in GitHub Desktop.
Lock screen slide view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.animation.Animator; | |
import android.animation.AnimatorListenerAdapter; | |
import android.animation.ObjectAnimator; | |
import android.animation.ValueAnimator; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.os.Build; | |
import android.support.annotation.Nullable; | |
import android.support.v4.view.MotionEventCompat; | |
import android.util.AttributeSet; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public class SlideView extends View { | |
public interface IOnFinishListener { | |
void finish(); | |
} | |
private float mMotionDownX, mMotionDownY; | |
private IOnFinishListener mFinishListener; | |
private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() { | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
@Override | |
public boolean onDown(MotionEvent e) { | |
mMotionDownX = e.getRawX() - getTranslationX(); | |
mMotionDownY = e.getRawY() - getTranslationY(); | |
return true; | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
@Override | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
float duration = e2.getRawX() - mMotionDownX; | |
duration = duration < 0 ? 0 : duration; | |
setTranslationX(duration); | |
//setTranslationY(e2.getRawY() - mMotionDownY); | |
if (getBackground() != null) { | |
getBackground().setAlpha((int) ((mScreenWidth - getTranslationX()) / mScreenWidth * 200)); | |
} | |
return true; | |
} | |
}; | |
private GestureDetector mGestureDetector; | |
private int mScreenWidth; | |
public SlideView(Context context) { | |
this(context, null); | |
} | |
public SlideView(Context context, @Nullable AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public SlideView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
private void init() { | |
mGestureDetector = new GestureDetector(getContext(), mGestureListener); | |
mScreenWidth = getResources().getDisplayMetrics().widthPixels; | |
if (getBackground() != null) { | |
getBackground().setAlpha((int) ((mScreenWidth - getTranslationX()) / mScreenWidth * 200)); | |
} | |
} | |
public void setmFinishListener(IOnFinishListener mFinishListener) { | |
this.mFinishListener = mFinishListener; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
final int action = MotionEventCompat.getActionMasked(event); | |
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { | |
doTriggerEvent(event.getRawX()); | |
return true; | |
} | |
return mGestureDetector.onTouchEvent(event); | |
} | |
private void doTriggerEvent(float x) { | |
float movex = x - mMotionDownX; | |
if (movex > (mScreenWidth * 0.4)) { | |
moveMoveView(mScreenWidth - getLeft(), true); | |
} else { | |
moveMoveView(-getLeft(), false); | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
private void moveMoveView(float to, boolean finish) { | |
ObjectAnimator animator = ObjectAnimator.ofFloat(this, "translationX", to); | |
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator animation) { | |
if (getBackground() != null) { | |
getBackground().setAlpha((int) ((mScreenWidth - getTranslationX()) / mScreenWidth * 200)); | |
} | |
} | |
}); | |
if (finish) { | |
animator.addListener(new AnimatorListenerAdapter() { | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
super.onAnimationEnd(animation); | |
if (mFinishListener != null) { | |
mFinishListener.finish(); | |
} | |
} | |
}); | |
} | |
animator.setDuration(250).start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment