Android代码实现删除抛掷动画

jopen 10年前

下面讲讲是如何通过代码实现删除抛掷动画的。

         如下图所示就是所说的删除抛掷功能,对视图A启动该动画效果,动画完成后视图A到达B地,在这个过程中既有视图大小的变化,又有视图位置的变化:

  Android代码实现删除抛掷动画

         要实现这个动画效果,需要将位置变换动画类TranslateAnimation和大小变换动画类ScaleAnimation结合使用,具体的代码是这样子的:

     * 功能描述: 视图缩小至图片大小的动画        *         * @param fromX 源坐标        * @param fromY        * @param toX 目的坐标        * @param toY        * @param xRate 横纵向缩放比例        * @param yRate        * @param duration 动画时长        * @return 动画        */        private Animation sacleInView( int fromX, int toX, int fromY, int toY, float xRate,                float rate, float xTypeValue, float yTypeValue, int duration ){            AnimationSet set = new AnimationSet( true );            TranslateAnimation go = new TranslateAnimation( fromX, toX, fromY, toY );            go.setDuration( duration );                ScaleAnimation scale =                    new ScaleAnimation( 1.0f, xRate, 1.0f, rate, xTypeValue, yTypeValue );            scale.setDuration( duration );                set.addAnimation( go );            set.addAnimation( scale );                return set;        }            /**        * 功能描述: 图片放大至视图大小的动画        *         * @param rromX 源坐标        * @param fromY        * @param toX 目的坐标        * @param toY        * @param xRate 横纵向缩放比例        * @param yRate        * @param duration 动画时长        * @return 动画        */        private Animation sacleOutView( int fromX, int toX, int fromY, int toY, float xRate,                float yRate, float xTypeValue, float yTypeValue, int duration ){            AnimationSet set = new AnimationSet( true );            TranslateAnimation go = new TranslateAnimation( fromX, toX, fromY, toY );            go.setDuration( duration );                ScaleAnimation scale = new ScaleAnimation( xRate, 1.0f, yRate, 1.0f, xTypeValue, yTypeValue );            scale.setDuration( duration );                set.addAnimation( go );            set.addAnimation( scale );                return set;    }  

简单的使用例子是这样的:

public void startAnimation( ){            int[ ] fromLocation = new int[ 2 ];            view1.getLocationInWindow( fromLocation );            int[ ] toLocation = new int[ 2 ];            view2.getLocationInWindow( toLocation );                Animation scaleOutAnim = sacleOutView(            fromLocation[ 0 ] - toLocation[ 0 ], 0,            fromLocation[ 1 ] - toLocation[ 1 ], 0,            ( float )( ( 1.0 * view1.getWidth( ) ) / view2.getWidth( ) ),            ( float )( ( 1.0 * view1.getHeight( ) ) / view2.getHeight( ) ),            ( float )( fromLocation[ 0 ] - toLocation[ 0 ] ),            ( float )( fromLocation[ 1 ] - toLocation[ 1 ] ),            1000 );            view.startAnimation( scaleOutAnim );        }  
来自:http://blog.csdn.net/ekeuy/article/details/12163773