1. 鲜果网切换activity的效果就是滑动当前activity时,当前activity随手指滑动,而且上一个activity要在下面漏出来,当手指离开屏幕时,activity根据移动的位置关闭当前activity或者移动回原位。
2. 首先上一个activity作为当前activity的背景的实现是获取上一个activity的decorview,也就是根view,然后截图,把这个截图给当前activity作为背景。
// 获取根布局
final ViewGroup rootView = (ViewGroup) MainActivity.this
.getWindow().getDecorView();
rootView.setDrawingCacheEnabled(true);
Constants.bitmap = rootView.getDrawingCache();
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity. class);
startActivity(intent);
3. 当前activity中,布局如下:
<qingfengmy.testdemo.RootView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootview"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<RelativeLayout
android:id= "@+id/layout"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:background= "#ff00ffff" >
<TextView
android:id= "@+id/tv"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:background= "#ff0000"
android:text= "@string/hello_world"
android:textColor= "#ffffff" />
</RelativeLayout >
</qingfengmy.testdemo.RootView>
4. rootview设置背景为上一个activity的截图。
5. rootview加touch监听
rootView.setOnTouchListener( new OnTouchListener() {
private int xx;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent. ACTION_DOWN:
startx = ( int) event.getX();
starty = ( int) event.getY();
beginx = startx;
break;
case MotionEvent. ACTION_MOVE:
int movex = ( int) event.getX();
int movey = ( int) event.getY();
xx = movex - startx;
int xxx = movex - beginx;
if (xxx > 0 && xxx < windowWidth) {
Log. i("zt" , xxx + "=xxx;beginx=" + beginx);
Log. i("zt" , xx + "=xx;startx=" + startx );
rootView.scrollBy(- xx, 0);
}
startx = movex;
starty = movey;
break;
case MotionEvent. ACTION_UP:
// 算activity左边距在屏幕的位置
xx = ( int) (event.getX() - beginx);
Log. i("zt" , xx + ".windowWidth=" + windowWidth / 2);
if ( xx > windowWidth / 2) {
finish();
} else {
int xxxx = rootView.getScrollX();
Log. i("zt" , xxxx + "=xxxx," );
rootView.beginScroll(xxxx);
}
break;
default:
break;
}
return true;
}
});
6. rootview在up的时候,滚动回原位或者finish当前页面。其中滚动回原位的代码:
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.computeScrollOffset()) {
scrollTo( mScroller.getCurrX(), 0);
postInvalidate();
}
}
public void beginScroll(int startX) {
if (!s1) {
mScroller.startScroll(startX, 0, -startX, 0, Math.abs(startX)/6*15);
s1 = true;
} else {
mScroller.startScroll(startX, 0, -startX, 0, Math.abs(startX)/6*15);
s1 = false;
}
invalidate();
}