Android动画

动画

Android动画共分为四类

  1. Alpha 淡入淡出
  2. Scale 缩放
  3. Rotate 旋转
  4. Translate 平移

Alpha

//参数范围 0~1,0:完全透明, 1:完全不透明
//参数1: fromAlpha开始的透明度
//参数2: toAlpha结束的透明度
AlphaAnimation aa = new AlphaAnimation(1, 0);
aa.setDuration(300);//动画执行时间
View.startAnimation(aa);//启动动画,把View替换成要添加动画的控件

Scale

/** 
* Constructor to use when building a ScaleAnimation from code 
* 
* @param fromX Horizontal scaling factor to apply at the start of the 
*        animation 
* @param toX Horizontal scaling factor to apply at the end of the animation 
* @param fromY Vertical scaling factor to apply at the start of the 
*        animation 
* @param toY Vertical scaling factor to apply at the end of the animation 
* @param pivotXType Specifies how pivotXValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotXValue The X coordinate of the point about which the object 
*        is being scaled, specified as an absolute number where 0 is the 
*        left edge. (This point remains fixed while the object changes 
*        size.) This value can either be an absolute number if pivotXType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 
* @param pivotYType Specifies how pivotYValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotYValue The Y coordinate of the point about which the object
*        is being scaled, specified as an absolute number where 0 is the 
*        top edge. (This point remains fixed while the object changes 
*        size.) This value can either be an absolute number if pivotYType 
*        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 
*/
ScaleAnimation sa = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(3000);
View.startAnimation(sa);

Rotate

/** 
* Constructor to use when building a RotateAnimation from code 
*  
* @param fromDegrees Rotation offset to apply at the start of the 
*        animation. 
*  
* @param toDegrees Rotation offset to apply at the end of the animation. 
*  
* @param pivotXType Specifies how pivotXValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotXValue The X coordinate of the point about which the object 
*        is being rotated, specified as an absolute number where 0 is the 
*        left edge. This value can either be an absolute number if 
*        pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) 
*        otherwise. 
* @param pivotYType Specifies how pivotYValue should be interpreted. One of 
*        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 
*        Animation.RELATIVE_TO_PARENT. 
* @param pivotYValue The Y coordinate of the point about which the object 
*        is being rotated, specified as an absolute number where 0 is the 
*        top edge. This value can either be an absolute number if 
*        pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) 
*        otherwise. 
*/
RotateAnimation ra = new RotateAnimation(0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
ra.setDuration(3000);
View.startAnimation(ra);

Translate

TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);
ta.setDuration(3000);
View.startAnimation(ta);

Animation其他常用的方法

setRepeatCount(Animation.INFINITE);//设置动画的重复次数,INFINITE为无限次
setFillAfter(boolean fillAfter);//true:动画结束后,控件停留在执行后的状态
setFillBefore(boolean fillBefore);//true:动画结束后,控件停留在动画开始的状态
setInterpolator(new Interpolator);//动画的运动速度

组合动画

AnimationSet set = new Animation(true);
set.addAnimation(aa);
set.addAnimation(ra);
set.addAnimation(sa);
set.addAnimation(ta);
set.setDuration(3000);
set.setStartOffset(100);
View.startAnimation(set);

XML动画

xml文件存放在:res/anim下

XML组合动画 animation.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">  
    <alpha  
        android:fromAlpha="1.0"  
        android:toAlpha="0.0"  
        android:startOffset="500"  
        android:duration="2000"  
     /> 
    <scale  
        android:fromXScale="1.0"  
        android:toXScale="0.0"  
        android:fromYScale="1.0"  
        android:toYScale="0.0"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:duration="2000"  
     />  
     <rotate  
        android:fromDegrees="0"  
        android:toDegrees="400"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:duration="3000"  
     />  
     <translate  
        android:fromXDelta="50%"  
        android:toXDelta="100%"  
        android:fromYDelta="50%"  
        android:toYDelta="100%"  
        android:duration="3000"  
      /> 
      <!-- android:pivotX="50"使用绝对坐标   android:pivotX="50%"相对自己   android:pivotX="50%p"相对父控件 -->
</set>
</span>  

XML动画用法

代码获取动画

Animation a=AnimationUtils.loadAnimation(this, R.anim.animation);
View.startAnimation(a);

XML配置动画

<!-- 配置window展现、退出动画 -->
<style name="ActionSheetAnimation" parent="@android:style/Animation.Dialog">    
<item name="android:windowEnterAnimation">@anim/push_up_in</item>    
<item name="android:windowExitAnimation">@anim/push_up_out</item>
</style>

<!-- 设置动画 -->
<style name="Theme.DialogActivity" parent="@android:style/Theme.Holo.Light.Dialog">    
<item name="android:windowBackground">@color/black</item>    
<!-- 设置底层可见 -->    
<item name="android:windowIsTranslucent">true</item>    
<!-- 设置跳转效果 -->    
<item name="android:windowAnimationStyle">@style/ActionSheetAnimation</item>    
<item name="android:windowNoTitle">true</item>    
<item name="android:windowFullscreen">true</item>
</style>

<!-- 在Manifest中给增加动画的Activity配置Theme -->
android:theme="@style/Theme.DialogActivity"

帧动画

<!-- anim.xml -->
<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
 <item android:drawable="@drawable/a1" android:duration="500"/>
 <item android:drawable="@drawable/a2" android:duration="500"/>
 <item android:drawable="@drawable/a3" android:duration="500"/>  
 <item android:drawable="@drawable/a4" android:duration="500"/>  
</animation-list> 

ImageView.setBackgroundResource(R.drawable.anim);
AnimationDrawable ad = (AnimationDrawable)ImageView.getBackground();
ad.start();

LayoutAnimationController动画

XML 实现

<!-- anim.xml -->
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator"  
    android:shareInterpolator="true"  
    >  
    <alpha  
        android:fromAlpha="0"  
        android:toAlpha="1"  
        android:duration="3000"  
            />  
</set>  


<!-- layout.xml -->
<?xml version="1.0" encoding="utf-8"?>  
<layoutAnimation  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:delay="0.5"  
    android:animationOrder="normal"  
    android:animation="@anim/anim"  
    />


<ListView  
        android:id="@+id/listView1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"         
        android:layoutAnimation="@anim/layout"/>  

代码实现

AlphaAnimation alpha=new AlphaAnimation(0, 1);  
alpha.setDuration(3000);  
LayoutAnimationController lac=new LayoutAnimationController(alpha); 
//LayoutAnimationController.ORDER_NORMAL;//顺序显示  
//LayoutAnimationController.ORDER_REVERSE;//反显示  
//LayoutAnimationController.ORDER_RANDOM;//随机显示
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);  
lv.setLayoutAnimation(lac); 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容