# Android實現中軸旋轉特效 Android製作別樣的圖片瀏覽器

Android API Demos中有很多非常Nice的例子，這些例子的程式碼都寫的很出色，如果大家把API Demos中的每個例子研究透了，那麼恭喜你已經成為一個真正的Android高手了。這也算是給一些比較迷茫的Android開發者一個指出了一個提升自我能力的方向吧。API Demos中的例子眾多，今天我們就來模仿其中一個3D變換的特效，來實現一種別樣的圖片瀏覽器。

Camera中提供了三種旋轉方法，分別是rotateX()、rotateY()和rotateZ，呼叫這三個方法，並傳入相應的角度，就可以讓檢視圍繞這三個軸進行旋轉，而今天我們要做的中軸旋轉效果其實就是讓檢視圍繞Y軸進行旋轉。使用Camera讓檢視進行旋轉的示意圖，如下所示：

``````
/**
* An animation that rotates the view on the Y axis between two specified angles.
* This animation also adds a translation on the Z axis (depth) to improve the effect.
*/
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;
/**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair
* of X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length
* of the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees the start angle of the 3D rotation
* @param toDegrees the end angle of the 3D rotation
* @param centerX the X center of the 3D rotation
* @param centerY the Y center of the 3D rotation
* @param reverse true if the translation should be reversed, false otherwise
*/
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();
}
@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);
}
}
``````

``````
public class Picture {
/**
* 圖片名稱
*/
private String name;
/**
* 圖片物件的資源
*/
private int resource;
public Picture(String name, int resource) {
this.name = name;
this.resource = resource;
}
public String getName() {
return name;
}
public int getResource() {
return resource;
}
}
``````

``````
public PictureAdapter(Context context, int textViewResourceId, List<Picture> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Picture picture = getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1,
null);
} else {
view = convertView;
}
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
text1.setText(picture.getName());
return view;
}
}
``````

``````
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@ id/pic_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
<ImageView
android:id="@ id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:clickable="true"
android:visibility="gone"
/>
</RelativeLayout>``````

``````
public class MainActivity extends Activity {
/**
* 根佈局
*/
private RelativeLayout layout;
/**
* 用於展示圖片列表的ListView
*/
private ListView picListView;
/**
* 用於展示圖片詳細的ImageView
*/
private ImageView picture;
/**
* 圖片列表的介面卡
*/
/**
* 存放所有圖片的集合
*/
private List<Picture> picList = new ArrayList<Picture>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 對圖片列表資料進行初始化操作
initPics();
layout = (RelativeLayout) findViewById(R.id.layout);
picListView = (ListView) findViewById(R.id.pic_list_view);
picture = (ImageView) findViewById(R.id.picture);
picListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 當點選某一子項時，將ImageView中的圖片設定為相應的資源
picture.setImageResource(picList.get(position).getResource());
// 獲取佈局的中心點位置，作為旋轉的中心點
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 構建3D旋轉動畫物件，旋轉角度為0到90度，這使得ListView將會從可見變為不可見
final Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,
310.0f, true);
// 動畫持續時間500毫秒
rotation.setDuration(500);
// 動畫完成後保持完成的狀態
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 設定動畫的監聽器
rotation.setAnimationListener(new TurnToImageView());
layout.startAnimation(rotation);
}
});
picture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 獲取佈局的中心點位置，作為旋轉的中心點
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 構建3D旋轉動畫物件，旋轉角度為360到270度，這使得ImageView將會從可見變為不可見，並且旋轉的方向是相反的
final Rotate3dAnimation rotation = new Rotate3dAnimation(360, 270, centerX,
centerY, 310.0f, true);
// 動畫持續時間500毫秒
rotation.setDuration(500);
// 動畫完成後保持完成的狀態
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 設定動畫的監聽器
rotation.setAnimationListener(new TurnToListView());
layout.startAnimation(rotation);
}
});
}
/**
* 初始化圖片列表資料。
*/
private void initPics() {
Picture bird = new Picture("Bird", R.drawable.bird);
Picture winter = new Picture("Winter", R.drawable.winter);
Picture autumn = new Picture("Autumn", R.drawable.autumn);
Picture greatWall = new Picture("Great Wall", R.drawable.great_wall);
Picture waterFall = new Picture("Water Fall", R.drawable.water_fall);
}
/**
* 註冊在ListView點選動畫中的動畫監聽器，用於完成ListView的後續動畫。
*
* @author guolin
*/
class TurnToImageView implements AnimationListener {
@Override
public void onAnimationStart(Animation animation) {
}
/**
* 當ListView的動畫完成後，還需要再啟動ImageView的動畫，讓ImageView從不可見變為可見
*/
@Override
public void onAnimationEnd(Animation animation) {
// 獲取佈局的中心點位置，作為旋轉的中心點
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 將ListView隱藏
picListView.setVisibility(View.GONE);
// 將ImageView顯示
picture.setVisibility(View.VISIBLE);
picture.requestFocus();
// 構建3D旋轉動畫物件，旋轉角度為270到360度，這使得ImageView將會從不可見變為可見
final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,
310.0f, false);
// 動畫持續時間500毫秒
rotation.setDuration(500);
// 動畫完成後保持完成的狀態
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
layout.startAnimation(rotation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
/**
* 註冊在ImageView點選動畫中的動畫監聽器，用於完成ImageView的後續動畫。
*
* @author guolin
*/
class TurnToListView implements AnimationListener {
@Override
public void onAnimationStart(Animation animation) {
}
/**
* 當ImageView的動畫完成後，還需要再啟動ListView的動畫，讓ListView從不可見變為可見
*/
@Override
public void onAnimationEnd(Animation animation) {
// 獲取佈局的中心點位置，作為旋轉的中心點
float centerX = layout.getWidth() / 2f;
float centerY = layout.getHeight() / 2f;
// 將ImageView隱藏
picture.setVisibility(View.GONE);
// 將ListView顯示
picListView.setVisibility(View.VISIBLE);
picListView.requestFocus();
// 構建3D旋轉動畫物件，旋轉角度為90到0度，這使得ListView將會從不可見變為可見，從而回到原點
final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
310.0f, false);
// 動畫持續時間500毫秒
rotation.setDuration(500);
// 動畫完成後保持完成的狀態
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
layout.startAnimation(rotation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
} ``````

MainActivity中的程式碼已經有非常詳細的註釋了，這裡我再帶著大家把它的執行流程梳理一遍。首先在onCreate()方法中呼叫了initPics()方法，在這裡對圖片列表中的資料進行初始化。然後獲取佈局中控制元件的例項，並讓列表中的資料在ListView中顯示。接著分別給ListView和ImageView註冊了它們的點選事件。