溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android中Animation如何使用

發(fā)布時(shí)間:2021-06-26 15:04:39 來(lái)源:億速云 閱讀:159 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)Android中Animation如何使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

Android使用Animation方法一:在xml中定義動(dòng)畫(huà):

Xml代碼

  1. < ?xml version="1.0" encoding="utf-8"?>   

  2. < set xmlns:android=
    "http://schemas.android.com/apk/res/android">   

  3. < rotate   

  4. android:interpolator="@android:anim/accelerate_
    decelerate_interpolator"   

  5. android:fromDegrees="0"   

  6. android:toDegrees="+360"   

  7. android:duration="3000" />   

  8. < !-- rotate 旋轉(zhuǎn)動(dòng)畫(huà)效果   

  9. 屬性:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器,用來(lái)控制動(dòng)畫(huà)的速度變化   

  10. fromDegrees 屬性為動(dòng)畫(huà)起始時(shí)物件的角度   

  11. toDegrees 屬性為動(dòng)畫(huà)結(jié)束時(shí)物件旋轉(zhuǎn)的角度,+代表順時(shí)針   

  12. duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間,以毫秒為單位   

  13. -->   

  14. < /set>   

  15. < ?xml version="1.0" encoding="utf-8"?> 

  16. < set xmlns:android=
    "http://schemas.android.com/apk/res/android"> 

  17. < rotate   

  18. android:interpolator=
    "@android:anim/accelerate_decelerate_interpolator" 

  19. android:fromDegrees="0"   

  20. android:toDegrees="+360" 

  21. android:duration="3000" /> 

  22. < !-- rotate 旋轉(zhuǎn)動(dòng)畫(huà)效果  

  23. 屬性:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器,用來(lái)控制動(dòng)畫(huà)的速度變化  

  24. fromDegrees 屬性為動(dòng)畫(huà)起始時(shí)物件的角度   

  25. toDegrees 屬性為動(dòng)畫(huà)結(jié)束時(shí)物件旋轉(zhuǎn)的角度,+代表順時(shí)針  

  26. duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間,以毫秒為單位  

  27. --> 

  28. < /set> 

使用動(dòng)畫(huà)的Java代碼,程序的效果是點(diǎn)擊按鈕,TextView旋轉(zhuǎn)一周:

Java代碼

  1. package com.ray.animation;   

  2. import android.app.Activity;   

  3. import android.os.Bundle;   

  4. import android.view.View;   

  5. import android.view.View.OnClickListener;   

  6. import android.view.animation.Animation;   

  7. import android.view.animation.AnimationUtils;   

  8. import android.widget.Button;   

  9. import android.widget.TextView;   

  10. public class TestAnimation extends Activity 
    implements OnClickListener{   

  11. public void onCreate(Bundle savedInstanceState) {   

  12. super.onCreate(savedInstanceState);   

  13. setContentView(R.layout.main);   

  14. Button btn = (Button)findViewById(R.id.Button01);   

  15. btn.setOnClickListener(this);   

  16. }   

  17. @Override   

  18. public void onClick(View v) {   

  19. Animation anim = AnimationUtils.loadAnimation(this, 
    R.anim.my_rotate_action);   

  20. findViewById(R.id.TextView01).startAnimation(anim);   

  21. }   

  22. }   

  23. package com.ray.animation;  

  24. import android.app.Activity;  

  25. import android.os.Bundle;  

  26. import android.view.View;  

  27. import android.view.View.OnClickListener;  

  28. import android.view.animation.Animation;  

  29. import android.view.animation.AnimationUtils;  

  30. import android.widget.Button;  

  31. import android.widget.TextView;  

  32. public class TestAnimation extends Activity 
    implements OnClickListener{  

  33. public void onCreate(Bundle savedInstanceState) {  

  34. super.onCreate(savedInstanceState);  

  35. setContentView(R.layout.main);  

  36. Button btn = (Button)findViewById(R.id.Button01);  

  37. btn.setOnClickListener(this);   

  38. }  

  39. @Override  

  40. public void onClick(View v) {  

  41. Animation anim = AnimationUtils.loadAnimation(this, 
    R.anim.my_rotate_action);  

  42. findViewById(R.id.TextView01).startAnimation(anim);  

  43. }  

Android使用Animation方法二:直接在代碼中定義動(dòng)畫(huà)(效果跟方法一類(lèi)似):

Java代碼

  1. package com.ray.animation;   

  2. import android.app.Activity;   

  3. import android.os.Bundle;   

  4. import android.view.View;   

  5. import android.view.View.OnClickListener;   

  6. import android.view.animation.AccelerateDecelerateInterpolator;   

  7. import android.view.animation.Animation;   

  8. import android.view.animation.RotateAnimation;   

  9. import android.widget.Button;   

  10. public class TestAnimation extends Activity 
    implements OnClickListener{   

  11. public void onCreate(Bundle savedInstanceState) {   

  12. super.onCreate(savedInstanceState);   

  13. setContentView(R.layout.main);   

  14. Button btn = (Button)findViewById(R.id.Button);   

  15. btn.setOnClickListener(this);   

  16. }   

  17. public void onClick(View v) {   

  18. Animation anim = null;   

  19. anim = new RotateAnimation(0.0f,+360.0f);   

  20. anim.setInterpolator(new AccelerateDecelerateInterpolator());   

  21. anim.setDuration(3000);   

  22. findViewById(R.id.TextView01).startAnimation(anim);   

  23. }   

關(guān)于Android中Animation如何使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI