編輯:Android 資訊
ListView 是 Android 開發裡再熟悉不過的元件,但清一色相同的外觀看久了難免乏味。替列表加上進場動畫,不僅能讓畫面更有層次,也能明顯提升整體的操作體驗。本文整理五種常見的列表動畫效果與其實作方式。
前面四種效果都建立在傳統的 Tween 動畫之上,分別是位移(Translate)、透明度(Alpha)、旋轉(Rotate)與縮放(Scale)。第五種則借助自訂的 Rotate3dAnimation 工具類達成 Y 軸 3D 翻轉,建構時需傳入起始角、結束角與旋轉中心座標等參數。
LayoutAnimationController 能讓一組子元件依序出現,搭配 ListView 的 setLayoutAnimation() 即可讓整份清單逐項進場。核心程式碼如下:
private Button button, button2, button3, button4, button5;
private ListView mListView;
private Animation animation;
private LayoutAnimationController controller;
private String[] arry = { "一", "二", "三", "四", "五", "六" };
private ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arry);
mListView.setAdapter(adapter);
}
private void initView() {
mListView = (ListView) findViewById(R.id.list);
button = (Button) findViewById(R.id.btn_tran);
button2 = (Button) findViewById(R.id.btn_alpha);
button3 = (Button) findViewById(R.id.btn_rotate);
button4 = (Button) findViewById(R.id.btn_scale);
button5 = (Button) findViewById(R.id.rotate3d);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// ORDER_NORMAL 順序、ORDER_REVERSE 反向、ORDER_RANDOM 隨機
switch (arg0.getId()) {
case R.id.btn_tran:
animation = new TranslateAnimation(-50f, 0f, 0f, 0f);
animation.setDuration(500);
controller = new LayoutAnimationController(animation, 1f); // 第二個參數為項間延遲
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(controller);
adapter.notifyDataSetInvalidated();
break;
case R.id.btn_alpha:
animation = new AlphaAnimation(0f, 1f);
animation.setDuration(500);
controller = new LayoutAnimationController(animation, 1f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(controller);
adapter.notifyDataSetInvalidated();
break;
case R.id.btn_rotate:
animation = new RotateAnimation(0f, 360f);
animation.setDuration(500);
controller = new LayoutAnimationController(animation, 1f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(controller);
adapter.notifyDataSetInvalidated();
break;
case R.id.btn_scale:
animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
animation.setDuration(500);
controller = new LayoutAnimationController(animation, 1f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(controller);
adapter.notifyDataSetInvalidated();
break;
case R.id.rotate3d:
animation = new Rotate3dAnimation(0, 360, 200, 200, 0, true);
animation.setDuration(1000);
controller = new LayoutAnimationController(animation, 0.1f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
mListView.setLayoutAnimation(controller);
adapter.notifyDataSetInvalidated();
break;
}
}掌握這套寫法後,只要替換 Animation 實作並調整 duration 與 delay,就能自由變化出符合產品調性的進場效果。
為 Android Studio 提速:啟動、編譯與運行的優化實戰
對 Android 程序員來說,選對一款 IDE 能讓開發效率倍增。很多開發者鐘情於 Google 的 Android Studio,但用起來難免會遇到卡頓。本文整理了幾
讓 App 秒開:Android 啟動優化實戰思路
從一個抱怨開始 部門裡丟出了一個問題:App 啟動很慢,桌面圖標點下去要等好久才有反應,中低端機型特別明顯。由於自己對效能優化頗有興趣,於是借來一台低端機與一台中端機實測
從 AIDL 範例理解 Android Binder 進程間通信
Android 的元件可以執行在同一進程,也可以分布在不同進程。當元件位於同一進程時,通信相對簡單;但若需要跨進程呼叫,例如 Activity 與另一個應用中的 Serv
做了兩年半 Android 開發,這 18 條經驗我想早點告訴你
2014 年決定投入 Android 開發的那一天,我到現在都還記得——那是人生中數一數二正確的決定。一晃兩年半過去了。 剛入行的時候,沒有人告訴我什麼是對的做法。我犯過