DevBytes: View Animations

简介:

本文讲解了,怎样使用pre-3.0 API 创建多种多样的动画的效果,关于Property Animations 请参考上一讲的内容 (DevBytes: Property Animations)。

https://www.youtube.com/watch?v=_UWXqFBF86U

public class ViewAnimations extends Activity {

    CheckBox mCheckBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_animations);

        mCheckBox = (CheckBox) findViewById(R.id.checkbox);
        final Button alphaButton = (Button) findViewById(R.id.alphaButton);
        final Button translateButton = (Button) findViewById(R.id.translateButton);
        final Button rotateButton = (Button) findViewById(R.id.rotateButton);
        final Button scaleButton = (Button) findViewById(R.id.scaleButton);
        final Button setButton = (Button) findViewById(R.id.setButton);

        // Fade the button out and back in
        final AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(1000);

        // Move the button over and then back
        final TranslateAnimation translateAnimation =
                new TranslateAnimation(Animation.ABSOLUTE, 0,
                Animation.RELATIVE_TO_PARENT, 1,
                Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 100);
        translateAnimation.setDuration(1000);

        // Spin the button around in a full circle
        final RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
        rotateAnimation.setDuration(1000);

        // Scale the button in X and Y.
        final ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
        scaleAnimation.setDuration(1000);

        // Run the animations above in sequence on the final button. Looks horrible.
        final AnimationSet setAnimation = new AnimationSet(true);
        setAnimation.addAnimation(alphaAnimation);
        setAnimation.addAnimation(translateAnimation);
        setAnimation.addAnimation(rotateAnimation);
        setAnimation.addAnimation(scaleAnimation);

        setupAnimation(alphaButton, alphaAnimation, R.anim.alpha_anim);
        setupAnimation(translateButton, translateAnimation, R.anim.translate_anim);
        setupAnimation(rotateButton, rotateAnimation, R.anim.rotate_anim);
        setupAnimation(scaleButton, scaleAnimation, R.anim.scale_anim);
        setupAnimation(setButton, setAnimation, R.anim.set_anim);

    }

    private void setupAnimation(View view, final Animation animation,
            final int animationID) {
        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // If the button is checked, load the animation from the given resource
                // id instead of using the passed-in animation paramter. See the xml files
                // for the details on those animations.
                v.startAnimation(mCheckBox.isChecked() ?
                        AnimationUtils.loadAnimation(ViewAnimations.this, animationID) :
                        animation);
            }
        });
    }
}

应用的截图和上一讲(DevBytes: Property Animations) 是一样的,只不过,在动画实现的方式上是不一样的。
具体API的使用的方法请参考API  DOC。

View Animation

作者:FireOfStar 转载:http://www.2cto.com/kf/201207/139783.html
你能够使用视图动画系统来执行View对象上的补间动画。补间动画是用诸如开始点、结束点、尺寸、旋转以及一些其他的动画特性来计算的动画。

补间动画能够在View对象的内容上执行一个简单的变换系列(位置、尺寸、旋转和透明度)。因此,如果有一个TextView对象,就能够移动、旋转、放大或缩小文本。如果该TextView对象有一个背景图片,那么这个背景图片会跟文本一起变换。animation包提供了补间动画中所使用的所有的类。

动画指令序列定义了补间动画,这些指令既可以用XML来定义,也可以用Android代码来定义。跟布局定义一样,推荐使用XML来定义动画,因为它更加可读、可重用、并且比应编码的动画更加可插拔。在下面的例子中,我们使用XML。(要学习更多的有关在应用程序代码中定义动画的知识,请阅读AnimationSet类和其他的Animation子类。)

动画指令定义了你想要的动画变换,以及动画发生的时机和动画的播放的时长。动画变换能够是顺序的或并发的,例如:有一个从左向右移动的TextView对象的内容,然后旋转180度,或者在文本移动的同时旋转。每种变换都需要一组参数来指定所要的变换(针对尺寸变换的开始尺寸和结束尺寸、针对旋转的开始角度和结束角度等等),以及一组共同的参数(例如,开始时间和持续时长)。如果要是让几种变换同时发生,就要给它们设置相同的开始时间;如果要让它们顺序播放,就要用开始时间加上前面动画变换的时长来计算下一个动画播放的开始时间。


动画XML文件要定义在你的Android工程的res/anim/目录中。这个文件必须要有一个单独的根元素:这个元素既可以是一个单独的<alpha>、<scal>、<translate>、<rotate>的插值元素,也可以是拥有这些元素(包括<set>元素)组合的<set>元素。默认情况下,所有的动画指令都是并发的。要让它们顺序的发生,就必须像下面的示例所示的那样,指定startOffset属性。


下面的XML来自于APIDemo中的一个用于拉伸,然后同时旋转的View对象:

<set android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
           android:fromXScale="1.4"
           android:toXScale="0.0"
           android:fromYScale="0.6"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400"
           android:fillBefore="false" />
        <rotate
           android:fromDegrees="0"
           android:toDegrees="-45"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400" />
    </set>
</set>

在左上角屏幕的坐标(在上面的这个例子中没有使用)是(0,0),并且向右下角逐渐增加。


有一些值,如pivotX,能够相对于对象自己或它的父容器来指定。对于想要的值必须使用正确的格式(50是指相对它的父容器的左上角的50%,50%则是指相对于它自己的左上角的50%)。


通过分配一个Interpolator对象,能够决定如何随着时间的推移来进行一个动画的变换。Android包括了几种Interpolator子类,它们能够指定各种速度的曲线,例如:AccelerateInterpolator会告诉系统执行一个开始慢,然后逐渐加速的变换。每种变换都会有一个属性值被应用于XML中。


保存在工程的res/anim/目录中的hyperspace_jump.xml文件,下列代码会引用这个文件,并把它应用于一个来自布局的ImageView对象。

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

作为startAnimation()方法的一个替代方法,你能够用Animation.setStartTime()方法来定义动画的开始时间,然后用View.setAnimation()方法把这个动画对象分配给View对象。

注意:不管你的动画如何移动或调整尺寸,拥有动画的View对象的边界都不会自动的调整来适应变化,即使动画超出了View对象的边界也不会被裁剪,但是如果动画超出了它的父容器的的边界,那么它将会被裁剪。


参考:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值