Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖片翻轉動畫簡易實現代碼

編輯:關於 Android 程式設計

本文分享一個有趣的動畫效果:圖片的翻轉。這裡示範的是單張圖片的翻轉,若有多張圖片,可以參考 APIDemo 中的例子,加上一個 ArrayAdapter 即可,也可以自行發揮修改,實現自己想要的翻轉效果。以下程式碼基本上可以直接運行。

布局檔 main.xml

先在 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>

Activity 程式碼

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 中的實作)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved