Android補間動畫的實現(xiàn)示例
幀動畫是通過連續(xù)播放圖片來模擬動畫效果,而補間動畫開發(fā)者只需指定動畫開始,以及動畫結束"關鍵幀",而動畫變化的"中間幀"則由系統(tǒng)計算并補齊!

1.補間動畫的分類和Interpolator
Andoird所支持的補間動畫效果有如下這五種,或者說四種吧,第五種是前面幾種的組合而已。
- AlphaAnimation: 透明度漸變效果,創(chuàng)建時許指定開始以及結束透明度,還有動畫的持續(xù)時間,透明度的變化范圍(0,1),0是完全透明,1是完全不透明;對應<alpha/>標簽!
- ScaleAnimation:縮放漸變效果,創(chuàng)建時需指定開始以及結束的縮放比,以及縮放參考點,還有動畫的持續(xù)時間;對應<scale/>標簽!
- TranslateAnimation:位移漸變效果,創(chuàng)建時指定起始以及結束位置,并指定動畫的持續(xù)時間即可;對應<translate/>標簽!
- RotateAnimation:旋轉漸變效果,創(chuàng)建時指定動畫起始以及結束的旋轉角度,以及動畫持續(xù)時間和旋轉的軸心;對應<rotate/>標簽
- AnimationSet:組合漸變,就是前面多種漸變的組合,對應<set/>標簽
在開始講解各種動畫的用法之前,我們先要來講解一個東西:Interpolator
用來控制動畫的變化速度,可以理解成動畫渲染器,當然我們也可以自己實現(xiàn)Interpolator接口,自行來控制動畫的變化速度,而Android中已經(jīng)為我們提供了五個可供選擇的實現(xiàn)類:
- LinearInterpolator:動畫以均勻的速度改變
- AccelerateInterpolator:在動畫開始的地方改變速度較慢,然后開始加速
- AccelerateDecelerateInterpolator:在動畫開始、結束的地方改變速度較慢,中間時加速
- CycleInterpolator:動畫循環(huán)播放特定次數(shù),變化速度按正弦曲線改變:Math.sin(2 * mCycles * Math.PI * input)
- DecelerateInterpolator:在動畫開始的地方改變速度較快,然后開始減速
- AnticipateInterpolator:反向,先向相反方向改變一段再加速播放
- AnticipateOvershootInterpolator:開始的時候向后然后向前甩一定值后返回最后的值
- BounceInterpolator: 跳躍,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
- OvershottInterpolator:回彈,最后超出目的值然后緩慢改變到目的值
2.各種動畫的詳細講解
這里的android:duration都是動畫的持續(xù)時間,單位是毫秒
1)AlphaAnimation(透明度漸變)
anim_alpha.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>屬性解釋:
fromAlpha :起始透明度toAlpha:結束透明度透明度的范圍為:0-1,完全透明-完全不透明
2)ScaleAnimation(縮放漸變)
anim_scale.xml:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>屬性解釋:
- fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例
- toXScale/toYScale:沿著X軸/Y軸縮放的結束比例
- pivotX/pivotY:縮放的中軸點X/Y坐標,即距離自身左邊緣的位置,比如50%就是以圖像的中心為中軸點
3)TranslateAnimation(位移漸變)
anim_translate.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>屬性解釋:
- fromXDelta/fromYDelta:動畫起始位置的X/Y坐標
- toXDelta/toYDelta:動畫結束位置的X/Y坐標
4)RotateAnimation(旋轉漸變)
anim_rotate.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/> 屬性解釋:
- fromDegrees/toDegrees:旋轉的起始/結束角度
- repeatCount:旋轉的次數(shù),默認值為0,代表一次,假如是其他值,比如3,則旋轉4次另外,值為-1或者infinite時,表示動畫永不停止
- repeatMode:設置重復模式,默認restart,但只有當repeatCount大于0或者infinite或-1時才有效。還可以設置成reverse,表示偶數(shù)次顯示動畫時會做方向相反的運動!
5)AnimationSet(組合漸變)
非常簡單,就是前面幾個動畫組合到一起而已
anim_set.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set> 3.寫個例子來體驗下
好的,下面我們就用上面寫的動畫來寫一個例子,讓我們體會體會何為補間動畫:首先來個簡單的布局:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度漸變" />
<Button
android:id="@+id/btn_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="縮放漸變" />
<Button
android:id="@+id/btn_tran"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位移漸變" />
<Button
android:id="@+id/btn_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋轉漸變" />
<Button
android:id="@+id/btn_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="組合漸變" />
<ImageView
android:id="@+id/img_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="48dp"
android:src="@mipmap/img_face" />
</LinearLayout>好噠,接著到我們的MainActivity.java,同樣非常簡單,只需調用AnimationUtils.loadAnimation()加載動畫,然后我們的View控件調用startAnimation開啟動畫即可。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_alpha;
private Button btn_scale;
private Button btn_tran;
private Button btn_rotate;
private Button btn_set;
private ImageView img_show;
private Animation animation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
}
private void bindViews() {
btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_scale = (Button) findViewById(R.id.btn_scale);
btn_tran = (Button) findViewById(R.id.btn_tran);
btn_rotate = (Button) findViewById(R.id.btn_rotate);
btn_set = (Button) findViewById(R.id.btn_set);
img_show = (ImageView) findViewById(R.id.img_show);
btn_alpha.setOnClickListener(this);
btn_scale.setOnClickListener(this);
btn_tran.setOnClickListener(this);
btn_rotate.setOnClickListener(this);
btn_set.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_alpha:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
img_show.startAnimation(animation);
break;
case R.id.btn_scale:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
img_show.startAnimation(animation);
break;
case R.id.btn_tran:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
img_show.startAnimation(animation);
break;
case R.id.btn_rotate:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
img_show.startAnimation(animation);
break;
case R.id.btn_set:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_set);
img_show.startAnimation(animation);
break;
}
}
}運行效果圖:

到此這篇關于Android補間動畫的實現(xiàn)示例的文章就介紹到這了,更多相關Android補間動畫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android實戰(zhàn)教程第七篇之如何在內存中存儲用戶名和密碼
這篇文章主要為大家詳細介紹了Android如何實現(xiàn)在內存中存儲用戶名和密碼的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android開發(fā)之a(chǎn)ctivity的生命周期詳解
這篇文章主要介紹了Android開發(fā)之a(chǎn)ctivity的生命周期,詳細分析了activity的運行原理與生命周期,需要的朋友可以參考下2016-06-06
根據(jù)USER-AGENT判斷手機類型并跳轉到相應的app下載頁面
檢測瀏覽器的USER-AGENT,然后根據(jù)正則表達式來確定客戶端類型,并跳轉到相應的app下載頁面,這個方法還是比較實用的,大家可以看看2014-09-09

