/*
* 这个demo演示了视图之间切换的动画。
* 1.layoutAnimation用来为layout或者Viewgroup的子View添加动画效果。
* 创建layoutAnimation的xml文件:
* delay:表示子View出现的时间间隔;animationOrder:子View出现的顺序;animation:子View出现的动画。
* 在本例中为ListView添加了layoutAnimation="@anim/layoutanimation"动画,子view从左至右移进
* 并从下至上依次添加。
*/
public class MainActivity extends Activity implements OnItemClickListener,
OnClickListener {
private ViewGroup mContainer;
private ListView mList;
private ImageView mImage;
private boolean clickImage = false;
private static final String[] PHOTOS_NAMES = new String[] { "Lyon",
"Livermore", "Tahoe Pier", "Lake Tahoe", "Grand Canyon", "Bodie" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (ViewGroup) findViewById(R.id.container);
mList = (ListView) findViewById(R.id.list);
mImage = (ImageView) findViewById(R.id.imageView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PHOTOS_NAMES);
mList.setAdapter(adapter);
mList.setOnItemClickListener(this);
mImage.setImageResource(R.drawable.gallery_photo_6);
mImage.setClickable(true);
mImage.setFocusable(true);
mImage.setOnClickListener(this);
// 因为我们缓存一个较大的视图,所以在两个动画之间我们需要保留缓存
mContainer
.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
}
/*
* 应用旋转动画
*/
private void applyRotation(float start, float end) {
float centerX = mContainer.getWidth() / 2;
float centerY = mContainer.getHeight() / 2;
// 定义3d旋转动画
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());
mContainer.startAnimation(rotation);
}
private class DisplayNextView implements Animation.AnimationListener {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 将Runable对象发送到主线程队列,用来刷新主线程
mContainer.post(new SwapViews());
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
/*
* 视图切换动画,从动画的后半部分开始
*/
private class SwapViews implements Runnable {
@Override
public void run() {
float centerX = mContainer.getWidth() / 2;
float centerY = mContainer.getHeight() / 2;
Rotate3dAnimation rotation;
if (!clickImage) {
mList.setVisibility(View.GONE);
mImage.setVisibility(View.VISIBLE);
mImage.requestFocus();
rotation = new Rotate3dAnimation(90, 180, centerX, centerY,
310.0f, false);
}else{
mList.setVisibility(View.VISIBLE);
mImage.setVisibility(View.GONE);
mList.requestFocus();
clickImage=false;
rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
applyRotation(0, 90);
}
@Override
public void onClick(View v) {
clickImage=true;
//System.out.println("click");
applyRotation(180, 90);
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_world"
android:persistentDrawingCache="animation|scrolling"
android:layoutAnimation="@anim/layoutanimation"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:visibility="gone"
/>
</FrameLayout>
layoutanimation.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animationOrder="reverse"
android:animation="@anim/slide_right" />
slide_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="1000"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
Rotate3dAnimation.java
/**
* 创建一个可以绕y轴旋转的动画效果
*
* @param fromDegrees 3D旋转开始的角度
* @param toDegrees 结束角度
* @param centerX 旋转中心的X坐标
* @param centerY 旋转中心的Y坐标
* @param depthZ 在Z轴方向拉伸的距离
* @param reverse 是否反向旋转
*/
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);
}
}