Android代码实现删除抛掷动画

对于android动画的使用,我由衷于通过代码调用Animation相关类来实现,下面讲讲我是如何通过代码实现删除抛掷动画的。

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

 

         要实现这个动画效果,需要将位置变换动画类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 );
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值