垂直屏幕拆分过渡(动画) [英] Vertical Screen Split Transitions (Animation)

查看:22
本文介绍了垂直屏幕拆分过渡(动画)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动画应该做到以下几点:

The animation should do the following:

  1. 将屏幕垂直分成 2 部分.
  2. 上半部分向上移动.
  3. 下部向下移动.
  4. 最后,相反的方式.(关闭屏幕)
  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)

推荐答案

这个想法相当简单:

  1. 将您的活动 A 保存为位图
  2. 将位图分成两部分
  3. 向外动画位图(向上和向下)

为了得到Activity的位图:

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();

为了分割位图:

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;
}

创建位图后,应用动画

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.

参考链接:ActivitySplitAnimation

这篇关于垂直屏幕拆分过渡(动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆