Android 動畫之TranslateAnimation應用詳解

來源:互聯網
上載者:User

標籤:

TranslateAnimation動畫,TranslateAnimation比較常用,比如QQ,網易新聞菜單條的動畫,就可以用TranslateAnimation實現, 
通過TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 來定義動畫 

參數說明: 

代碼如下:
float fromXDelta 動畫開始的點離當前View X座標上的差值 
float toXDelta 動畫結束的點離當前View X座標上的差值 
float fromYDelta 動畫開始的點離當前View Y座標上的差值 
float toYDelta 動畫開始的點離當前View Y座標上的差值 


常用方法: 

代碼如下:
animation.setDuration(long durationMillis);//設定動畫期間 
animation.setRepeatCount(int i);//設定重複次數 
animation.setRepeatMode(Animation.REVERSE);//設定反方向執行 


Xml屬性: 

代碼如下:
android:duration:運行動畫的時間 
android:repeatCount:定義動畫重複的時間 


代碼: 

代碼如下:
public class MainActivity extends Activity { 
ImageView image; 
Button start; 
Button cancel; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
image = (ImageView) findViewById(R.id.main_img); 
start = (Button) findViewById(R.id.main_start); 
cancel = (Button) findViewById(R.id.main_cancel); 
/** 設定位移動畫 向右位移150 */ 
final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0); 
animation.setDuration(2000);//設定動畫期間 
animation.setRepeatCount(2);//設定重複次數   -1代表一直迴圈
animation.setRepeatMode(Animation.REVERSE);//設定反方向執行 
start.setOnClickListener(new OnClickListener() { 
public void onClick(View arg0) { 
image.setAnimation(animation); 
/** 開始動畫 */ 
animation.startNow(); 

}); 
cancel.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
/** 結束動畫 */ 
animation.cancel(); 

}); 

 

Android 動畫之TranslateAnimation應用詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.