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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android LayoutAnimation使用及扩展

發(fā)布時間:2025/4/16 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android LayoutAnimation使用及扩展 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章來源

在Android中,最簡單的動畫就是補間動畫了。通過補間動畫,可以對一個控件進(jìn)行位移、縮放、旋轉(zhuǎn)、改變透明度等動畫。但是補間動畫只能對一個控件使用,如果要對某一組控件播放一樣的動畫的話,可以考慮layout-animation。

LayoutAnimation

layout-animation可由xml和代碼兩種方式配置:

XML配置

由于layout-animation是對于某一組控件的操作,就需要一個基本的動畫來定義單個控件的動畫。另外還可以定義動畫的顯示順序和延遲:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"android:delay="30%"android:animationOrder="reverse"android:animation="@anim/slide_right" />

其中

android:delay表示動畫播放的延時,既可以是百分比,也可以是float小數(shù)。
android:animationOrder表示動畫的播放順序,有三個取值normal(順序)、reverse(反序)、random(隨機)。
android:animation指向了子控件所要播放的動畫。
上述步驟完成之后,就可以將layout-animation應(yīng)用到ViewGroup中,xml布局添加下面一行就ok:

android:layoutAnimation="@anim/list_anim_layout"

這樣在加載布局的時候就會自動播放layout-animtion。

代碼配置
如果在xml中文件已經(jīng)寫好LayoutAnimation,可以使用AnimationUtils直接加載:

AnimationUtils.loadLayoutAnimation(context, id)

另外還可以手動java代碼編寫,如:

//通過加載XML動畫設(shè)置文件來創(chuàng)建一個Animation對象;Animation animation=AnimationUtils.loadAnimation(this, R.anim.slide_right);//得到一個LayoutAnimationController對象;LayoutAnimationController controller = new LayoutAnimationController(animation);//設(shè)置控件顯示的順序;controller.setOrder(LayoutAnimationController.ORDER_REVERSE);//設(shè)置控件顯示間隔時間;controller.setDelay(0.3);//為ListView設(shè)置LayoutAnimationController屬性;listView.setLayoutAnimation(controller);listView.startLayoutAnimation();

通過代碼設(shè)置可以達(dá)到同樣效果。

擴展

前幾天遇到一個需求,將動畫順序改為左上到右下角展開,例如Material Design中這個樣子。雖然一個個按順序播放頁可以實現(xiàn),但也太low了,就希望能夠找一個簡便的方法。(PS. 個人崇尚簡約就是美)

仔細(xì)觀察,很明顯就是一個LayoutAnimation,但是LayoutAnimation默認(rèn)只有三種順序,即順序逆序和隨機,不能滿足需求。去翻翻源碼看它是怎么實現(xiàn)的,有沒有提供方法自定義順序?結(jié)果翻到了一個LayoutAnimationController#getTransformedIndex(AnimationParameters params)方法,返回值就是播放動畫的順序。并且這個方法是protected的,明顯就是可由子類來擴展。既然找到了方法,那么就去實現(xiàn)它:

/*** custom LayoutAnimationController for playing child animation* in any order.**/ public class CustomLayoutAnimationController extends LayoutAnimationController {// 7 just lucky number public static final int ORDER_CUSTOM = 7;private Callback onIndexListener;public void setOnIndexListener(OnIndexListener onIndexListener) {this.onIndexListener = onIndexListener; }public CustomLayoutAnimationController(Animation anim) {super(anim); }public CustomLayoutAnimationController(Animation anim, float delay) {super(anim, delay); }public CustomLayoutAnimationController(Context context, AttributeSet attrs) {super(context, attrs); }/*** override method for custom play child view animation order */ protected int getTransformedIndex(AnimationParameters params) {if(getOrder() == ORDER_CUSTOM && onIndexListener != null) {return onIndexListener.onIndex(this, params.count, params.index);} else {return super.getTransformedIndex(params);} }/*** callback for get play animation order**/ public static interface Callback{public int onIndex(CustomLayoutAnimationController controller, int count, int index); } }

通過復(fù)寫getTransformedIndex方法,添加自定義順序ORDER_CUSTOM,讓callback自定義控件播放動畫的順序,即可以達(dá)到任何想要的效果。

總結(jié)

Android在設(shè)計的時候,提供了很好的擴展性。各種組件都可以按需求擴展??赡苡玫母嗟氖沁€是View.onDraw(Canvas canvas)方法,但不僅僅是view,對于其他組件,也可以隨意的擴展。很多時候,并不一定需要自定義很多空間、功能之類的,往往擴展一些簡單的組件就能達(dá)到自己想要的結(jié)果。

總結(jié)

以上是生活随笔為你收集整理的Android LayoutAnimation使用及扩展的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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