三个小李子讲述安卓动画用法

这里通过三个小李子来讲述一下,Android中通过xml获取三种动画的方法。

属性动画:从api 11引入

<!--animator/alpha_animator.xml-->
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:propertyName="alpha"
    android:valueFrom="0.0f"
    android:valueTo="1.0f"></objectAnimator>
  ImageView imageView = new ImageView(this);
        setContentView(imageView);
        imageView.setImageResource(R.drawable.icon_0);
        ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.alpha_animator);
        objectAnimator.setTarget(imageView);
        objectAnimator.start();

视图动画:常用alpha/scale/translate/rotate

<!--anim/test.xml-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="500"
        android:toDegrees="360" />
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:startOffset="500"
        android:toAlpha="0.0"></alpha>
    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="500"
        android:toXScale="0"
        android:toYScale="0"></scale>
</set>
  ImageView imageView = new ImageView(this);
        setContentView(imageView);

        imageView.setImageResource(R.drawable.icon_0);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.test);
        imageView.setAnimation(animation);
        animation.start();

帧动画:几张图片的有规律出现

<?xml version="1.0" encoding="utf-8"?>
<!--drawable/test.xml-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/icon_0"
        android:duration="500"></item>
    <item
        android:drawable="@drawable/icon_bg_release_topic_a"
        android:duration="500"></item>
    <item
        android:drawable="@drawable/icon_1"
        android:duration="1000"></item>

</animation-list>
  ImageView imageView = new ImageView(this);
        setContentView(imageView);
        imageView.setBackgroundResource(R.drawable.test);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值