編輯:關於 Android 程式設計
面試中經常被問到的一個問題是:Android 一共有哪幾類動畫?先記住這份清單——變換動畫(透明度、縮放、平移、旋轉)、逐幀動畫、佈局動畫、屬性動畫。
變換動畫既可以寫在 XML 裡,也可以用 Java 代碼創建。經驗法則是:當動畫參數需要動態計算時用代碼更靈活;參數固定的場景則寫 XML 更省事。
XML 方式,在 res/anim 下新建 alphademo.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<!-- alpha 取值 0.0(全透明)~1.0(完全不透明) -->
</set>
加載並啟動:
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alphademo); imageView.startAnimation(alphaAnimation);
代碼方式:
// 參數一:起始透明度;參數二:結束透明度 AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 0.8f); alphaAnimation.setDuration(3000); // 持續時間,單位毫秒 imageView.startAnimation(alphaAnimation);
scaledemo.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<scale
android:duration="2000"
android:fromXScale="0.2" <!-- X 起始比例,小於 1 為縮小 -->
android:toXScale="1.2" <!-- X 結束比例 -->
android:fromYScale="0.0"
android:toYScale="1"
android:pivotX="50%" <!-- 縮放基準點 X -->
android:pivotY="50%" <!-- 縮放基準點 Y -->
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true" /> <!-- 動畫結束後保持末態 -->
</set>
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scaledemo); imageView.startAnimation(animation);
代碼方式:
// 參數依次為:X 起點、X 終點、Y 起點、Y 終點 ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 0.0f, 1.0f); scaleAnimation.setDuration(2000); scaleAnimation.setFillAfter(true); imageView.startAnimation(scaleAnimation);
XML 方式與啟動代碼:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translatedemo); imageView.startAnimation(animation);
代碼方式:
TranslateAnimation translateAnimation = new TranslateAnimation(10.0f, 200f, 30f, 210f); translateAnimation.setDuration(2000); translateAnimation.setFillAfter(true); imageView.startAnimation(translateAnimation);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="45" <!-- 起始角度 -->
android:toDegrees="60" <!-- 結束角度,可以大於 360 -->
android:fillAfter="true" />
<!-- 角度為負表示逆時針,為正表示順時針;
pivotX / pivotY 為相對於物件自身的基準點,取值 0%~100%,50% 即中心 -->
</set>
RotateAnimation rotateAnimation = new RotateAnimation(0f, -30f); rotateAnimation.setDuration(2000); rotateAnimation.setFillAfter(true); imageView.startAnimation(rotateAnimation);
透過 Animation.AnimationListener 可以把多段動畫串接起來,例如旋轉結束後再做平移:
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
TranslateAnimation translateAnimation = new TranslateAnimation(0f, 200f, 0f, 50f);
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(3000);
imageView.startAnimation(translateAnimation);
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
XML 中把多個動畫寫進同一個 <set> 即可;代碼方式則需要借助 AnimationSet:
AnimationSet animationSet = new AnimationSet(true); TranslateAnimation translateAnimation = new TranslateAnimation(0f, 200f, 0f, 50f); translateAnimation.setDuration(3000); animationSet.addAnimation(translateAnimation); // 記得加入動畫集 RotateAnimation rotateAnimation = new RotateAnimation(0f, -30f, 0.5f, 0.5f); rotateAnimation.setDuration(2000); animationSet.addAnimation(rotateAnimation); imageView.startAnimation(animationSet); // 傳入的是 AnimationSet
逐幀動畫的 XML 放在 drawable 目錄下,根元素是 animation-list,子元素為 item:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/loading1" android:duration="200" />
<item android:drawable="@mipmap/loading2" android:duration="200" />
<item android:drawable="@mipmap/loading3" android:duration="200" />
<item android:drawable="@mipmap/loading4" android:duration="200" />
</animation-list>
代碼側拿到 AnimationDrawable 並作為背景設置,然後調用 start():
public class FrameAnimationActivity extends AppCompatActivity {
private AnimationDrawable animationDrawable;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animation);
imageView = (ImageView) findViewById(R.id.image_view);
animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);
imageView.setBackground(animationDrawable);
}
public void startAnimation(View view) {
animationDrawable.start(); // 停止用 stop()
}
}
佈局動畫由 LayoutAnimationController 驅動,用來為某個 layout 或 ViewGroup 中的子控制項批量設置入場動畫。下面的例子讓 ListView 的每個條目依次從右側滑入並淡現。
public class LayoutAnimationActivity extends AppCompatActivity {
private ListView listView;
private List<String> list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_animation);
listView = (ListView) findViewById(R.id.listview);
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("條目 " + i);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
AnimationSet set = new AnimationSet(true);
Animation alpha = new AlphaAnimation(0.0f, 1.0f);
alpha.setDuration(2000);
set.addAnimation(alpha);
Animation translate = new TranslateAnimation(100f, 10f, 0, 0);
translate.setDuration(2000);
set.addAnimation(translate);
LayoutAnimationController controller = new LayoutAnimationController(set);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL); // 依次出現
listView.setLayoutAnimation(controller);
listView.startLayoutAnimation();
}
}
最後要補充的是屬性動畫(Property Animation)。它在 API 11 之後引入,直接操作物件的屬性值,突破了變換動畫「只改畫面不改屬性」的限制,也是現代 Android 動畫開發的首選方案。
Android 自定義 PopupMenu 樣式:顯示圖示與單選按鈕
PopupMenu 是 Android 中相當輕量的一顆元件。跟 PopupWindow 比起來,它的可客製空間有限,但勝在用法簡單直覺。本文要處理的兩個痛點是:如何強制
Android 动画笔记(三):LayoutTransition 与 layoutAnimation
做项目时经常遇到这样的需求:根据条件显示或隐藏某个控件。最直接的办法是调用 View.setVisibility(),功能上没问题,但切换过程生硬,观感欠佳。想要一个平滑
Android 下的 LED 控制(下)——Linux 驅動層解析
這一篇接著聊 Linux 側的實現。上一篇已經確認:JNI 通過打開 /dev/led 這個設備節點,再用 ioctl 來控制 LED 的亮滅與蜂鳴器的鳴響。那麼問題就來
為什麼谷歌推薦用 DialogFragment 創建對話框?源碼層面找答案
寫博客這件事最怕三天打魚兩天曬網。上次寫過一篇 Dialog 詳解,今天接着往下探究:Android 3.0 之後引入的 DialogFragment,究竟是為了解決什麼