Android中並無提供直接作3D翻轉的動畫,因此關於3D翻轉的動畫效果須要咱們本身實現,那麼咱們首先來分析一下Animation 和 Transformation。 java
Animation動畫的主要接口,其中主要定義了動畫的一些屬性好比開始時間,持續時間,是否重複播放等等。而Transformation中則包含一個矩陣和alpha值,矩陣是用來作平移,旋轉和縮放動畫的,而alpha值是用來作alpha動畫的,要實現3D旋轉動畫咱們須要繼承自Animation類來實現,咱們須要重載getTransformation和applyTransformation,在getTransformation中Animation會根據動畫的屬性來產生一系列的差值點,而後將這些差值點傳給applyTransformation,這個函數將根據這些點來生成不一樣的Transformation。下面是具體實現: android
public class Rotate3dAnimation extends Animation { //開始角度 private final float mFromDegrees; //結束角度 private final float mToDegrees; //中心點 private final float mCenterX; private final float mCenterY; private final float mDepthZ; //是否須要扭曲 private final boolean mReverse; //攝像頭 private Camera mCamera; public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; mReverse = reverse; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } //生成Transformation @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; //生成中間角度 float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); camera.save(); if (mReverse) { camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); } else { camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); } camera.rotateY(degrees); //取得變換後的矩陣 camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@android:id/list" android:persistentDrawingCache="animation|scrolling" android:layout_width="match_parent" android:layout_height="match_parent" android:layoutAnimation="@anim/layout_bottom_to_top_slide" /> <ImageView android:id="@+id/picture" android:scaleType="fitCenter" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> </FrameLayout>
public class Transition3d extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener { //照片列表 private ListView mPhotosList; private ViewGroup mContainer; private ImageView mImageView; // 照片的名字,用於顯示在list中 private static final String[] PHOTOS_NAMES = new String[] { "Lyon", "Livermore", "Tahoe Pier", "Lake Tahoe", "Grand Canyon", "Bodie" }; // 資源id private static final int[] PHOTOS_RESOURCES = new int[] { R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4, R.drawable.photo5, R.drawable.photo6 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animations_main_screen); mPhotosList = (ListView) findViewById(android.R.id.list); mImageView = (ImageView) findViewById(R.id.picture); mContainer = (ViewGroup) findViewById(R.id.container); // 準備ListView final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, PHOTOS_NAMES); mPhotosList.setAdapter(adapter); mPhotosList.setOnItemClickListener(this); // 準備ImageView mImageView.setClickable(true); mImageView.setFocusable(true); mImageView.setOnClickListener(this); //設置須要保存緩存 mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE); } /** * Setup a new 3D rotation on the container view. * * @param position the item that was clicked to show a picture, or -1 to show the list * @param start the start angle at which the rotation must begin * @param end the end angle of the rotation */ private void applyRotation(int position, float start, float end) { // 計算中心點 final float centerX = mContainer.getWidth() / 2.0f; final float centerY = mContainer.getHeight() / 2.0f; // Create a new 3D rotation with the supplied parameter // The animation listener is used to trigger the next animation final Rotate3dAnimation rotation = new Rotate3dAnimation(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); } public void onItemClick(AdapterView parent, View v, int position, long id) { // 設置ImageView mImageView.setImageResource(PHOTOS_RESOURCES[position]); applyRotation(position, 0, 90); } //點擊圖像時,返回listview public void onClick(View v) { applyRotation(-1, 180, 90); } /** * This class listens for the end of the first half of the animation. * It then posts a new action that effectively swaps the views when the container * is rotated 90 degrees and thus invisible. */ 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; Rotate3dAnimation rotation; if (mPosition > -1) { //顯示ImageView mPhotosList.setVisibility(View.GONE); mImageView.setVisibility(View.VISIBLE); mImageView.requestFocus(); rotation = new Rotate3dAnimation(90, 180, centerX, centerY, 310.0f, false); } else { //返回listview mImageView.setVisibility(View.GONE); mPhotosList.setVisibility(View.VISIBLE); mPhotosList.requestFocus(); rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false); } rotation.setDuration(500); rotation.setFillAfter(true); rotation.setInterpolator(new DecelerateInterpolator()); //開始動畫 mContainer.startAnimation(rotation); } } }