Ativity打开动画 切图效果

ActivitySplitAnimationUtil  

  1. package com.example.welcome;  
  2.   
  3. import android.animation.Animator;  
  4. import android.animation.AnimatorSet;  
  5. import android.animation.ObjectAnimator;  
  6. import android.animation.TimeInterpolator;  
  7. import android.annotation.SuppressLint;  
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.content.Intent;  
  11. import android.graphics.Bitmap;  
  12. import android.graphics.Canvas;  
  13. import android.graphics.Paint;  
  14. import android.graphics.PixelFormat;  
  15. import android.graphics.Rect;  
  16. import android.graphics.drawable.BitmapDrawable;  
  17. import android.graphics.drawable.Drawable;  
  18. import android.os.Handler;  
  19. import android.util.Log;  
  20. import android.view.Gravity;  
  21. import android.view.View;  
  22. import android.view.WindowManager;  
  23. import android.view.animation.DecelerateInterpolator;  
  24. import android.widget.ImageView;  
  25.   
  26. /** 
  27.  * Utility class to create a split activity animation 
  28.  * 
  29.  * @author Udi Cohen (@udinic) 
  30.  */  
  31. @SuppressLint("NewApi")  
  32. public class ActivitySplitAnimationUtil {  
  33.   
  34.     public static Bitmap mBitmap = null;  
  35.     private static int[] mLoc1;  
  36.     private static int[] mLoc2;  
  37.     private static ImageView mTopImage;  
  38.     private static ImageView mBottomImage;  
  39.     private static AnimatorSet mSetAnim;  
  40.   
  41.     /** 
  42.      * Start a new Activity with a Split animation 
  43.      * 
  44.      * @param currActivity The current Activity 
  45.      * @param intent       The Intent needed tot start the new Activity 
  46.      * @param splitYCoord  The Y coordinate where we want to split the Activity on the animation. -1 will split the Activity equally 
  47.      */  
  48.     public static void startActivity(Activity currActivity, Intent intent, int splitYCoord) {  
  49.   
  50.         // Preparing the bitmaps that we need to show  
  51.         prepare(currActivity, splitYCoord);  
  52.   
  53.         currActivity.startActivity(intent);  
  54.         currActivity.overridePendingTransition(00);  
  55.     }  
  56.   
  57.     /** 
  58.      * Start a new Activity with a Split animation right in the middle of the Activity 
  59.      * 
  60.      * @param currActivity The current Activity 
  61.      * @param intent       The Intent needed tot start the new Activity 
  62.      */  
  63.     public static void startActivity(Activity currActivity, Intent intent) {  
  64.         startActivity(currActivity, intent, -1);  
  65.     }  
  66.   
  67.     /** 
  68.      * Preparing the graphics on the destination Activity. 
  69.      * Should be called on the destination activity on Activity#onCreate() BEFORE setContentView() 
  70.      * 
  71.      * @param destActivity the destination Activity 
  72.      */  
  73.     public static void prepareAnimation(final Activity destActivity) {  
  74.         mTopImage = createImageView(destActivity, mBitmap, mLoc1);  
  75.         mBottomImage = createImageView(destActivity, mBitmap, mLoc2);  
  76.     }  
  77.   
  78.     /** 
  79.      * Start the animation the reveals the destination Activity 
  80.      * Should be called on the destination activity on Activity#onCreate() AFTER setContentView() 
  81.      * 
  82.      * @param destActivity the destination Activity 
  83.      * @param duration The duration of the animation 
  84.      * @param interpolator The interpulator to use for the animation. null for no interpulation. 
  85.      */  
  86.     public static void animate(final Activity destActivity, final int duration, final TimeInterpolator interpolator) {  
  87.   
  88.         // Post this on the UI thread's message queue. It's needed for the items to be already measured  
  89.         new Handler().post(new Runnable() {  
  90.   
  91.             @Override  
  92.             public void run() {  
  93.                 mSetAnim = new AnimatorSet();  
  94.                 mTopImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);  
  95.                 mBottomImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);  
  96.                 mSetAnim.addListener(new Animator.AnimatorListener() {  
  97.                     @Override  
  98.                     public void onAnimationStart(Animator animation) {  
  99.                     }  
  100.   
  101.                     @Override  
  102.                     public void onAnimationEnd(Animator animation) {  
  103.                         clean(destActivity);  
  104.                     }  
  105.   
  106.                     @Override  
  107.                     public void onAnimationCancel(Animator animation) {  
  108.                         clean(destActivity);  
  109.                     }  
  110.   
  111.                     @Override  
  112.                     public void onAnimationRepeat(Animator animation) {  
  113.   
  114.                     }  
  115.                 });  
  116.   
  117.                 // Animating the 2 parts away from each other  
  118.                 Animator anim1 = ObjectAnimator.ofFloat(mTopImage, "translationY", mTopImage.getHeight() * -1);  
  119.                 Animator anim2 = ObjectAnimator.ofFloat(mBottomImage, "translationY", mBottomImage.getHeight());  
  120.   
  121.                 if (interpolator != null) {  
  122.                     anim1.setInterpolator(interpolator);  
  123.                     anim2.setInterpolator(interpolator);  
  124.                 }  
  125.   
  126.                 mSetAnim.setDuration(duration);  
  127.                 mSetAnim.playTogether(anim1, anim2);  
  128.                 mSetAnim.start();  
  129.             }  
  130.         });  
  131.     }  
  132.   
  133.     /** 
  134.      * Start the animation that reveals the destination Activity 
  135.      * Should be called on the destination activity on Activity#onCreate() AFTER setContentView() 
  136.      * 
  137.      * @param destActivity the destination Activity 
  138.      * @param duration The duration of the animation 
  139.      */  
  140.     public static void animate(final Activity destActivity, final int duration) {  
  141.         animate(destActivity, duration, new DecelerateInterpolator());  
  142.     }  
  143.   
  144.     /** 
  145.      * Cancel an in progress animation 
  146.      */  
  147.     public static void cancel() {  
  148.         if (mSetAnim != null)  
  149.             mSetAnim.cancel();  
  150.     }  
  151.   
  152.     /** 
  153.      * Clean stuff 
  154.      * 
  155.      * @param activity The Activity where the animation is occurring 
  156.      */  
  157.     private static void clean(Activity activity) {  
  158.         if (mTopImage != null) {  
  159.             mTopImage.setLayerType(View.LAYER_TYPE_NONE, null);  
  160.             try {  
  161.                 // If we use the regular removeView() we'll get a small UI glitch  
  162.                 activity.getWindowManager().removeViewImmediate(mBottomImage);  
  163.             } catch (Exception ignored) {  
  164.             }  
  165.         }  
  166.         if (mBottomImage != null) {  
  167.             mBottomImage.setLayerType(View.LAYER_TYPE_NONE, null);  
  168.             try {  
  169.                 activity.getWindowManager().removeViewImmediate(mTopImage);  
  170.             } catch (Exception ignored) {  
  171.             }  
  172.         }  
  173.   
  174.         mBitmap = null;  
  175.     }  
  176.   
  177.     /** 
  178.      * Preparing the graphics for the animation 
  179.      * 
  180.      * @param currActivity the current Activity from where we start the new one 
  181.      * @param splitYCoord  The Y coordinate where we want to split the activity. -1 will split the activity equally 
  182.      */  
  183.     private static void prepare(Activity currActivity, int splitYCoord) {  
  184.   
  185.         // Get the content of the activity and put in a bitmap  
  186.         View root = currActivity.getWindow().getDecorView().findViewById(android.R.id.content);  
  187.         root.setDrawingCacheEnabled(true);  
  188.         mBitmap = root.getDrawingCache();  
  189.   
  190.         // If the split Y coordinate is -1 - We'll split the activity equally  
  191.         splitYCoord = (splitYCoord != -1 ? splitYCoord : mBitmap.getHeight() / 2);  
  192.   
  193.         if (splitYCoord > mBitmap.getHeight())  
  194.             throw new IllegalArgumentException("Split Y coordinate [" + splitYCoord + "] exceeds the activity's height [" + mBitmap.getHeight() + "]");  
  195.   
  196.         // Set the location to put the 2 bitmaps on the destination activity  
  197.         Log.i("prepare", root.getTop()+"");  
  198.         Log.i("prepare", splitYCoord+"");  
  199.         mLoc1 = new int[]{0, splitYCoord, root.getTop()};  
  200.         mLoc2 = new int[]{splitYCoord, mBitmap.getHeight(), root.getTop()};  
  201.     }  
  202.   
  203.     /** 
  204.      * Creating the an image, containing one part of the animation on the destination activity 
  205.      * 
  206.      * @param destActivity The destination activity 
  207.      * @param bmp          The Bitmap of the part we want to add to the destination activity 
  208.      * @param loc          The location this part should be on the screen 
  209.      * @return 
  210.      */  
  211.     private static ImageView createImageView(Activity destActivity, Bitmap bmp, int loc[]) {  
  212.         MyImageView imageView = new MyImageView(destActivity);  
  213.         imageView.setImageBitmap(bmp);  
  214.         imageView.setImageOffsets(bmp.getWidth(), loc[0], loc[1]);                       
  215.           
  216.         WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();  
  217.         windowParams.gravity = Gravity.TOP;  
  218.         windowParams.x = 0;  
  219.         windowParams.y = loc[2] + loc[0];  
  220.         windowParams.height = loc[1] - loc[0];  
  221.         windowParams.width = bmp.getWidth();  
  222.         windowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;  
  223.         windowParams.format = PixelFormat.TRANSLUCENT;  
  224.         windowParams.windowAnimations = 0;  
  225.         destActivity.getWindowManager().addView(imageView, windowParams);  
  226.   
  227.         return imageView;  
  228.     }  
  229.       
  230.     /** 
  231.      * MyImageView 
  232.      * Extended ImageView that draws just part of an image, base on start/end position   
  233.      */  
  234.     private static class MyImageView extends ImageView  
  235.     {  
  236.         private Rect mSrcRect;  
  237.         private Rect mDstRect;  
  238.         private Paint mPaint;         
  239.           
  240.         public MyImageView(Context context)   
  241.         {  
  242.             super(context);  
  243.             mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  244.         }  
  245.           
  246.         /** 
  247.          * Setting the bitmap offests to control the visible area 
  248.          * 
  249.          * @param width        The bitmap image 
  250.          * @param bmp          The start Y position 
  251.          * @param loc          The end Y position 
  252.          * @return 
  253.          */  
  254.         public void setImageOffsets(int width, int startY, int endY)  
  255.         {  
  256.             mSrcRect = new Rect(0, startY, width, endY);  
  257.             mDstRect = new Rect(00, width, endY - startY);  
  258.         }  
  259.                   
  260.         @Override  
  261.         protected void onDraw(Canvas canvas)  
  262.         {  
  263.             Bitmap bm = null;  
  264.             Drawable drawable = getDrawable();  
  265.             if (null != drawable && drawable instanceof BitmapDrawable)  
  266.             {  
  267.                 bm = ((BitmapDrawable)drawable).getBitmap();  
  268.             }  
  269.               
  270.             if (null == bm)  
  271.             {  
  272.                 super.onDraw(canvas);  
  273.             }  
  274.             else  
  275.             {  
  276.                 canvas.drawBitmap(bm, mSrcRect, mDstRect, mPaint);  
  277.             }  
  278.         }         
  279.     }  
  280. }  

转 自http://blog.csdn.net/silk2018/article/details/39375029

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值