這里將講述:
- 逐幀動畫(FrameAnimation) 、補間動畫(TweenAnimation) 敢朱、屬性動畫(PropertyAnimation)剪菱;
- As的res文件中,分別使用drawable拴签、anim孝常、animator目錄下的xml編寫動畫;
逐幀動畫(FrameAnimation)
它的實現(xiàn)方式也有兩種:代碼和xml方式篓吁;
-
代碼方式:
private void setSrcFrameAnim() { animationDrawable = new AnimationDrawable(); // 為AnimationDrawable添加動畫幀 animationDrawable.addFrame( getResources().getDrawable(R.drawable.img00), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img01), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img02), 50); // 設置為循環(huán)播放 animationDrawable.setOneShot(false); imageView.setBackground(animationDrawable); animationDrawable.start(); }
-
xml方式:
在res/drawable文件夾下新建animation-list的XML實現(xiàn)幀動畫<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > <!-- animation-list 幀動畫 --> <!-- android:oneshot的值為 false代表播放多次茫因,true代表只播放一次 --> <item android:drawable="@drawable/img00" android:duration="50"/> <item android:drawable="@drawable/img01" android:duration="50"/> <item android:drawable="@drawable/img02" android:duration="50"/> </animation-list>
在布局中可以這樣:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.havorld.frameanimation.MainActivity" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <!-- android:background="@drawable/frame_anim" --> </RelativeLayout>
在代碼中我們這樣:
private void setFrameAnim() { imageView.setBackgroundResource(R.drawable.frame_anim); animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start(); //或者這樣 animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_anim); imageView.setBackground(animationDrawable); animationDrawable.start(); }
-
Drawable Animation(補間動畫)
補間動畫的屬性有:RotateAnimation、AlphaAnimation杖剪、ScaleAnimation冻押、TranslateAnimation、AnimationSet 盛嘿,再者可以加上用于xml動畫文件引入的AnimationUtils類洛巢,示例:
ImageView iv = (ImageView) dialog.findViewById(R.id.loading_iv);
RotateAnimation rotateAnimation = new RotateAnimation(0, 5760, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f); //相對自己左上為0.0
// Animation rotateAnimation=AnimationUtils.loadAnimation(this, R.anim.rote); //當然這里你也可以用xml方式
rotateAnimation.setDuration(10000);
iv.startAnimation(rotateAnimation);
// rotateAnimation.start(); //或這樣啟動
-
Property Animation 屬性動畫
涉及到的屬性值有 (名稱一定要寫對):
??? 1、透明度:alpha
??? 2次兆、旋轉度數(shù):rotation稿茉、rotationX、rotationY
??? 3芥炭、平移:translationX漓库、translationY
??? 4、縮放:scaleX园蝠、scaleY
示例 1. 使用代碼實現(xiàn):
ObjectAnimator animator = ObjectAnimator
.ofFloat(iv, "rotation", 0.0F, 360.0F)
.setDuration(800);
animator .setRepeatCount(ObjectAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator .start();
示例 2. 使用 xml文件配置動畫(R.animator.anim_file):
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially" >
<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:valueFrom="-500"
android:valueTo="0"
android:valueType="floatType" >
</objectAnimator>
<set android:ordering="together" >
<objectAnimator
android:duration="3000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360"
android:valueType="floatType" >
</objectAnimator>
<set android:ordering="sequentially" >
<objectAnimator
android:duration="1500"
android:propertyName="alpha"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType" >
</objectAnimator>
<objectAnimator
android:duration="1500"
android:propertyName="alpha"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType" >
</objectAnimator>
</set>
</set>
</set>
然后在代碼中用AnimatorInflater引入:
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.anim_file);
animator.setTarget(view);
animator.start();
-
動畫總結:
補間動畫:
XXXAnimation animation = new XXXAnimation (); //需要什么動畫就new什么動畫渺蒿,然后.start();
AnimationSet animationSet = new AnimationSet(false); //代碼方式實現(xiàn)補間動畫
animationSet.addAnimation(new AlphaAnimation(1,0));
Animation animation = AnimationUtils.loadAnimation(context, R.anim.cs);//加載的是補間動畫文件(在anim中);
注意:若想給動畫設置重復模式彪薛,設置給AnimationSet是無效的茂装,需要給每個單個動畫設置才能生效,如:
alphaAnimation.setRepeatMode(Animation.RESTART);
alphaAnimation.setRepeatCount(Animation.INFINITE);屬性動畫:
ObjectAnimator.ofFloat(iv, "rotation", 0.0F, 360.0F).start();
AnimatorSet animatorSet = new AnimatorSet(); //代碼方式實現(xiàn)屬性動畫
animatorSet.playTogether();
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.scale_with_alpha); //加載的是屬性動畫文件(在animator中)anim善延、animator目錄下的xml中動畫屬性是不同的:
- res的anim文件夾下XML文件中屬性對應關系(補間動畫):
<set> 對應代碼中 AnimationSet
<rotate> 對應: RotateAnimation
<alpha> 對應: AlphaAnimation
<scale> 對應: ScaleAnimation
<translate> 對應: TranslateAnimation - res的animator文件夾的XML文件中屬性對應關系(屬性動畫):
<set> 對應代碼中的 AnimatorSet
<objectAnimator> 對應代碼中的 ObjectAnimator
<animator> 對應代碼中的 ValueAnimator
- res的anim文件夾下XML文件中屬性對應關系(補間動畫):
-
接下來說下Drawable的繪制(在res的drawable目錄下):
1 . <layer-list>的機理就是一層層的覆蓋少态,示例:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--底部圖層a,是其他圖層item位置鎖定的基礎--> <item android:height="99dp" android:width="99dp" android:gravity="center" > <rotate android:drawable="@drawable/ic_launcher" <----此處可用shap替代易遣,也可以放帶動畫的Drawable android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360"/> </item> <!--該圖層放在底部圖層a之上彼妻,位置為相對a的位置--> <item android:gravity="center" android:height="40dp" android:width="40dp"> <rotate android:drawable="@drawable/lt" android:fromDegrees="360" android:pivotX="50%" android:pivotY="50%" android:toDegrees="0"/> </item> </layer-list>
2 . Drawable的<animated-selector>類似于selector(效果如CheckBox的動畫變換) 示例:
<?xml version="1.0" encoding="utf-8"?> <animated-selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/state_on" android:drawable="@drawable/lt" android:state_selected="true"/> <item android:id="@+id/state_off" android:drawable="@drawable/progressbar" android:state_selected="false"/> <transition android:fromId="@+id/state_on" android:toId="@+id/state_off"> <animation-list android:oneshot="true"> <item android:drawable="@drawable/lt" android:duration="188"/> <item android:drawable="@drawable/ic_launcher" android:duration="188"/> <item android:drawable="@drawable/shape_noraml" android:duration="122"/> <item android:drawable="@drawable/progressbar" android:duration="122"/> </animation-list> </transition> <transition <!--補間動畫執(zhí)行過程配置--> android:fromId="@+id/state_off" android:toId="@+id/state_on"> <animation-list android:oneshot="true"> <item android:drawable="@drawable/progressbar" android:duration="122"/> <item android:drawable="@drawable/shape_noraml" android:duration="122"/> <item android:drawable="@drawable/ic_launcher" android:duration="188"/> <item android:drawable="@drawable/lt" android:duration="188"/> </animation-list> </transition> </animated-selector>
布局文件中代碼
<Button android:id="@+id/btn_web_native" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/animation_selector" android:onClick="onClicked" />
在代碼中配置
view.setSelected(!view.isSelected());
-
告訴你個小秘密:
ProgressBar聽這名字就是做進度用的,但是它功能不僅僅如此训挡,你也可以用它來做圖片切換澳骤、旋轉等動畫歧强,比如上邊三種方法繪制的Drawable都能設置給ProgressBar,而達到強大的動畫效果为肮,如下:
布局代碼:
<ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="77dp" android:layout_centerHorizontal="true" android:layout_below="@id/loading_iv" android:indeterminateDrawable="@drawable/loadding" />
drawable/loadding.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--底部圖層a摊册,是其他圖層item位置鎖定的基礎--> <item android:gravity="center" android:height="59dp" android:width="59dp"> <rotate android:fromDegrees="1080" android:pivotX="50%" android:pivotY="50%" android:toDegrees="0"> <shape <----此處改用shap替代drawable,下邊則采用帶動畫的drawable android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="9" android:useLevel="false"> <gradient android:centerColor="@android:color/holo_red_light" android:centerY="0.2" android:endColor="#1E90FF" android:startColor="#000000" android:type="sweep" android:useLevel="false"/> </shape> </rotate> </item> <!--該圖層放在底部圖層a之上颊艳,位置為相對a的位置--> <item android:drawable="@drawable/progressbar" <----此處采用帶動畫的drawable android:gravity="center" android:height="33dp" android:width="33dp"> </item> </layer-list>
xml文件@drawable/progressbar代碼:
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:pivotX="50%" android:pivotY="50%" android:duration="10" android:repeatCount="-1" android:fromDegrees="0" android:toDegrees="360"> <shape android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="8" android:useLevel="false"> <gradient android:centerColor="#FF7121" android:centerY="0.50" android:endColor="#FFFF00" android:startColor="#6BD3FF" android:type="sweep" android:useLevel="false"/> </shape> </animated-rotate>
當然ProgressBar也可以用上邊的animation-list方式添加 android:indeterminateDrawable="@drawable/animation-list" 屬性茅特,效果也很棒,這個實驗的機會就留給你啦棋枕!
-
其它
還有一種簡單的設置動畫的方法白修,如下:
//ViewCompat這樣的單次屬性動畫,只適用于要求只執(zhí)行一次的需求重斑,執(zhí)行完后所有狀態(tài)將會停留在最后一刻兵睛, //這種單值設置動畫就是為了設置view的終態(tài)的,其默認view的初始狀態(tài)就是當其view的狀態(tài)窥浪。 ViewCompat.animate(target) .setDuration(2100) .translationY(screenData.onScreenHeight/2) .setInterpolator(new BounceInterpolator()) .rotation(1200) .alpha(0) .scaleX(8) .scaleY(7) .start()祖很;
。
漾脂。
假颇。
。
骨稿。
笨鸡。
好啦,就先寫到這里坦冠,后繼再來追加形耗。。辙浑。
.