逐幀動畫
1.最重要:自定義一個drawable->animation.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
根標籤爲animation-list,其中oneshot代表着是否只展示一遍,設置爲false會不停的循環播放動畫
根標籤下,通過item標籤對動畫中的每一個圖片進行聲明
android:duration 表示展示所用的該圖片的時間長度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"//一次播放還是循環播放
>
<item android:drawable="@drawable/icon1" android:duration="150"></item>
<item android:drawable="@drawable/icon2" android:duration="150"></item>
<item android:drawable="@drawable/icon3" android:duration="150"></item>
<item android:drawable="@drawable/icon4" android:duration="150"></item>
<item android:drawable="@drawable/icon5" android:duration="150"></item>
<item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>
2.創建AnimationDrawable 對象
//XML裏聯繫剛創建的drawable
<ImageView android:id="@+id/animationIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:src="@drawable/animation1"/>
//java代碼
AnimationDrawable anim=(AnimationDrawable)imageview.getDrawable();
注意:
代碼使用imageview.getDrawable獲取AnimationDrawable 對象
layout中屬性就需要使用android:src="@drawable/animation"
代碼使用imageview.getBackground獲取AnimationDrawable 對象
layout中屬性就需要使用android:background="@drawable/animation"
3.啓動動畫
anim.start();//啓動需要在某個監聽器內,不能放在onCreate方法內執行,因爲AnimationDrawable 類在onCreate方法前還沒加載完
4.代碼中添加幀可用addFrame()
補間動畫
- 移動補間動畫:TranslateAnimation
TranslateAnimation animTran=new TranslateAnimation(currX,nextX,currY,nextY);
animTran.setDuration(200);
imageView.startAnimation(animTran);
ps: 補間動畫只是將view繪製在目標位置,並不是將view真實移動到目標位置,所以監聽器什麼的響應不了
- 透明補間動畫: AlphaAnimation
Animation animation = new AlphaAnimation(fromAlpha,toAlpha);
//fromAlpha起始透明度,toAlpha目標透明度
- 旋轉補間動畫:RotateAnimation
Animation animation = new RotateAnimation(
360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
/*
參數1:旋轉的起始角度
參數2:旋轉的終止角度
參數3:旋轉中心的x軸取值參照方式
參數4:中心點x軸的取值
參數5:旋轉中心的y軸取值參照方式
參數6:中心點y軸的取值
*/
- 縮放補間動畫:ScaleAnimation
Animation animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
/*
參數1:x方向起始大小(1f表示原圖大小)
參數2:x方向終止大小(0.2f表示原圖的0.2倍)
參數3:y方向起始大小(1f表示原圖大小)
參數4:y方向終止大小(0.2f表示原圖的0.2倍)
參數5:縮放中心點x軸取值的參照方式
參數6:中心點x軸的取值(0.5f表示相對與原圖的0.5倍)
參數7:縮放中心點y軸取值參照方式
參數8:中心點y軸的取值(0.5f表示相對與原圖的0.5倍)
*/
XML方法定義補間動畫
anim_alpha.xml
1.<?xml version="1.0" encoding="utf-8"?>
2.<set xmlns:android="http://schemas.android.com/apk/res/android"
3.android:fillEnabled="true"
4.android:fillAfter="true"
5. >
6. <alpha
7. android:duration="2000"
8. android:fromAlpha="1"
9. android:repeatCount="1"
10. android:repeatMode="reverse"
11. android:toAlpha="0" />
12.</set>
java代碼聯繫xml
1. Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
屬性動畫
比較常用的幾個動畫類是:ValueAnimator,ObjectAnimator和AnimatorSet,其中ObjectAnimator繼承自ValueAnimator
以下舉三個例子簡單使用
- 1.改變一個對象(myObject)的translationY屬性,讓其沿着Y軸向上平移一段距離:它的高度,改動畫在默認時間完成,可
自定義時間,還可以自定義插值器和估值算法。
ObjectAnimator.ofFloat(myObject,"translationY",-myObject.getHeight()).start();
- 2.改變一個對象的背景色屬性。下面動畫讓背景色在3秒內實現0xffff8080到0xff8080ff的漸變,動畫會無限循環且反轉
ValueAnimator colorAnim=ObjectAnimator.ofInt(myObject,"backgroundColor",/*Red*/0xffff8080,/*Blue*/0xff8080ff);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);//無限
colorAnim.setRepeatMode(ValueAnimator.REVERSE);//相反
colorAnim.start();
- 3.動畫集合,5秒對View的旋轉,平移,縮放和透明度都進行改變。
AnimatorSet set=new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(myView,"rotationX",0,360),
ObjectAnimator.ofFloat(myView,"rotationY",0,180),
ObjectAnimator.ofFloat(myView,"rotation",0,360),
ObjectAnimator.ofFloat(myView,"translationX",0,90),
ObjectAnimator.ofFloat(myView,"translationY",0,90),
ObjectAnimator.ofFloat(myView,"scaleX",1,1.5f),
ObjectAnimator.ofFloat(myView,"scaleY",1,0.5f),
ObjectAnimator.ofFloat(myView,"alpha",1,0.25f,1)
);
set.setDuration(5*1000).start();
XML實現屬性動畫
在animator目錄創建property_animator.xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:propertyName="x"
android:duration="300"
android:valueTo="200"
android:valueType="intType"/>
<objectAnimator
android:propertyName="y"
android:duration="300"
android:valueTo="300"
android:valueType="intType"/>
</set>
java代碼
AnimatorSet set1=(AnimatorSet) AnimatorInflater.loadAnimator(AttrAnimaActvity.this,R.animator.property_animator);
set1.setTarget(myView);
set1.start();
在實際開發中建議使用代碼來實現屬性動畫,因爲用代碼實現更簡單