Android補間動畫的實現(xiàn)示例
幀動畫是通過連續(xù)播放圖片來模擬動畫效果,而補間動畫開發(fā)者只需指定動畫開始,以及動畫結(jié)束"關鍵幀",而動畫變化的"中間幀"則由系統(tǒng)計算并補齊!
1.補間動畫的分類和Interpolator
Andoird所支持的補間動畫效果有如下這五種,或者說四種吧,第五種是前面幾種的組合而已。
- AlphaAnimation: 透明度漸變效果,創(chuàng)建時許指定開始以及結(jié)束透明度,還有動畫的持續(xù)時間,透明度的變化范圍(0,1),0是完全透明,1是完全不透明;對應<alpha/>標簽!
- ScaleAnimation:縮放漸變效果,創(chuàng)建時需指定開始以及結(jié)束的縮放比,以及縮放參考點,還有動畫的持續(xù)時間;對應<scale/>標簽!
- TranslateAnimation:位移漸變效果,創(chuàng)建時指定起始以及結(jié)束位置,并指定動畫的持續(xù)時間即可;對應<translate/>標簽!
- RotateAnimation:旋轉(zhuǎn)漸變效果,創(chuàng)建時指定動畫起始以及結(jié)束的旋轉(zhuǎn)角度,以及動畫持續(xù)時間和旋轉(zhuǎn)的軸心;對應<rotate/>標簽
- AnimationSet:組合漸變,就是前面多種漸變的組合,對應<set/>標簽
在開始講解各種動畫的用法之前,我們先要來講解一個東西:Interpolator
用來控制動畫的變化速度,可以理解成動畫渲染器,當然我們也可以自己實現(xiàn)Interpolator接口,自行來控制動畫的變化速度,而Android中已經(jīng)為我們提供了五個可供選擇的實現(xiàn)類:
- LinearInterpolator:動畫以均勻的速度改變
- AccelerateInterpolator:在動畫開始的地方改變速度較慢,然后開始加速
- AccelerateDecelerateInterpolator:在動畫開始、結(jié)束的地方改變速度較慢,中間時加速
- 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:結(jié)束透明度透明度的范圍為: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軸縮放的結(jié)束比例
- 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:動畫結(jié)束位置的X/Y坐標
4)RotateAnimation(旋轉(zhuǎn)漸變)
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:旋轉(zhuǎn)的起始/結(jié)束角度
- repeatCount:旋轉(zhuǎn)的次數(shù),默認值為0,代表一次,假如是其他值,比如3,則旋轉(zhuǎn)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="旋轉(zhuǎn)漸變" /> <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,同樣非常簡單,只需調(diào)用AnimationUtils.loadAnimation()加載動畫,然后我們的View控件調(diào)用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補間動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android的RV列表刷新詳解Payload與Diff方式異同
這篇文章主要為大家介紹了Android的RV列表刷新詳解Payload與Diff方式異同,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Android 自定義ListView實現(xiàn)QQ空間界面(說說內(nèi)包含圖片、視頻、點贊、評論、轉(zhuǎn)發(fā)功能)
這篇文章主要介紹了Android 自定義ListView實現(xiàn)QQ空間界面,qq空間說說內(nèi)包含圖片、視頻、點贊、評論、轉(zhuǎn)發(fā)功能,需要的朋友可以參考下2019-12-12Android自定義可編輯、刪除的側(cè)滑LisitView
這篇文章主要為大家詳細介紹了Android自定義可編輯、刪除的側(cè)滑LisitView,感興趣的小伙伴們可以參考一下2016-07-07Android中SharedPreference詳解及簡單實例
這篇文章主要介紹了 Android中SharedPreference詳解及簡單實例的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09Android編程解析XML文件的方法詳解【基于XmlPullParser】
這篇文章主要介紹了Android編程解析XML文件的方法,結(jié)合實例形式分析了Android基于XmlPullParser解析xml文件的相關操作技巧與注意事項,需要的朋友可以參考下2017-07-07