1.補(bǔ)間動(dòng)畫 View Animation(Tween Animation)
1.1位移動(dòng)畫
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="300"
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="200"/>
<!--
fromXDelta 動(dòng)畫起始位置的橫坐標(biāo)
toXDelta 動(dòng)畫起結(jié)束位置的橫坐標(biāo)
fromYDelta 動(dòng)畫起始位置的縱坐標(biāo)
toYDelta 動(dòng)畫結(jié)束位置的縱坐標(biāo)
duration 動(dòng)畫的持續(xù)時(shí)間
100%p 表示自身的長度的位置
-->
代碼實(shí)現(xiàn):
Animation animation = new TranslateAnimation(0,50,0,50);
參數(shù)1:x軸的起始位置
參數(shù)2:x軸的終止位置
參數(shù)3: y軸的起始位置
參數(shù)4:y軸的終止位置
相對于原圖位置的原點(diǎn)(圖片的右上角為0,0)详民,如果不想用這個(gè)點(diǎn)作為參照點(diǎn)牙勘,可以使用其他構(gòu)造
TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)
參數(shù)1,參數(shù)3橄仍,參數(shù)5绣溜,參數(shù)7就是設(shè)置參照點(diǎn)的方式
imageView.startAnimation(animation);
1.2旋轉(zhuǎn)動(dòng)畫
xml定義
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="200" />
<!--
fromDegrees:表示旋轉(zhuǎn)的起始角度
toDegrees:表示旋轉(zhuǎn)的結(jié)束角度
duration 動(dòng)畫的持續(xù)時(shí)間
from<to:順時(shí)針旋轉(zhuǎn)
from>to:逆時(shí)針旋轉(zhuǎn)
-->
代碼實(shí)現(xiàn):
Animation animation = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
參數(shù)1:旋轉(zhuǎn)的起始角度
參數(shù)2:旋轉(zhuǎn)的終止角度
參數(shù)3:旋轉(zhuǎn)中心的x軸取值參照方式
參數(shù)4:中心點(diǎn)x軸的取值
參數(shù)5:旋轉(zhuǎn)中心的y軸取值參照方式
參數(shù)6:中心點(diǎn)y軸的取值
imageView.startAnimation(animation);
1.3透明度動(dòng)畫
xml定義:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="200"/>
<!--
fromAlpha :起始透明度
toAlpha:結(jié)束透明度
1.0表示完全不透明
0.0表示完全透明
-->
代碼實(shí)現(xiàn)
Animation animation = new AlphaAnimation(1f,0.1f);
參數(shù)1: 起始透明度;
參數(shù)2: 目標(biāo)透明度;
1.4縮放動(dòng)畫
xml定義
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="0.5"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"/>
<!--
fromXScale:表示沿著x軸縮放的起始比例
toXScale:表示沿著x軸縮放的結(jié)束比例
fromYScale:表示沿著y軸縮放的起始比例
toYScale:表示沿著y軸縮放的結(jié)束比例
1.0表示原來的大小
50%表示圖片中心點(diǎn)
-->
代碼實(shí)現(xiàn):
Animation animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
參數(shù)1:x方向起始大小(1f表示原圖大小)
參數(shù)2:x方向終止大小(0.2f表示原圖的0.2倍)
參數(shù)3:y方向起始大小(1f表示原圖大小)
參數(shù)4:y方向終止大小(0.2f表示原圖的0.2倍)
參數(shù)5:縮放中心點(diǎn)x軸取值的參照方式
參數(shù)6:中心點(diǎn)x軸的取值(0.5f表示相對與原圖的0.5倍)
參數(shù)7:縮放中心點(diǎn)y軸取值參照方式
參數(shù)8:中心點(diǎn)y軸的取值(0.5f表示相對與原圖的0.5倍)
(注:參照方式:Animation.RELATIVE_TO_SELF:自身 Animation.RELATIVE_TO_PARENT:父控件 Animation.ABSOLUTE:絕對)
imageView.startAnimation(animation);
1.5 共有屬性
android:duration(播放時(shí)間)
android:repeatCount: 重復(fù)的次數(shù) 默認(rèn)值是0 代表旋轉(zhuǎn)1次,值為-1或者infinite時(shí)伦仍,表示動(dòng)畫永不停止
android:repeatMode: 設(shè)置重復(fù)的模式结窘。默認(rèn)是restart。(當(dāng)repeatCount的值大于0或者為infinite時(shí)才有效)
設(shè)成reverse充蓝,表示偶數(shù)次顯示動(dòng)畫時(shí)會(huì)做與動(dòng)畫文件定義的方向相反的方向動(dòng)行隧枫。
對應(yīng)代碼方法:
Animation.setDuration(long durationMillis)
Animation.setRepeatCount(int repeatCount)
Animation.setRepeatMode(int repeatMode)
android:interpolator(動(dòng)畫的渲染器)
accelerate_interpolator(動(dòng)畫加速器) 使動(dòng)畫在開始的時(shí)候 最慢,然后逐漸加速
decelerate_interpolator(動(dòng)畫減速器) 使動(dòng)畫在開始的時(shí)候 最快,然后逐漸減速
accelerate_decelerate_interpolator(動(dòng)畫加速減速器)慢到快(中間位置最快)到慢
anticipate_interpolator(動(dòng)畫前反向器) 先往動(dòng)畫移動(dòng)的反方向移動(dòng)一點(diǎn)然后在沿著設(shè)定的動(dòng)畫移動(dòng)
overshoot_interpolator(動(dòng)畫后反向器) 最后超出目的值然后緩慢改變到目的值
anticipate_overshoot_interpolator (動(dòng)畫雙反向器) 先往動(dòng)畫的反方向移動(dòng)一點(diǎn),然后沿著設(shè)定的方向移動(dòng)到終點(diǎn)之后繼續(xù)移動(dòng)一點(diǎn)然后在回彈到最終設(shè)定的位置
bounce_interpolator(動(dòng)畫彈跳器) 快到目的值時(shí)值會(huì)跳躍
cycle_interpolator(動(dòng)畫循環(huán)器) 動(dòng)畫循環(huán)一定次數(shù)
linear_interpolator(動(dòng)畫勻速器) 線性均勻改變
overshoot_interpolator(動(dòng)畫減速器) 快速到達(dá)終點(diǎn)并超出一小步最后回到終點(diǎn)
使用:android:interpolator="@android:anim/accelerate_interpolator"
對應(yīng)代碼方法:
Animation.setInterpolator(Context context, int resID) //R.anim.accelerate_interpolator
Animation.setInterpolator(Interpolator i)
TimeInterpolator 一個(gè)接口谓苟,允許你自定義interpolator悠垛,以下幾個(gè)都是實(shí)現(xiàn)了這個(gè)接口
AccelerateInterpolator 加速,開始時(shí)慢中間加速
DecelerateInterpolator 減速娜谊,開始時(shí)快然后減速
AccelerateDecelerateInterolator 先加速后減速确买,開始結(jié)束時(shí)慢,中間加速
AnticipateInterpolator 反向 纱皆,先向相反方向改變一段再加速播放
AnticipateOvershootInterpolator 反向加超越湾趾,先向相反方向改變,再加速播放派草,會(huì)超出目的值然后緩慢移動(dòng)至目的值
BounceInterpolator 跳躍搀缠,快到目的值時(shí)值會(huì)跳躍,如目的值100近迁,后面的值可能依次為85艺普,77,70,80歧譬,90岸浑,100
CycleInterpolator 循環(huán),動(dòng)畫循環(huán)一定次數(shù)瑰步,值的改變?yōu)橐徽液瘮?shù):Math.sin(2 * mCycles * Math.PI * input)
LinearInterpolator 線性矢洲,線性均勻改變
OvershootInterpolator 超越,最后超出目的值然后緩慢改變到目的值
2.組合動(dòng)畫
xml定義:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true" >
<scale ... />
<rotate ... />
<translate ... />
<alpha ... />
<!--
shareInterpolator:里面動(dòng)畫效果是否共享一個(gè)Interpolator
不共享設(shè)置android:shareInterpolator="false"缩焦,每一個(gè)動(dòng)畫效果自己添加interpolator读虏。
-->
</set>
代碼實(shí)現(xiàn):
AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(1000);
alpha.setStartOffset(1000); //延時(shí)一秒后播放
AnimationSet animSet = new AnimationSet(false);
animSet.addAnimation(alpha);
animSet.cancel();//取消動(dòng)畫
animSet.reset();//釋放資源
2.2 LayoutAnimation(GridLayoutAnimationController)
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/alpha"
/>
<!--
delay:延遲多少開始播放
-->
代碼實(shí)現(xiàn):
AlphaAnimation alpha=new AlphaAnimation(0, 1);
alpha.setDuration(1000);
LayoutAnimationController lac=new LayoutAnimationController(alpha);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
ListView.setLayoutAnimation(lac);
LayoutAnimationController.ORDER_NORMAL; //順序顯示
LayoutAnimationController.ORDER_REVERSE; //反顯示
LayoutAnimationController.ORDER_RANDOM //隨機(jī)顯示
使用:
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layoutanimation"/>
2.逐幀動(dòng)畫 Drawable Animation(Frame Animation)
xml定義:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/girl1" android:duration="100" />
<item android:drawable="@drawable/girl2" android:duration="100" />
<item android:drawable="@drawable/girl3" android:duration="100" />
<!--
oneshot:如果為true袁滥,表示動(dòng)畫只播放一次停止在最后一幀上盖桥,如果設(shè)置為false表示動(dòng)畫循環(huán)播放
-->
</animation-list>
代碼實(shí)現(xiàn):
AnimationDrawable anim = new AnimationDrawable();
for (int i = 1; i <= 3; i++) {
int id = getResources().getIdentifier("girl" + i, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
anim.addFrame(drawable, 100);
}
播放
View.setBackgroundResource(R.anim.frame); // 可以在xml里直接@drawble/
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start();
注:想activity一開始就播放請放在onWindowFocusChanged方法里
View.setBackgroundResource(R.anim.frame); // 可以在xml里直接@drawble/
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start();
注:想activity一開始就播放請放在onWindowFocusChanged方法里
動(dòng)畫恢復(fù)到第一幀動(dòng)畫: anim.selectDrawable(0); //選擇當(dāng)前動(dòng)畫的第一幀,然后停止 anim.stop();