Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

android - Vertical Screen Split Transitions (Animation)

The animation should do the following:

  1. Vertically split the screen into 2 parts.
  2. Upper part moving upward.
  3. Lower part moving down.
  4. Finally, the reverse way. (closing the screen)

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The idea is fairly simple:

  1. Save Your Activity A as a bitmap
  2. Split bitmap into 2 parts
  3. Animate the bitmaps outwards (Up and Down)

In order to get a bitmap of the Activity:

View root = currActivity.getWindow().getDecorView().findViewById(android.R.id.content);
root.setDrawingCacheEnabled(true);
Bitmap bmp = root.getDrawingCache();

In order to split the bitmap:

int splitYCoord = (splitYCoord != -1 ? splitYCoord : bmp.getHeight() / 2);
if (splitYCoord > bmp.getHeight())
            throw new IllegalArgumentException("Split Y coordinate [" + splitYCoord + "] exceeds the activity's height [" + bmp.getHeight() + "]");
Bitmap mBmp1 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), splitYCoord);
Bitmap mBmp2 = Bitmap.createBitmap(bmp, 0, splitYCoord, bmp.getWidth(), bmp.getHeight() - splitYCoord);
private static int[] mLoc1;
private static int[] mLoc2;
mLoc1 = new int[]{0, root.getTop()};
mLoc2 = new int[]{0, root.getTop() + splitYCoord};
private static ImageView mTopImage;
private static ImageView mBottomImage;
mTopImage = createImageView(destActivity, mBmp1, mLoc1);
mBottomImage = createImageView(destActivity, mBmp2, mLoc2);

private static ImageView createImageView(Activity destActivity, Bitmap bmp, int loc[]) {
    ImageView imageView = new ImageView(destActivity);
    imageView.setImageBitmap(bmp);

    WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
    windowParams.gravity = Gravity.TOP;
    windowParams.x = loc[0];
    windowParams.y = loc[1];
    windowParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    windowParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
    windowParams.flags =
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
    windowParams.format = PixelFormat.TRANSLUCENT;
    windowParams.windowAnimations = 0;
    destActivity.getWindowManager().addView(imageView, windowParams);

    return imageView;
}

After we created the bitmaps, Apply Animation

AnimatorSet 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 onAnimationEnd(Animator animation) {
                        clean(destActivity);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        clean(destActivity);
                    }
                        ...
                });

Animator anim1 = ObjectAnimator.ofFloat(mTopImage, "translationY", mTopImage.getHeight() * -1);
Animator anim2 = ObjectAnimator.ofFloat(mBottomImage, "translationY", mBottomImage.getHeight());

mSetAnim.setDuration(duration);
mSetAnim.playTogether(anim1, anim2);
mSetAnim.start();

private void clean(Activity activity) {
    if (mTopImage != null) {
        mTopImage.setLayerType(View.LAYER_TYPE_NONE, null);
        try {
            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) {}
    }

    mBmp1 = null;
    mBmp2 = null;
}

Above code is just for reference purpose. You can find full demo from below link.

Ref Link: ActivitySplitAnimation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...