温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 中怎么实现补间动画

发布时间:2021-08-05 16:59:42 来源:亿速云 阅读:224 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关Android 中怎么实现补间动画,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

补间动画的优点是可以节省空间。补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧 之间补充渐变的动画效果来实现的。目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类 库中。

  • AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。

  • TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。

  • ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。

RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。

  • AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。

补间动画的效果同样可以使用XML语言来定义,这些动画模板文件通常会被放在Android项目的res/anim/目录下。

public class MainActivity extends Activity {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }

    public void click1(View v) {
        AlphaAnimation ani = new AlphaAnimation(0.0f, 1.0f);
        ani.setDuration(2000);
        ani.setRepeatCount(2);
        ani.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ani);
    }

    public void click11(View v) {
        Animation ani = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
        iv.startAnimation(ani);
    }

    public void click2(View v) {
        ScaleAnimation ani = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        ani.setDuration(2000);
        ani.setRepeatCount(2);
        ani.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ani);
    }

    public void click22(View v) {
        Animation ani = AnimationUtils.loadAnimation(this, R.anim.rotate_ani);
        iv.startAnimation(ani);
    }

    public void click3(View v) {
        RotateAnimation ani = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        ani.setDuration(2000);
        ani.setRepeatCount(2);
        ani.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ani);
    }

    public void click33(View v) {
        Animation ani = AnimationUtils.loadAnimation(this, R.anim.scale_ani);
        iv.startAnimation(ani);
    }

    public void click4(View v) {
        TranslateAnimation ani = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 1.0f);
        ani.setDuration(2000);
        ani.setRepeatCount(2);
        ani.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ani);
    }

    public void click44(View v) {
        Animation ani = AnimationUtils.loadAnimation(this, R.anim.translate);
        iv.startAnimation(ani);
    }

}

Android 中怎么实现补间动画

Animation的xml                                                                      

Android 中怎么实现补间动画

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromAlpha="1.0"
    android:toAlpha="0.5"
    android:fillAfter="true"
    android:duration="2000" >
</alpha>

Android 中怎么实现补间动画

Android 中怎么实现补间动画

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000" >

</rotate>

Android 中怎么实现补间动画

Android 中怎么实现补间动画

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.2"
    android:toXScale="2.0"
    android:fromYScale="0.2"
    android:toYScale="2.0"
    android:fillAfter="true"
    android:duration="2000" >

</scale>

Android 中怎么实现补间动画

Android 中怎么实现补间动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="20%p"
    android:toXDelta="50%p"
    android:fromYDelta="0"
    android:toYDelta="50%p"
    android:duration="2000"
    android:repeatCount="2"
    android:repeatMode="reverse" >

</translate>

Android 中怎么实现补间动画

代码解析                                                                                   

  • alpha

fromAlpha :起始透明度

toAlpha:结束透明度

1.0表示完全不透明

0.0表示完全透明

  • rotate

fromDegrees:表示旋转的起始角度

toDegrees:表示旋转的结束角度

repeatCount:旋转的次数  默认值是0 代表旋转1次  如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止

repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。

repeatCount=-1 或者infinite循环了  还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。

  • scale

fromXScale:表示沿着x轴缩放的起始比例

toXScale:表示沿着x轴缩放的结束比例

fromYScale:表示沿着y轴缩放的起始比例

toYScale:表示沿着y轴缩放的结束比例

图片中心点:

android:pivotX="50%"
android:pivotY="50%"
  • translate

android:interpolator 动画的渲染器

accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速

decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速

accelerate_decelerate_interpolator(动画加速减速器)

中间位置分层:  使动画在开始的时候 最慢,然后逐渐加速         

使动画在开始的时候 最快,然后逐渐减速  结束的位置最慢

fromXDelta  动画起始位置的横坐标

toXDelta    动画起结束位置的横坐标

fromYDelta  动画起始位置的纵坐标

toYDelta   动画结束位置的纵坐标

duration 动画的持续时间

在实际项目中,我们经常使用补间动画,原因是补间动画使用起来比较方便,功能也比逐帧动画强大不少,而且还可以很方便地进行动画叠加,实现更加复杂的效果。

看完上述内容,你们对Android 中怎么实现补间动画有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI