package
com.example.aexh_11_transition3d;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.View.OnClickListener;
import
android.view.animation.AccelerateInterpolator;
import
android.view.animation.Animation;
import
android.view.animation.DecelerateInterpolator;
import
android.view.animation.Animation.AnimationListener;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity
implements
OnClickListener
{
/**
* 仿照系统的范例,修改成点击图片播放动画更新图片。
* apiDemos范例: com.example.android.apis.animation
* Transition3d
*/
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 };
private
ImageView mImageview;
private
ViewGroup mLayout;
private
int
mPosition;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageview = (ImageView) findViewById(R.id.imageView1);
mImageview.setOnClickListener(
this
);
mLayout = (ViewGroup) findViewById(R.id.image_layout);
/**
* 下面这句应该是用来压缩大图的,没加上这句运行正常
*/
mLayout.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
}
@Override
public
void
onClick(View v)
{
if
(v.getId() == R.id.imageView1)
{
mPosition++;
applyRotation(
0
,
90
);
mImageview.setImageResource(PHOTOS_RESOURCES[mPosition %
6
]);
}
}
/**
* 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(
float
start,
float
end)
{
final
float
centerX = mLayout.getWidth() /
2
.0f;
final
float
centerY = mLayout.getHeight() /
2
.0f;
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
AnimationListener()
{
@Override
public
void
onAnimationStart(Animation animation)
{
}
@Override
public
void
onAnimationRepeat(Animation animation)
{
}
@Override
public
void
onAnimationEnd(Animation animation)
{
mLayout.post(mAaction);
}
});
mLayout.startAnimation(rotation);
}
Runnable mAaction =
new
Runnable()
{
@Override
public
void
run()
{
final
float
centerX = mLayout.getWidth() /
2
.0f;
final
float
centerY = mLayout.getHeight() /
2
.0f;
Rotate3dAnimation rotation;
rotation =
new
Rotate3dAnimation(
90
,
180
, centerX, centerY,
310
.0f,
false
);
rotation.setDuration(
500
);
rotation.setFillAfter(
true
);
rotation.setInterpolator(
new
DecelerateInterpolator());
mLayout.startAnimation(rotation);
}
};
}