# 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注册了它们的点击事件。