Ken Burns 特效是视频产品中使用的一种平移和缩放静态图片的
特效
完成这个效果,需要第三方jar,这个可以在资源jar上下载
public class Hack08Activity extends Activity implements Animator.AnimatorListener {
private static final int ANIM_COUNT = 4;
private static final int[] PHOTOS = new int[] { R.drawable.photo1,
R.drawable.photo2, R.drawable.photo3, R.drawable.photo4,
R.drawable.photo5, R.drawable.photo6 };
private FrameLayout mContainer;
private ImageView mView;
private Random mRandom = new Random();
private int mIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContainer = new FrameLayout(this);
mContainer.setLayoutParams(new FrameLayout.LayoutParams(//创建布局容器
FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT));
mView = createNewView();
mContainer.addView(mView);//创建ImageView,并将其添加到布局容器中
setContentView(mContainer);
}
private ImageView createNewView() {
ImageView ret = new ImageView(this);
ret.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
ret.setScaleType(ImageView.ScaleType.FIT_XY);
ret.setImageResource(PHOTOS[mIndex]);//设置要显示的图片,并设置下一个要显示的图片的索引
mIndex = (mIndex + 1 < PHOTOS.length) ? mIndex + 1 : 0;
return ret;
}
@Override
protected void onResume() {
super.onResume();
nextAnimation();
}
private void nextAnimation() {
AnimatorSet anim = new AnimatorSet();
final int index = mRandom.nextInt(ANIM_COUNT);//随机选择动画
switch (index) {
case 0:
anim.playTogether(
ObjectAnimator.ofFloat(mView, "scaleX", 1.5f, 1f),//缩放动画
ObjectAnimator.ofFloat(mView, "scaleY", 1.5f, 1f));
break;
case 1:
anim.playTogether(
ObjectAnimator.ofFloat(mView, "scaleX", 1, 1.5f),
ObjectAnimator.ofFloat(mView, "scaleY", 1, 1.5f));
break;
case 2:
//位移动画
AnimatorProxy.wrap(mView).setScaleX(1.5f);
AnimatorProxy.wrap(mView).setScaleY(1.5f);
anim.playTogether(ObjectAnimator.ofFloat(mView,
"translationY", 80f, 0f));
break;
case 3:
default:
AnimatorProxy.wrap(mView).setScaleX(1.5f);
AnimatorProxy.wrap(mView).setScaleY(1.5f);
anim.playTogether(ObjectAnimator.ofFloat(mView,
"translationX", 0f, 40f));
break;
}
//设置动画持续时间,设置动画监听器为当前Activity,启动动画
anim.setDuration(3000);
anim.addListener(this);
anim.start();
}
@Override
public void onAnimationCancel(Animator arg0) {
}
@Override
public void onAnimationEnd(Animator animator) {
//从布局容器中移除之前的 View,并添加新的 View
mContainer.removeView(mView);
mView = createNewView();
mContainer.addView(mView);
nextAnimation();//开始显示下一个动画
}
@Override
public void onAnimationRepeat(Animator arg0) {
}
@Override
public void onAnimationStart(Animator arg0) {
}
}