文章出處
文章列表
四種補間動畫:
1、透明;
2、縮放;
3、位移;
4、旋轉;
1 //點擊按鈕 實現iv 透明的效果 動畫 2 public void click1(View v) { 3 //1.0意味著著完全不透明 0.0意味著完全透明 4 AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f); 5 aa.setDuration(2000); //設置動畫執行的時間 6 aa.setRepeatCount(1); //設置重復的次數 7 aa.setRepeatMode(Animation.REVERSE);//設置動畫執行的模式 8 //iv開始執行動畫 9 iv.startAnimation(aa); 10 11 } 12 13 14 //點擊按鈕 實現iv 執行一個旋轉 動畫 15 public void click2(View v) { 16 //fromDegrees 開始角度 toDegrees 結束角度 17 // RotateAnimation ra = new RotateAnimation(0, 360); 18 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 19 ra.setDuration(2000); //設置動畫執行的時間 20 ra.setRepeatCount(1); //設置重復的次數 21 ra.setRepeatMode(Animation.REVERSE);//設置動畫執行的模式 22 //iv開始執行動畫 23 iv.startAnimation(ra); 24 25 } 26 27 //點擊按鈕進行一個縮放動畫 28 public void click3(View v) { 29 ScaleAnimation sa = new ScaleAnimation(1.0f,2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 30 sa.setDuration(2000); //設置動畫執行的時間 31 sa.setRepeatCount(1); //設置重復的次數 32 sa.setRepeatMode(Animation.REVERSE);//設置動畫執行的模式 33 //iv開始執行動畫 34 iv.startAnimation(sa); 35 } 36 37 //位移動畫 38 public void click4(View v){ 39 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f); 40 ta.setDuration(2000); //設置動畫執行的時間 41 ta.setFillAfter(true);//當動畫結束后 動畫停留在結束位置 42 43 //開始動畫 44 iv.startAnimation(ta); 45 } 46 47 //動畫一起飛 48 public void click5(View v){ 49 AnimationSet set = new AnimationSet(false); 50 51 //透明動畫 52 AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f); 53 aa.setDuration(2000); //設置動畫執行的時間 54 aa.setRepeatCount(1); //設置重復的次數 55 aa.setRepeatMode(Animation.REVERSE);//設置動畫執行的模式 56 //旋轉動畫 57 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 58 ra.setDuration(2000); //設置動畫執行的時間 59 ra.setRepeatCount(1); //設置重復的次數 60 ra.setRepeatMode(Animation.REVERSE);//設置動畫執行的模式 61 //縮放 62 ScaleAnimation sa = new ScaleAnimation(1.0f,2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 63 sa.setDuration(2000); //設置動畫執行的時間 64 sa.setRepeatCount(1); //設置重復的次數 65 sa.setRepeatMode(Animation.REVERSE);//設置動畫執行的模式 66 67 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f); 68 ta.setDuration(2000); //設置動畫執行的時間 69 ta.setFillAfter(true);//當動畫結束后 動畫停留在結束位置 70 71 //添加動畫 72 set.addAnimation(aa); 73 set.addAnimation(ra); 74 set.addAnimation(sa); 75 set.addAnimation(ta); 76 77 //最后一步 要記得 執行動畫 78 iv.startAnimation(set); 79 }
幾個屬性介紹:
1、Duration:設置動畫執行的時間;
2、RepeatCount:動畫的重復次數,如果要無限次播放,填寫一個小于0的數,一般寫-1;
3、fillAfter:動畫結束之后是否保持動畫的最終狀態;true,表示保持動畫的最終狀態
4、fillBefore:動畫結束之后是否保持動畫開始前的狀態;true,表示恢復到動畫開始前的狀態
5、startOffset:動畫的延遲時長,單位是毫秒
6、RepeatMode:動畫的執行模式:
- reverse:動畫是從一開始的1.0漸變成0.3,然后在從0.3漸變為1.0,重復往返
- restart:凍哈是從一開始的1.0漸變成0.3,然后圖片從0.3突變為1.0,然后在漸變成0.3,重復往返,
XML定義補間動畫:
透明:
1 <alpha 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:fromAlpha="1.0" 4 android:toAlpha="0.0" 5 android:duration="200" 6 android:repeatMode="reverse" 7 android:repeatCount="2"> 8 </alpha>
旋轉:
1 <rotate 2 android:fromDegrees="0" 3 android:toDegrees="360" 4 android:pivotX="50%" 5 android:pivotY="50%" 6 android:repeatCount="1" 7 android:repeatMode="reverse" 8 android:duration="2000" 9 xmlns:android="http://schemas.android.com/apk/res/android"> 10 </rotate>
縮放:
1 <scale 2 android:fromXScale="1.0" 3 android:toXScale="2.0" 4 android:fromYScale="1.0" 5 android:toYScale="2.0" 6 android:pivotX="50%" 7 android:pivotY="50%" 8 android:repeatMode="reverse" 9 android:repeatCount="1" 10 android:duration="2000" 11 xmlns:android="http://schemas.android.com/apk/res/android"> 12 </scale>
位移:
1 <translate 2 android:fromXDelta="0%p" 3 android:toXDelta="0%p" 4 android:fromYDelta="0%p" 5 android:toYDelta="20%p" 6 android:fillAfter="true" 7 android:duration="2000" 8 xmlns:android="http://schemas.android.com/apk/res/android"> 9 </translate>
實現XML動畫:
1 public void click1(View v) { 2 Animation aa = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha); 3 //iv開始執行動畫 4 iv.startAnimation(aa); 5 }
文章列表
全站熱搜