日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android动画Rotate

發(fā)布時間:2023/12/31 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android动画Rotate 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

項目有一個需求,有一個刷新按鈕,上面放著一個常見的靜止的刷新圓圈,如下圖:

?

?

一旦用戶按了刷新按鈕,需要讓這個刷新圓圈轉(zhuǎn)動起來,讓用戶感覺到程序還在運行著,而不是卡死了。

?

有兩個思路,一是將這個圖按照旋轉(zhuǎn)時間不同旋轉(zhuǎn)成不同旋轉(zhuǎn)角度的圖片,就像要做一張gif圖片一樣,例如我要每次旋轉(zhuǎn)30度,就需要360\30=12張圖片,然后再anim文件夾下新建xml文件,內(nèi)容如下:

?

Xml代碼 ?
  • <animation-list?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:oneshot="true">??
  • ????<item?android:drawable="@drawable/rocket_thrust1"?android:duration="200"?/>??
  • ????<item?android:drawable="@drawable/rocket_thrust2"?android:duration="200"?/>??
  • ????<item?android:drawable="@drawable/rocket_thrust3"?android:duration="200"?/>??
  • </animation-list>??
  • <animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true"><item android:drawable="@drawable/rocket_thrust1" android:duration="200" /><item android:drawable="@drawable/rocket_thrust2" android:duration="200" /><item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>

    ?

    ?

    在代碼中這樣寫:

    ?

    Java代碼 ?
  • AnimationDrawable?rocketAnimation; ??
  • ??
  • public?void?onCreate(Bundle?savedInstanceState)?{ ??
  • ??super.onCreate(savedInstanceState); ??
  • ??setContentView(R.layout.main); ??
  • ??
  • ??ImageView?rocketImage?=?(ImageView)?findViewById(R.id.rocket_image); ??
  • ??rocketImage.setBackgroundResource(R.anim.rocket_thrust); ??
  • ??rocketAnimation?=?(AnimationDrawable)?rocketImage.getBackground(); ??
  • } ??
  • ??
  • public?boolean?onTouchEvent(MotionEvent?event)?{ ??
  • ??if?(event.getAction()?==?MotionEvent.ACTION_DOWN)?{ ??
  • ????rocketAnimation.start(); ??
  • ????return?true; ??
  • ??} ??
  • ??return?super.onTouchEvent(event); ??
  • }??
  • AnimationDrawable rocketAnimation;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);rocketImage.setBackgroundResource(R.anim.rocket_thrust);rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); }public boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {rocketAnimation.start();return true;}return super.onTouchEvent(event); }

    ?

    具體代碼含義參考:http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html

    ?

    ?

    這種做法其實就是將每一幀圖片都顯示了一次,但是由于需要更多圖片,文件體積會上升。

    ?

    于是想到用rotate做單幀圖片旋轉(zhuǎn),查到的資料:http://rainbowsu.iteye.com/blog/766608

    ?

    但是作者沒能實現(xiàn)循環(huán)旋轉(zhuǎn),我嘗試了下,修改了下anim文件的格式,成功了

    ?

    Xml代碼 ?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <rotate?xmlns:android="http://schemas.android.com/apk/res/android"?android:interpolator="@android :anim/linear_interpolator"??
  • ????android:fromDegrees="0"?android:toDegrees="+360"?android:duration="1000"??
  • ????android:pivotX="50%"?android:pivotY="50%"?android:repeatCount="infinite"?/>??
  • <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"android:fromDegrees="0" android:toDegrees="+360" android:duration="1000"android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" />

    ?

    ?

    其中android:duration="1000"表示旋轉(zhuǎn)速率是1秒鐘。

    ?

    代碼:

    ?

    Java代碼 ?
  • package?info.wegosoft; ??
  • ??
  • import?android.app.Activity; ??
  • import?android.os.Bundle; ??
  • import?android.view.animation.Animation; ??
  • import?android.view.animation.AnimationUtils; ??
  • ??
  • public?class?LoadingAnimationTest?extends?Activity?{ ??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{ ??
  • ????????super.onCreate(savedInstanceState); ??
  • ????????setContentView(R.layout.main); ??
  • ???????? ??
  • ????????Animation?anim?=?AnimationUtils.loadAnimation(this,?R.anim.round_loading);???? ??
  • ???????? ??
  • ????????findViewById(R.id.loadingBtn).startAnimation(anim);??? ??
  • ????} ??
  • }??
  • package info.wegosoft;import android.app.Activity; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.AnimationUtils;public class LoadingAnimationTest extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Animation anim = AnimationUtils.loadAnimation(this, R.anim.round_loading); findViewById(R.id.loadingBtn).startAnimation(anim); } }

    ?

    布局文件:

    ?

    Java代碼 ?
  • <?xml?version="1.0"?encoding="utf-8"?> ??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"?android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"> ??
  • ????<Button?android:id="@+id/loadingBtn"?android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"?android:background="@drawable/refresh_normal"></Button> ??
  • </LinearLayout>??
  • <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/loadingBtn" android:layout_width="wrap_content"android:layout_height="wrap_content" android:background="@drawable/refresh_normal"></Button> </LinearLayout>

    ?

    ?

    工程見附件。

    ?

    最后提供官方文檔相關(guān)說明的鏈接:http://developer.android.com/guide/topics/resources/animation-resource.html

    ?

    注意其中的勻速插值器LinearInterpolator似乎不能設(shè)置速率,我在這浪費了很多時間。

    轉(zhuǎn)載于:https://my.oschina.net/u/586684/blog/170406

    總結(jié)

    以上是生活随笔為你收集整理的android动画Rotate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。