现在给加大分享一下打开Activity时中心打开的效果。
思路:
1.在设置打开目标Aitivity的时,先获取当前Acitivty,设置分割的背景的坐标。
2.当目标Activity打开之前,根据分割的坐标,创建ImageView,上下两部分,并且添加到目标Activity中。
3.最后设置上下两部分IMageView的动画,是他们一起开启动画。
4.当动画结束后,要把目标Activity中的添加的两个ImageView移除掉。
下面通过代码来实现:
设置开启目标Activity
public static void startActivity(Activity currActivity, Intent intent,
int splitYCoord) {
// 准备一个BItMap图片,是当前的Activity界面作为的背景
prepare(currActivity, splitYCoord);
currActivity.startActivity(intent);
currActivity.overridePendingTransition(0, 0);
}
currActivity:当前的Activity
intent:开启新的Activity的Intent
splitYCoord:想去分割Activity的纵坐标,-1表示平等的从中间分割
在这个方法里,最主要的功能获取当前Acitivity界面作为一个Bitmap
private static void prepare(Activity currActivity, int splitYCoord) {
// 获取当前的Activity,作为一个BItMap
View root = currActivity.getWindow().getDecorView()
.findViewById(android.R.id.content);
root.setDrawingCacheEnabled(true);
mBitmap = root.getDrawingCache();
// If the split Y coordinate is -1 - We'll split the activity equally
splitYCoord = (splitYCoord != -1 ? splitYCoord
: mBitmap.getHeight() / 2);
if (splitYCoord > mBitmap.getHeight())
throw new IllegalArgumentException("Split Y coordinate ["
+ splitYCoord + "] exceeds the activity's height ["
+ mBitmap.getHeight() + "]");
// 设置给两个上下BitMap的坐标,是给目标Activity制作的动画的ImageView提供的坐标
mLoc1 = new int[] { 0, splitYCoord, root.getTop() };
mLoc2 = new int[] { splitYCoord, mBitmap.getHeight(), root.getTop() };
}
主要是获取当前的Activity的内容,生成一个BitMap,然后根据分割标记,把分割的上下两部分ImageView的坐标划分出来
在目标的Activity中oncreate()的Activity的onCreate()中的setContentView()前面调用准备动画
public static void prepareAnimation(final Activity destActivity) {
mTopImage = createImageView(destActivity, mBitmap, mLoc1);
mBottomImage = createImageView(destActivity, mBitmap, mLoc2);
}
创建分割的ImageView,并且添加到目标Activity的窗体上
private static ImageView createImageView(Activity destActivity, Bitmap bmp,
int loc[]) {
MyImageView imageView = new MyImageView(destActivity);
imageView.setImageBitmap(bmp);
imageView.setImageOffsets(bmp.getWidth(), loc[0], loc[1]);
WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.TOP;
windowParams.x = 0;
windowParams.y = loc[2] + loc[0];
windowParams.height = loc[1] - loc[0];
windowParams.width = bmp.getWidth();
windowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;
destActivity.getWindowManager().addView(imageView, windowParams);
return imageView;
}
private static class MyImageView extends ImageView {
private Rect mSrcRect;
private Rect mDstRect;
private Paint mPaint;
public MyImageView(Context context) {
super(context);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
/**
* Setting the bitmap offests to control the visible area
*
* @param width
* The bitmap image
* @param bmp
* The start Y position
* @param loc
* The end Y position
* @return
*/
public void setImageOffsets(int width, int startY, int endY) {
mSrcRect = new Rect(0, startY, width, endY);
mDstRect = new Rect(0, 0, width, endY - startY);
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bm = null;
Drawable drawable = getDrawable();
if (null != drawable && drawable instanceof BitmapDrawable) {
bm = ((BitmapDrawable) drawable).getBitmap();
}
if (null == bm) {
super.onDraw(canvas);
} else {
canvas.drawBitmap(bm, mSrcRect, mDstRect, mPaint);
}
}
}
开启动画
public static void animate(final Activity destActivity, final int duration,
final TimeInterpolator interpolator) {
// Post this on the UI thread's message queue. It's needed for the items
// to be already measured
new Handler().post(new Runnable() {
@Override
public void run() {
mSetAnim = new AnimatorSet();
mTopImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mBottomImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mSetAnim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
clean(destActivity);
}
@Override
public void onAnimationCancel(Animator animation) {
clean(destActivity);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
// Animating the 2 parts away from each other
Animator anim1 = ObjectAnimator.ofFloat(mTopImage,
"translationY", mTopImage.getHeight() * -1);
Animator anim2 = ObjectAnimator.ofFloat(mBottomImage,
"translationY", mBottomImage.getHeight());
if (interpolator != null) {
anim1.setInterpolator(interpolator);
anim2.setInterpolator(interpolator);
}
mSetAnim.setDuration(duration);
mSetAnim.playTogether(anim1, anim2);
mSetAnim.start();
}
});
}
当动画结束,移除目标Activity的ImageView。
private static void clean(Activity activity) {
if (mTopImage != null) {
mTopImage.setLayerType(View.LAYER_TYPE_NONE, null);
try {
// If we use the regular removeView() we'll get a small UI
// glitch
activity.getWindowManager().removeViewImmediate(mBottomImage);
} catch (Exception ignored) {
}
}
if (mBottomImage != null) {
mBottomImage.setLayerType(View.LAYER_TYPE_NONE, null);
try {
activity.getWindowManager().removeViewImmediate(mTopImage);
} catch (Exception ignored) {
}
}
mBitmap = null;
}
如果想给其他View设置中心打开效果,那么就按照这三步执行即可
使用步骤:
我这是创建了一个BaseActivity,把准备动画和开启动画放在了BaseAcitivity中
activity01.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yzk.centersplit.activity.Activity2" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/num1"
android:gravity="center"
android:text="我是美女一号"
android:textColor="@android:color/holo_blue_light"
android:textSize="30sp" />
</RelativeLayout>
activity02.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yzk.centersplit.activity.Activity2" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/num2"
android:gravity="center"
android:text="我是不是很萌"
android:textColor="@android:color/holo_green_dark"
android:textSize="30sp" />
</RelativeLayout>
activity03.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yzk.centersplit.activity.Activity2" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/bg3"
android:gravity="center"
android:text="风景是不是很美"
android:textColor="@android:color/holo_orange_dark"
android:textSize="30sp" />
</RelativeLayout>
activity04.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yzk.centersplit.activity.Activity2" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/bg4"
android:gravity="center"
android:text="想不想拥有一个豪车"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp" />
</RelativeLayout>
BaseActivity.java
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// 准备好动画,创建了上一个Activity的以中间为线
ActivitySplitAnimationUtil.prepareAnimation(this);
super.onCreate(savedInstanceState);
// 开启动画
ActivitySplitAnimationUtil.animate(this, 2000);
}
}
Activity1.java
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 此时就会把当前的activity作为中间分割动画的背景了
ActivitySplitAnimationUtil.startActivity(Activity1.this,
new Intent(Activity1.this, Activity2.class));
}
});
}
}
Activity2.java
public class Activity2 extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ActivitySplitAnimationUtil.startActivity(Activity2.this,
new Intent(Activity2.this, Activity3.class));
}
});
}
}
Activity3.java
public class Activity3 extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ActivitySplitAnimationUtil.startActivity(Activity3.this,
new Intent(Activity3.this, Activity4.class));
}
});
}
}
Activity4.java
public class Activity4 extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity04);
}
}
源码下载:http://download.csdn.net/detail/forwardyzk/8332101
如果效果图看不到,请使用360安全浏览器查看。不知道为什么在谷歌浏览器上gif图片显示不出来。
知道怎样解决的请给你建议。
效果图: