前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android的Animation用法简介

android的Animation用法简介

作者头像
提莫队长
发布2019-03-01 09:45:05
5770
发布2019-03-01 09:45:05
举报
文章被收录于专栏:刘晓杰刘晓杰

简单一点的,如AlphaAnimation。直接定义,然后设置属性,然后startAnimation 复杂一点的,可以用AnimationSet。如下:

代码语言:javascript
复制
                AnimationSet animationSet = new AnimationSet(false);

                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
                alphaAnimation.setDuration(3000);

                RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation.setDuration(3000);

                animationSet.addAnimation(alphaAnimation);
                animationSet.addAnimation(rotateAnimation);

                imageView.startAnimation(animationSet);

上面的构造函数的参数什么意思就不说明了,自己可以查API。当然也可以用到AnimationUtils

代码语言:javascript
复制
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);

其中alpha.xml如下

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="1"
        android:toAlpha="0.1"
        android:duration="3000"
        android:repeatCount="3"/>

    <rotate 
        android:startOffset="3000"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="3000"/>

</set>

最后讲一下FrameAnimation。先定义Drawable(frame.xml)

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/red" android:duration="200" />
    <item android:drawable="@drawable/yellow" android:duration="200" />
    <item android:drawable="@drawable/blue" android:duration="200" />
</animation-list>

然后像这样使用:

代码语言:javascript
复制
imageView.setBackgroundResource(R.drawable.frame);
ad = (AnimationDrawable)imageView.getBackground();
ad.start();
//不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年03月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档