編輯:關於 Android 程式設計
本文分享一個有趣的動畫效果:圖片的翻轉。這裡示範的是單張圖片的翻轉,若有多張圖片,可以參考 APIDemo 中的例子,加上一個 ArrayAdapter 即可,也可以自行發揮修改,實現自己想要的翻轉效果。以下程式碼基本上可以直接運行。
先在 FrameLayout 中放一個 ImageView:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rotate" android:textSize="50px" android:layout_x="150px" android:layout_y="30px" android:src="@drawable/ro"> </ImageView> </FrameLayout>
public class TestRotate extends Activity implements OnClickListener{
private ImageView imageview;
private ViewGroup mContainer;
/**
* 這個變量設置的是圖片,如果是多張圖片,可以用數組,如
* private static final int[] IMAGE = new int[]{
* R.drawable.ro,
* R.drawable.icon
* };
*/
private static final int IMAGE = R.drawable.ro;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview = (ImageView) findViewById(R.id.image);
mContainer = (ViewGroup) findViewById(R.id.container);
// 設置最新顯示的圖片,如果是數組,可以寫成 IMAGE[int]
imageview.setImageResource(IMAGE);
// 設置 ImageView 的 OnClickListener
imageview.setClickable(true);
imageview.setFocusable(true);
imageview.setOnClickListener(this);
}
private void applyRotation(int position, float start, float end) {
// Find the center of the container
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
final Rotate3d rotation =
new Rotate3d(start, end, centerX, centerY, 310.0f, true);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(position));
mContainer.startAnimation(rotation);
}
@Override
public void onClick(View v) {
// 調用這個方法就是翻轉圖片
// 參數:第一個是位置,第二是開始的角度,第三是結束的角度
// 如果要回到上一張,把第一個參數設置成 -1 就行了
applyRotation(0,0,90);
}
private final class DisplayNextView implements Animation.AnimationListener {
private final int mPosition;
private DisplayNextView(int position) {
mPosition = position;
}
public void onAnimationStart(Animation animation) { }
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews(mPosition));
}
public void onAnimationRepeat(Animation animation) { }
}
/**
* This class is responsible for swapping the views and start the second
* half of the animation.
*/
private final class SwapViews implements Runnable {
private final int mPosition;
public SwapViews(int position) {
mPosition = position;
}
public void run() {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
Rotate3d rotation;
if (mPosition > -1) {
imageview.setVisibility(View.VISIBLE);
imageview.requestFocus();
rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);
} else {
imageview.setVisibility(View.GONE);
rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
}
}
}
整個翻轉效果其實分為兩個半程:前半程由 applyRotation 觸發(0 度轉到 90 度,加速),動畫結束後透過 DisplayNextView 呼叫 SwapViews 執行後半程(90 度轉到 180 度,減速),中間搭配圖片的顯示與隱藏切換,就呈現出自然的翻牌效果。注意此範例依賴自定義的 Rotate3d 動畫類(可參考 APIDemo 中的實作)。
Android ScrollView 加入左右箭頭提示
需求 當 ScrollView 內容超過螢幕寬度時,使用者不一定知道還可以滑動。可以在左右兩側加入箭頭提示,並依滾動位置顯示或隱藏。 作法 自訂 ScrollView
Android 通訊錄開發:ListView 刪除功能的實作脈路
刪除列表項的核心觀念 在通訊錄或任何 ListView 應用中,刪除功能的本質是更新資料來源,再通知 Adapter 刷新畫面。由於 Android 要求 UI 更新在主
Android 自定義 ViewGroup 佈局:onMeasure 與 onLayout 的實作要點
當系統內建的 LinearLayout、RelativeLayout 都無法滿足排列需求時,就得自己繼承 ViewGroup 寫一個佈局。自定義 ViewGroup 的核
深入搞懂 Activity 的四大啟動模式
最近有幾位朋友留言,想讓我談談對 Activity 啟動模式的理解。我始終認為,對某個知識點的理解必須親手操作才能印象深刻,所以今天結合案例來全面梳理一下。此前受「區長」