編輯:關於android開發
Android 平台提供了一套完整的動畫框架,在Android3.0之前有兩種動畫Tween Animation(補間動畫)和Frame Animation(幀動畫),
對應SDK中的View Animation和Drawable Animation。
在Android3.0之後,新增了一種動畫Property Animation(屬性動畫)。
一: 補間動畫(res/anim/ )
Tween Animation可以對view實現一系列的轉換,給出兩個關鍵幀,通過一些算法將給定屬性值在給定的時間內在兩個關鍵幀間漸變。可以通過 代碼 和 xml 定義,代碼中定義用: AlphaAnimation…
特點:
Tween Animation只能應用於View對象,並且只支持一部分屬性。它並不會改變屬性的值,只是改變了View對象繪制的位置。比如,一個按鈕在動畫過後,不再原來的位置,但是觸發點擊事件的任然是原坐標。
xml中必須有一個跟節點,可以是漸變<alpha>, 伸縮<scale>, 移動<translate>, 旋轉<rotate>, 或者 <set>。<set>是一個動畫集,可以包含前面的一個或多個。
1 <!-- XML中定義 – --> 2 <set android:shareInterpolator="false"> 3 <scale 4 android:interpolator="@android:anim/accelerate_decelerate_interpolator" 5 android:fromXScale="1.0" 6 android:toXScale="1.4" 7 android:fromYScale="1.0" 8 android:toYScale="0.6" 9 android:pivotX="50%" 10 android:pivotY="50%" 11 android:fillAfter="false" 12 android:duration="700" /> 13 </set>
代碼中使用:
1 ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); 2 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 3 spaceshipImage.startAnimation(hyperspaceJumpAnimation);
定義動畫的三個步驟 1. 創建動畫對象 2. 設置時長 3 . 開啟動畫
Interpolators 插值器
可以讓動畫按照一定的頻率運動,實現加速、減速、重復、回彈等效果。
XML 文件存放在:res/anim 下,並定義android:interpolator="@android:anim/.."來使用
二: 幀動畫(res/drawable/)
Frame Animation是一系列的圖片按順序顯示,來模擬動畫的效果。 代碼中通過AnimationDrawable來定義。
1 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 2 android:oneshot="true"> 3 <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 4 <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 5 <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 6 </animation-list>
必須以<animation-list>為根元素,以<item>表示要輪換顯示的圖片,duration屬性表示各項顯示的時間。
1 protected void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 setContentView(R.layout.main);
4 imageView = (ImageView) findViewById(R.id.imageView1);
5 imageView.setBackgroundResource(R.drawable.drawable_anim);
6 anim = (AnimationDrawable) imageView.getBackground();
7 }
8
9 public boolean onTouchEvent(MotionEvent event) {
10 if (event.getAction() == MotionEvent.ACTION_DOWN) {
11 anim.stop();
12 anim.start();
13 return true;
14 }
15 return super.onTouchEvent(event);
16 }
注意事項:
三 : 屬性動畫(res/animator/)//3.0之後新加的特性
Property Animation更改的是對象的實際屬性,如Button的縮放,Button的位置與大小屬性值都改變了。它不止可以應用於View,還可以應用於almost任何對象,在任何時候(即使沒有draws to the screen)。Property Animation只是表示一個值在一段時間內的改變,當值改變時要做什麼事情完全是你自己決定的。
Property Animation允許我們定義的屬性:
①: ValueAnimator :包含Property Animation動畫的所有核心功能,如動畫時間,開始、結束屬性值,相應時間屬性值計算方法等。
應用ValueAnimator有兩個步聚:
第一步 ValuAnimiator 已經完成了,我們只需要實現ValueAnimator.onUpdateListener接口
1 ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
2 animation.setDuration(1000);
3 animation.addUpdateListener(new AnimatorUpdateListener() {
4 @Override
5 public void onAnimationUpdate(ValueAnimator animation) {
6 TextView tv = new TextView(getApplication());
7 tv.setTranslationX((Float) animation.getAnimatedValue());
8 }
9 });
10 animation.setInterpolator(new CycleInterpolator(3));
11 animation.start();
②:ObjectAnimator :它是ValueAnimator的子類,它完成了ValueAnimator中 前兩步 操作
為了讓 ObjectAnimator 正確的更新屬性, 我們必須做下面的操作:
foo, 你必須有一個 setFoo() 方法,如果沒有,則只能使用ValueAnimator 1 復制代碼
2 tv=(TextView)findViewById(R.id.textview1);
3 btn=(Button)findViewById(R.id.button1);
4 btn.setOnClickListener(new OnClickListener() {
5 @Override
6 public void onClick(View v) {
7 ObjectAnimator oa
8 =ObjectAnimator.ofFloat(tv, "alpha"
9 , 0f, 1f);
10 oa.setDuration(3000);
11 oa.start();
12 }
13 });
在有些情況下,我們可能需要在onAnimationUpdate() callback中 執行invalidate() 方法來強制屏幕重繪。
③:AnimatorSet動畫集
在動畫集中,我們可以設置組中動畫的時序關系,如同時播放,順序播放等。
1 AnimatorSet bouncer = new AnimatorSet(); 2 bouncer.play(bounceAnim).before(squashAnim1); 3 bouncer.play(squashAnim1).with(squashAnim2); 4 bouncer.play(squashAnim1).with(stretchAnim1); 5 bouncer.play(squashAnim1).with(stretchAnim2); 6 bouncer.play(bounceBackAnim).after(stretchAnim2); 7 ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); 8 fadeAnim.setDuration(250); 9 AnimatorSet animatorSet = new AnimatorSet(); 10 animatorSet.play(bouncer).before(fadeAnim); 11 animatorSet.start();
④: 動畫監聽
Animator.AnimatorListener中定義了動畫的一些公共接口
1 onAnimationStart() 2 3 onAnimationEnd() 4 5 onAnimationRepeat() 6 7 //當動畫被取消時調用,同時會調用onAnimationEnd(). 8 onAnimationCancel()
注意:
通過觀察我們發現AnimatorListenerAdapter這個類 實現了Animator.AnimatorListener接口{每個方法的內部實現都是空} ,我們可以通過繼承這個類 {只復寫需要復寫的方法},而不是實現接口,來達到精簡代碼的操作。
1 ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);
2 oa.setDuration(3000);
3 oa.addListener(new AnimatorListenerAdapter(){
4 public void on AnimationEnd(Animator animation){
5 Log.i("Animation","end");
6 }
7 });
8 oa.start();
⑤ViewGroups過渡動畫
1 <LinearLayout 2 android:orientation="vertical" 3 android:layout_width="wrap_content" 4 android:layout_height="match_parent" 5 android:id="@+id/verticalContainer" 6 <!--設置為true,在添加或者移除View的時候,播放動畫--> 7 android:animateLayoutChanges="true" /> 8 復制代碼
常見過度動畫類型:
⑥View的animate()方法 和 ViewPropertyAnimator
1 // need API12
2 imageView.animate()
3 .alpha(0)
4 .y(50).setDuration(1000)
5 // need API 12
6 .withStartAction(new Runnable() {
7 //動畫前要執行的操作
8 @Override
9 public void run() {
10 Log.e(TAG, "START");
11 }
12 // need API 16
13 }).withEndAction(new Runnable() {
14 // 動畫後要執行 的操作
15 @Override
16 public void run() {
17 Log.e(TAG, "END");
18 runOnUiThread(new Runnable() {
19 @Override
20 public void run() {
21 imageView.setY(0);
22 imageView.setAlpha(1.0f);
23 }
24 });
25 }
26 }).start();
ViewPropertyAnimator類多用於對一個View的多個屬性進行動畫,該類對多屬性動畫進行了優化,會合並一些invalidate()來減少刷新視圖
1 /**與上面代碼的f功能相同*/
2 PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
3 0f, 1f);
4 PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 50, 0);
5 ObjectAnimator.ofPropertyValuesHolder(mBlueBall, pvhX, pvhY).setDuration(1000).start();
⑦TypeEvaluator
根據屬性的開始、結束值與TimeInterpolation計算出的因子計算出當前時間的屬性值
1 public class FloatEvaluator implements TypeEvaluator {
2
3 public Object evaluate(float fraction, Object startValue, Object endValue) {
4 float startFloat = ((Number) startValue).floatValue();
5 /**
6 fraction 是根據動畫執行的時間跟應用的Interplator,計算出的一個0~1之間的因子
7 */
8 return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
9 }
10 }
⑧XML中使用屬性動畫
XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 3 android:duration="1000" 4 android:propertyName="scaleX" 5 android:valueFrom="1.0" 6 android:valueTo="2.0" 7 android:valueType="floatType" > 8 </objectAnimator>
code:
1 public void scaleX(View view)
2 {
3 // 加載動畫
4 Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex);
5 anim.setTarget(mMv);
6 anim.start();
7 }
學習來自此處,謝謝大家!
注冊界面設計及實現之(三)SharedPerferences實現數據暫存,sharedptr實現
注冊界面設計及實現之(三)SharedPerferences實現數據暫存,sharedptr實現開發步驟: 創建一個SharedPerferences接口對象,並使用其
android 使用http請求查詢手機號碼歸屬地,android手機號碼
android 使用http請求查詢手機號碼歸屬地,android手機號碼歸屬地數據源 http://webservice.webxml.com.cn/WebServic
ListView + PopupWindow實現滑動刪除,popupwindowlistview
ListView + PopupWindow實現滑動刪除,popupwindowlistview 原文:ListView滑動刪除 ,仿騰訊QQ(鴻洋_) 文章
Android--JNI簡單的實例解析
Android--JNI簡單的實例解析 最近項目迭代了幾個版本,目前比較輕松,雖然項目閒了,但是人不能太閒,否則就廢了。千裡之行始於足下、量變引起質變、學而不思則罔.