android animation——動畫的基本屬性
阿新 • • 發佈:2019-01-25
如果想在app中加入炫酷的動畫,那麼這些基本屬性是必須掌握的。
本來我使用word文件記錄的,傳遞過來只能以這種方式顯示了。
1, view動畫
a) 四種變換:
i. 平移,<translate>,TranslateAnimation
ii. 縮放,<scale>,ScaleAnimation
iii. 旋轉,<rotate>,RotateAnimation
iv. 透明度,<alpha>,AlphaAnimation
b) Xml檔案建立
i. res/anim
c) 標籤含義
i. <set >動畫集合:
1. interpolator 為插值器,插值器的選擇影響動畫的移動過程
2. shareInterpolator 表示集合中的動畫是否公用一個插值器,true/false
3. duration 動畫的持續時間
4. fillAfter 動畫結束後view是否停留在結束為止,true/false
ii. <translate>平移:
1. fromXDelta:x的起始值,0
2. toXDelta:x 的結束值,100
3. fromYDelta:y的起始值
4. toYDelta:y的結束值
iii. <scale>縮放:
1. fromXScale:水平縮放起始值:0
2. toXScale:水平縮放結束值:1
3. fromYScale:豎直方向起始值
4. toYScale: 豎直方向結束值
5. pivotX:縮放軸點X
6. pivotY:縮放軸點Y
iv. <rotate>旋轉:
1. fromDegrees旋轉開始的角度:
2. toDegrees:旋轉結束的角度
3. pivotX:旋轉的軸點x
4. pivotY:旋轉的軸點y
v. <alpha>透明度:
1. fromAlpha:透明度的起始值,0
2. toAlpha:透明度的結束值,1
d) 引用xml檔案:
i. Animation animation=AnimationUtils.loadAnimaton(context,R.anim.anim_demo);
View.startAnimation(animationss);
2, 幀動畫
a) Xml檔案建立:drawable/anim_demo
b) Xml檔案寫法:
i. 根標籤:<animation-list>
1. Oneshot 是否只播放一次,true/false
ii. Item :<item>:
1. Duration 一幀的播放時間,毫秒
iii. 引用xml檔案:
1. View.setBackgroundResource(R.drawable.anim_demo);
2. AnimationDrawable animationDrawable=(AnimationDrawable)view.getBackground();
3, LayoutAnimation(父佈局的動畫效果,指向子view進行):
a) 定義xml檔案:
i. <LayoutAnimation delay=”0.5” adimationOrder=”normal” animation=”@anim/anim_demo”/>
1. Delay:開始動畫延遲,子動畫會依次延遲出場,0到1
2. adimationOrder:子動畫順序
a) normal 順序顯示
b) reverse 逆向顯示
c) random 隨機出場
3. animation 為子元素指定具體的入場動畫
b) 使用方法:
i. 給GroupView指定layoutAnimation屬性
1. layoutAnimation=”@anim/anim_layout”
c) 程式碼實現方法:
i. Animation animation=AnimationUtils.loadAnimation(context,R.anim.anim_item);
ii. LayoutAnimationController controller=new LayoutAniationController(animation);
iii. Controller.setDelay(0.5f);
iv. Controller.setorder(LayoutAnimationController.ORDER_NORMAL);
v. listView.setLayoutAnimation(controller);
4, Activity的切換效果:
a) overridePendingTransition(int enterAnim,int exitAnim);
i. 這個方法必須在finish();或者startActivity(intent);之後呼叫
ii. EnterAnim: activity被開啟時呼叫的動畫資源
iii. exitAnim:activity被暫停時呼叫的動畫資源
b) 使用方法:
i. Intent intent=new Intent(this,activity.class);
ii. startActivity(intent);
iii. overridePendingTransition(R.anim.anim_demo1,R.anim.anim_demo2);
c) activity退出時也可呼叫該方法
d) Fragment也可以新增切換動畫
i. FragmentTransaction中的setCustomAnimations()方法
5, 屬性動畫(最靈活的動畫實現方式):
a) ObjectAnimator:
i. 程式碼實現:ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(view,"translationY",-view.getHeight());
objectAnimator.setDuration(1000);//動畫執行時間
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);//設定重複計數。設定成無限迴圈
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);//設定重複的模式,反轉效果
objectAnimator.start();
ii. 標籤含義:
1. propertyName=“translationY”:屬性動畫的作用物件名稱
2. duration=”1000”:動畫的時常
3. valueFrom=”200”:屬性的起始值
4. valueTo=“300”:屬性的結束值
5. startOffset=”500”:動畫的延遲時間
6. repeatCount=”1”:動畫的重複次數,infinite表示無限迴圈,0預設,-1也表示無限迴圈
7. repeatMode=”動畫的重複模式”,reverse反轉效果,restart從頭開始
8. valueType=”intType”:表示propertyName所指向的屬性型別,intType和FloatType兩種
b) ValueAnimator:
i. 程式碼實現:ValueAnimator valueAnimator1=ObjectAnimator.ofInt(view,"backgroundColor",/*紅色*/0xffff8080,/*藍色*/0xff8080ff);
valueAnimator1.setDuration(1000);//動畫執行時間
valueAnimator1.setEvaluator(new ArgbEvaluator());//設定估值器
valueAnimator1.setRepeatCount(ValueAnimator.INFINITE);//設定重複計數。設定成無限迴圈
valueAnimator1.setRepeatMode(ValueAnimator.REVERSE);//設定重複的模式,反轉效果
valueAnimator1.start();
ii. 標籤含義
1. ValueAnimator的標籤屬性只比ObjectAnimator少了propertyName標籤
c) AnimatorSet(屬性動畫集合):
i. AnimatorSet animatorSet=new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(show_anim,"rotationX",0,360),
ObjectAnimator.ofFloat(show_anim,"rotationY",0,180),
ObjectAnimator.ofFloat(show_anim,"rotation",0,-90),
ObjectAnimator.ofFloat(show_anim,"translationY",0,90),
ObjectAnimator.ofFloat(show_anim,"translationX",0,90),
ObjectAnimator.ofFloat(show_anim,"scaleX",1,1.5f),
ObjectAnimator.ofFloat(show_anim,"scaleY",1,1.5f),
ObjectAnimator.ofFloat(show_anim,"alpha",1,0.25f,1)
);
animatorSet.setDuration(time);
animatorSet.start();
ii. 標籤含義:
1. Ordering:子動畫的播放模式:together表示同時播放,sequentially表示按順序播放
6, 插值器的種類:
a) LinearInterpolator linearInterpolator;//線性插值器,根據時間百分比設定屬性百分比
b) TimeInterpolator timeInterpolator;//時間插值器,勻速動畫
c) AccelerateInterpolator accelerateInterpolator;//加速度插值器,兩頭慢中間快,預設的播放效果
d) DecelerateInterpolator decelerateInterpolator;//減速插值器,越來越慢