本篇文章給大家分享的是有關怎么在Android應用中實現一個動畫效果,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Android 三種動畫詳解
幀動畫
一張張圖片不斷的切換,形成動畫效果
在drawable目錄下定義xml文件,子節點為animation-list,在這里定義要顯示的圖片和每張圖片的顯示時長
<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>
在屏幕上播放幀動畫
ImageView iv = (ImageView) findViewById(R.id.iv); //把動畫文件設置為imageView的背景 iv.setBackgroundResource(R.drawable.animations); AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); //播放動畫 ad.start();
補間動畫
位移:
//創建為位移動畫對象,設置動畫的初始位置和結束位置 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);
動畫播放相關的設置
//設置動畫持續時間 ta.setDuration(2000); //動畫重復播放的次數 ta.setRepeatCount(1); //動畫重復播放的模式 ta.setRepeatMode(Animation.REVERSE); //動畫播放完畢后,組件停留在動畫結束的位置上 ta.setFillAfter(true); //播放動畫 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);
旋轉:
RotateAnimation ra = new RotateAnimation(20, 360);
RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
所有動畫一起飛
//創建動畫集合 AnimationSet set = new AnimationSet(false); //往集合中添加動畫 set.addAnimation(aa); set.addAnimation(sa); set.addAnimation(ra); iv.startAnimation(set);
屬性動畫
位移:
//具有get、set方法的成員變量就稱為屬性 ObjectAnimator 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);
旋轉
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);
可變參數
第三個參數可變參數可以傳入多個參數,可以實現往回位移(旋轉、縮放、透明)
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;
所有動畫一起飛
//創建動畫師集合 AnimatorSet set = new AnimatorSet(); //設置要播放動畫的組件 set.setTarget(bt); //所有動畫有先后順序的播放 //set.playSequentially(oa, oa2, oa3, oa4); //所有動畫一起播放 set.playTogether(oa, oa2, oa3, oa4); set.start();
以上就是怎么在Android應用中實現一個動畫效果,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。