編輯:關於 Android 程式設計
從 Android 3.0 開始,Google 引入了屬性動畫(Property Animation)框架,讓我們能做出更豐富、更可控的動畫,於是趁這個需求聊一下。
需求是這樣:點擊一個 View 時,把下方隱藏的 View 展開顯示。直覺做法是切換 visibility 從 GONE 到 VISIBLE,但那是瞬間切換,沒有過渡,看起來很生硬。屬性動畫就是更好的方案——既然要動的是「高度」,那就讓高度從 0 平滑增加到目標值。
收起的狀態:

展開的狀態:

點擊箭頭即可在兩者間切換。
三個線性布局堆疊:標題、固定內容、待展開的內容(android:id="@+id/linear_hidden"),最下方是一個帶 onClick 的箭頭列。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ltl.mpiggybank.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#458EFD"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="下拉展開動畫"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#548AEA"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dip"
android:text="昨日收益(元)"
android:textColor="#ffffff"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:textColor="#ffffff"
android:textSize="45sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_hidden"
android:layout_width="match_parent"
android:layout_height="120dip"
android:background="#548AEA"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dip"
android:text="顯示的View"
android:textColor="#ffffff"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:textColor="#ffffff"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#548AEA"
android:gravity="center"
android:onClick="onClick"
android:orientation="vertical">
<ImageView
android:id="@+id/my_iv"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_margin="20dip"
android:src="@drawable/scroll" />
</LinearLayout>
</LinearLayout>
關鍵是算出目標高度。布局裡寫的是 120dp,執行時必須換成像素,否則不同螢幕密度下動畫幅度會跑掉:
mDensity = getResources().getDisplayMetrics().density; mHiddenViewMeasuredHeight = (int) (mDensity * 120 + 0.5);
讓高度在兩個數值之間插值,每次更新就寫回 LayoutParams:
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator arg0) {
int value = (int) arg0.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.height = value;
v.setLayoutParams(layoutParams);
}
});
public class MainActivity extends ActionBarActivity {
private LinearLayout mHiddenLayout;
private float mDensity;
private int mHiddenViewMeasuredHeight;
private ImageView mIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mHiddenLayout = (LinearLayout) findViewById(R.id.linear_hidden);
mIv = (ImageView) findViewById(R.id.my_iv);
mDensity = getResources().getDisplayMetrics().density;
mHiddenViewMeasuredHeight = (int) (mDensity * 120 + 0.5);
}
public void onClick(View v) {
if (mHiddenLayout.getVisibility() == View.GONE) {
animateOpen(mHiddenLayout);
animationIvOpen();
} else {
animateClose(mHiddenLayout);
animationIvClose();
}
}
private void animateOpen(View v) {
v.setVisibility(View.VISIBLE);
createDropAnimator(v, 0, mHiddenViewMeasuredHeight).start();
}
private void animateClose(final View view) {
ValueAnimator animator = createDropAnimator(view, view.getHeight(), 0);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// 收起完成後才設為 GONE,避免高度為 0 卻仍佔位
view.setVisibility(View.GONE);
}
});
animator.start();
}
private ValueAnimator createDropAnimator(final View v, int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator arg0) {
int value = (int) arg0.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.height = value;
v.setLayoutParams(layoutParams);
}
});
return animator;
}
private void animationIvOpen() {
RotateAnimation animation = new RotateAnimation(0, 180,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setFillAfter(true);
animation.setDuration(100);
mIv.startAnimation(animation);
}
private void animationIvClose() {
RotateAnimation animation = new RotateAnimation(180, 0,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setFillAfter(true);
animation.setDuration(100);
mIv.startAnimation(animation);
}
}
兩個細節值得留意:展開前要先設 VISIBLE 再從 0 開始動畫,收起則要在動畫結束後才設 GONE,順序顛倒就會出現閃跳。另外每次改 LayoutParams.height 都會觸發重新測量與布局,動畫時間別拉太長;若列表中有大量這類效果,改用 ViewAnimationUtils.createCircularReveal 或 Transition 框架會更省效能。
《Android Studio 實用指南》8.20 提煉接口
提煉接口(Extract Interface)是一項重構手法:從一個已存在的類中挑選若干方法,把它們提取到一個獨立的接口里,讓這個類去實現該接口。對於需要抽出統一契約、方
Android 滑動效果的由來與核心知識解析
相比 Android 2.x 時代流行的長按、點擊操作,滑動無疑帶來了更好的使用者體驗。因此從 Android 4.X 開始,越來越多的滑動操作效果應運而生,大量第三方應
React Native 實戰:用 Navigator 實現頁面跳轉
主界面開發 上一節已完成首頁的開發,這節繼續完成主界面,也就是添加底部「首頁」與「我的」兩個 tabbar。 在 js 文件夾下新建 MainScene.js: impo
RecyclerView 下拉刷新與加載更多:一次講透封裝思路與踩坑記錄
「下拉刷新 + 上滑加載更多」幾乎是資訊類應用的標配交互。聽起來簡單,真正動手時卻要同時應對 RecyclerView、SwipeRefreshLayout、滑動監聽、