Android中兩種使用Animation的方法

來源:互聯網
上載者:User

在Android中,分別可以在xml中定義Animation,也可以在程式碼中定義,下面的小例子是利用RotateAnimation簡單展示一下兩種方法的用法,對於其他動畫,如ScaleAnimation,AlphaAnimation,原理是一樣的。

 

方法一:在xml中定義動畫:

Xml代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://rayleung.javaeye.com/blog/413842#">
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.            
  4. <rotate    
  5.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  6.         android:fromDegrees="0"    
  7.         android:toDegrees="+360"  
  8.         android:duration="3000" />  
  9.            
  10. <!-- rotate 旋轉動畫效果   
  11.        屬性:interpolator 指定一個動畫的插入器,用來控制動畫的速度變化   
  12.         fromDegrees 屬性為動畫起始時物件的角度       
  13.         toDegrees   屬性為動畫結束時物件旋轉的角度,+代表順時針   
  14.         duration  屬性為動畫期間,以毫秒為單位   
  15. -->  
  16. </set>  
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">        <rotate         android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromDegrees="0"         android:toDegrees="+360"        android:duration="3000" />        <!-- rotate 旋轉動畫效果       屬性:interpolator 指定一個動畫的插入器,用來控制動畫的速度變化        fromDegrees 屬性為動畫起始時物件的角度            toDegrees   屬性為動畫結束時物件旋轉的角度,+代表順時針        duration  屬性為動畫期間,以毫秒為單位--></set>

 

使用動畫的Java代碼,程式的效果是點擊按鈕,TextView旋轉一周:

Java代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://rayleung.javaeye.com/blog/413842#">
  1. package com.ray.animation;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.view.View.OnClickListener;   
  7. import android.view.animation.Animation;   
  8. import android.view.animation.AnimationUtils;   
  9. import android.widget.Button;   
  10. import android.widget.TextView;   
  11.   
  12. public class TestAnimation extends Activity implements OnClickListener{   
  13.     public void onCreate(Bundle savedInstanceState) {   
  14.         super.onCreate(savedInstanceState);   
  15.         setContentView(R.layout.main);   
  16.         Button btn = (Button)findViewById(R.id.Button01);   
  17.         btn.setOnClickListener(this);        
  18.     }   
  19.   
  20.     @Override  
  21.     public void onClick(View v) {   
  22.         Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);   
  23.         findViewById(R.id.TextView01).startAnimation(anim);   
  24.     }   
  25. }  
package com.ray.animation;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.TextView;public class TestAnimation extends Activity implements OnClickListener{    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button btn = (Button)findViewById(R.id.Button01);        btn.setOnClickListener(this);         }@Overridepublic void onClick(View v) {Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);findViewById(R.id.TextView01).startAnimation(anim);}}

 

 方法二:直接在代碼中定義動畫(效果跟方法一類似):

Java代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://rayleung.javaeye.com/blog/413842#">
  1. package com.ray.animation;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.view.View.OnClickListener;   
  7. import android.view.animation.AccelerateDecelerateInterpolator;   
  8. import android.view.animation.Animation;   
  9. import android.view.animation.RotateAnimation;   
  10. import android.widget.Button;   
  11.   
  12. public class TestAnimation extends Activity implements OnClickListener{   
  13.   
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.main);   
  17.         Button btn = (Button)findViewById(R.id.Button);   
  18.         btn.setOnClickListener(this);        
  19.     }   
  20.   
  21.     public void onClick(View v) {   
  22.         Animation anim = null;   
  23.         anim = new RotateAnimation(0.0f,+360.0f);   
  24.         anim.setInterpolator(new AccelerateDecelerateInterpolator());   
  25.         anim.setDuration(3000);   
  26.         findViewById(R.id.TextView01).startAnimation(anim);    
  27.     }   
  28. }  
相關文章

聯繫我們

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