Android ApiDemos樣本解析(96):Views->Animation->Int

來源:互聯網
上載者:User

定義動畫一般是通過定義主要畫面格(首幀或是尾幀)然後由系統自動產生中間幀,產生中間幀的過程可以稱為“插值 interpolate”。Android Animation 支援多種插值演算法:Interpolators (可以翻譯成插值器)。

所有Interpolators 都實現Interpolator 介面(實際上為TimeInterpolator介面),這個介面定義了一個方法:

public abstract float getInterpolation(float input)

input 為正規化後動畫的時間,範圍總之0-1之間。 0代表開始時間,1代表結束時間。
傳回值也是正規化後的值,可以代表平移的時間,旋轉的角度,Alpha值變化,標準範圍為0-1之間,但允許值小於0或大於1,表示插值到標準地區兩邊。
總的來說,Interpolator定義了動畫變化的速率,提供提供不同的函數定義變化值相對於時間的變化規則,可以定義各種各樣的非線性變換函數,比如加速,減速等。

Android 系統內建了多種Interpolator 可以通過多種動畫變化效果:

AccelerateDecelerateInterpolator 先加速再減速
AccelerateInterpolator 加速
AnticipateInterpolator 先回退一小步,然後再迅速前進
AnticipateOvershootInterpolator 先回退一小步,然後再迅速前進,在超過右邊界一小步
BounceInterpolator 實現彈球效果
CycleInterpolator 周期運動
DecelerateInterpolator 減速
LinearInterpolator 勻速
OvershootInterpolator 快速前進到右邊界上,再往外突出一小步
這些Interpolator 可以應用於任意的Animation中,如TranslateAnimation, RotateAnimation, ScaleAnimation ,AlphaAnimation 等,其插值的對象隨Animation 種類不同而不同。比如對於TranslateAnimation來說,插值的對象是位移,對應的動畫是平移的速率。對於RotateAnimation來說插值的對象為旋轉角度,對應的動畫為旋轉的速率。

本例介紹使用多種Interpolator 給一個TextView 添加動畫。

<TextView
android:id=”@+id/target”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”26sp”
android:text=”@string/animation_3_text”/>

本例對應的樣本類為Animation3,使用了如下幾種Interpolator:

[java] 
private static final String[] INTERPOLATORS = {  
 "Accelerate", "Decelerate", "Accelerate/Decelerate",  
 "Anticipate", "Overshoot", "Anticipate/Overshoot",  
 "Bounce" 
}; 

private static final String[] INTERPOLATORS = {
 "Accelerate", "Decelerate", "Accelerate/Decelerate",
 "Anticipate", "Overshoot", "Anticipate/Overshoot",
 "Bounce"
};採用的Animation 類型為TranslateAnimation 平移動畫移動TextView:


[java] 
final View target = findViewById(R.id.target);  
final View targetParent = (View) target.getParent();  
   
Animation a = new TranslateAnimation(0.0f,  
 targetParent.getWidth() - target.getWidth()  
 - targetParent.getPaddingLeft() -  
 targetParent.getPaddingRight(), 0.0f, 0.0f);  
a.setDuration(1000);  
a.setStartOffset(300);  
a.setRepeatMode(Animation.RESTART);  
a.setRepeatCount(Animation.INFINITE);  
   
...  
   
a.setInterpolator(AnimationUtils.loadInterpolator(this,  
 android.R.anim.accelerate_decelerate_interpolator));  
...  
   
target.startAnimation(a); 

final View target = findViewById(R.id.target);
final View targetParent = (View) target.getParent();
 
Animation a = new TranslateAnimation(0.0f,
 targetParent.getWidth() - target.getWidth()
 - targetParent.getPaddingLeft() -
 targetParent.getPaddingRight(), 0.0f, 0.0f);
a.setDuration(1000);
a.setStartOffset(300);
a.setRepeatMode(Animation.RESTART);
a.setRepeatCount(Animation.INFINITE);
 
...
 
a.setInterpolator(AnimationUtils.loadInterpolator(this,
 android.R.anim.accelerate_decelerate_interpolator));
...
 
target.startAnimation(a);
 

 


你也可以自訂Interpolator,只要實現Interpolator介面即可。

 作者:mapdigit
 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.