Android 三種動(dòng)畫(huà)詳解
幀動(dòng)畫(huà)
一張張圖片不斷的切換,形成動(dòng)畫(huà)效果
在drawable目錄下定義xml文件,子節(jié)點(diǎn)為animation-list,在這里定義要顯示的圖片和每張圖片的顯示時(shí)長(zhǎng)
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/g1" android:duration="200" /> <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" 、 android:duration="200" /> </animation-list>
在屏幕上播放幀動(dòng)畫(huà)
ImageView iv = (ImageView) findViewById(R.id.iv); //把動(dòng)畫(huà)文件設(shè)置為imageView的背景 iv.setBackgroundResource(R.drawable.animations); AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); //播放動(dòng)畫(huà) ad.start();
補(bǔ)間動(dòng)畫(huà)
位移:
//創(chuàng)建為位移動(dòng)畫(huà)對(duì)象,設(shè)置動(dòng)畫(huà)的初始位置和結(jié)束位置TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
動(dòng)畫(huà)播放相關(guān)的設(shè)置
//設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間 ta.setDuration(2000); //動(dòng)畫(huà)重復(fù)播放的次數(shù) ta.setRepeatCount(1); //動(dòng)畫(huà)重復(fù)播放的模式 ta.setRepeatMode(Animation.REVERSE); //動(dòng)畫(huà)播放完畢后,組件停留在動(dòng)畫(huà)結(jié)束的位置上 ta.setFillAfter(true); //播放動(dòng)畫(huà) iv.startAnimation(ta);
縮放:
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
透明:
0為完全透明,1為完全不透明
AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
旋轉(zhuǎn):
RotateAnimation ra = new RotateAnimation(20, 360);
RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
所有動(dòng)畫(huà)一起飛
//創(chuàng)建動(dòng)畫(huà)集合 AnimationSet set = new AnimationSet(false); //往集合中添加動(dòng)畫(huà) set.addAnimation(aa); set.addAnimation(sa); set.addAnimation(ra); iv.startAnimation(set);
屬性動(dòng)畫(huà)
位移:
//具有g(shù)et、set方法的成員變量就稱(chēng)為屬性O(shè)bjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;
縮放:
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
透明:
透明度,0是完全透明,1是完全不透明
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
旋轉(zhuǎn)
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);
可變參數(shù)
第三個(gè)參數(shù)可變參數(shù)可以傳入多個(gè)參數(shù),可以實(shí)現(xiàn)往回位移(旋轉(zhuǎn)、縮放、透明)
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;
所有動(dòng)畫(huà)一起飛
//創(chuàng)建動(dòng)畫(huà)師集合 AnimatorSet set = new AnimatorSet(); //設(shè)置要播放動(dòng)畫(huà)的組件 set.setTarget(bt); //所有動(dòng)畫(huà)有先后順序的播放 //set.playSequentially(oa, oa2, oa3, oa4); //所有動(dòng)畫(huà)一起播放 set.playTogether(oa, oa2, oa3, oa4); set.start();
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注