Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created May 8, 2015 15:11
Show Gist options
  • Save xxnjdlys/b61cd0c7d6743c62ec8f to your computer and use it in GitHub Desktop.
Save xxnjdlys/b61cd0c7d6743c62ec8f to your computer and use it in GitHub Desktop.
package com.wukongtv.wkremote.client.widget;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;
/**
* Created by sadieyu
* Date: 15-1-20.
*/
public class SwipeBackLayout extends FrameLayout {
private View mContentView;
private int mTouchSlop;
private int mDownX;
private int mDownY;
private int mTempX;
/**
*see {{@link Scroller} }
*/
private Scroller mScroller;
private int mViewWidth;
private boolean mISilding;
private boolean mIsFinish;
private Activity mActivity;
public SwipeBackLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SwipeBackLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mScroller = new Scroller(context);
}
public void attachToActivity(Activity activity) {
mActivity = activity;
//get the background color (or Drawable) from the current theme
TypedArray typedArray = activity.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.windowBackground });
int background = typedArray.getResourceId(0, 0);
typedArray.recycle();
/**
* {@link android.view.Window#getDecorView()}
*
* Retrieve the top-level window decor view (containing the standard
* window frame/decorations and the client's content inside of that), which
* can be added as a window to the window manager.
*/
ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
decorChild.setBackgroundResource(background);
decor.removeView(decorChild);
addView(decorChild);
setContentView(decorChild);
decor.addView(this);
}
private void setContentView(View decorChild) {
mContentView = (View) decorChild.getParent();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = mTempX = (int) ev.getRawX();
mDownY = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) ev.getRawX();
if (moveX - mDownX > mTouchSlop
&& Math.abs((int) ev.getRawY() - mDownY) < mTouchSlop) {
return true;
}
break;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getRawX();
int deltaX = mTempX - moveX;
mTempX = moveX;
if (moveX - mDownX > mTouchSlop
&& Math.abs((int) event.getRawY() - mDownY) < mTouchSlop) {
mISilding = true;
}
if (moveX - mDownX >= 0 && mISilding) {
mContentView.scrollBy(deltaX, 0);
}
break;
case MotionEvent.ACTION_UP:
mISilding = false;
if (mContentView.getScrollX() <= -mViewWidth / 2) {
mIsFinish = true;
scrollRight();
} else {
scrollOrigin();
mIsFinish = false;
}
break;
}
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
mViewWidth = this.getWidth();
}
}
private void scrollRight() {
final int delta = (mViewWidth + mContentView.getScrollX());
mScroller.startScroll(mContentView.getScrollX(), 0, -delta, 0,
Math.abs(delta));
postInvalidate();
}
private void scrollOrigin() {
int delta = mContentView.getScrollX();
mScroller.startScroll(mContentView.getScrollX(), 0, -delta, 0,
Math.abs(delta));
postInvalidate();
}
/**
* Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.
* This will typically be done if the child is animating a scroll using a Scroller object.
* so {@link View#computeScroll} can be invoked when
* {@link android.view.View.OnTouchListener},
* {@link android.view.View#invalidate()},
* {@link android.view.View#postInvalidate()}
*/
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
mContentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
if (mScroller.isFinished() && mIsFinish) {
mActivity.finish();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment