2013-04-20 49 views
2

動畫應該做到以下幾點:垂直屏裂轉換(動畫)

  1. 垂直分割屏幕分爲兩個部分。
  2. 上部向上移動。
  3. 下半部分向下移動。
  4. 最後,逆向。 (關閉屏幕)

enter image description here

回答

9

的想法很簡單:

  1. 保存活動爲您提供爲位圖
  2. 分割位分爲兩個部分
  3. 動畫位圖向外(上下)

爲了讓活動的位圖:

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

上面的代碼只是爲參考目的。你可以從下面的鏈接找到完整的演示。

參考鏈接:ActivitySplitAnimation

+0

+1哇!太棒了!非常感謝! :)我現在就試試! – Roylee 2013-04-20 04:31:37

+0

那麼它支持API 8? – Roylee 2013-04-20 07:08:11

+1

@Roylee實際上,我們需要編寫我們自己的代碼,以便在代碼中使用上層android api。 – 2013-04-20 09:11:09