先上效果图:
先写Layout文件:
这3个ImageView都在屏幕的底部,clone_item需要固定在球弹起的最高位置:
初始化这3个imageView:
sat_main = (ImageView)findViewById(R.id.sat_main);
final ImageView itemView = (ImageView)findViewById(R.id.sat_item);
final ImageView cloneView = (ImageView)findViewById(R.id.clone_item);
cloneView.setImageResource(R.drawable.searchable_web);
itemView.setImageResource(R.drawable.searchable_web);
itemView.setVisibility(View.GONE);
初始化cloneView的位置:
//这个是使cloneview固定在leftmargin x bottomMargin y的地方
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams) cloneView.getLayoutParams();
layoutParams.bottomMargin = Math.abs(y);
layoutParams.leftMargin = Math.abs(x);
cloneView.setLayoutParams(layoutParams);
点击按钮时候,按钮本身会旋转:
用以下方法得到球的最终位置:x坐标是distance*cos(角度),y是distance*sin(角度)
//取得distance的cos(degree)
public static int getTranslateX(float degree, int distance) {
return Double.valueOf(distance * Math.cos(Math.toRadians(degree))).intValue();
}
public static int getTranslateY(float degree, int distance){
return Double.valueOf(-1 * distance * Math.sin(Math.toRadians(degree))).intValue();
}
然后球弹起的动画:
public static Animation createItemOutAnimation(Context context, int index, long expandDuration, int x, int y){
AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
long alphaDuration = 60;
if(expandDuration < 60){
alphaDuration = expandDuration / 4;
}
alphaAnimation.setDuration(alphaDuration);
alphaAnimation.setStartOffset(0);
//x和y是球弹到最高点的坐标
TranslateAnimation translate = new TranslateAnimation(0, x, 0, y);
translate.setStartOffset(0);
translate.setDuration(expandDuration);
//OvershootInterpolator:表示向前甩一定值后再回到原来位置。
translate.setInterpolator(context, R.anim.sat_item_overshoot_interpolator);
RotateAnimation rotate = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//AccelerateInterpolator:动画从开始到结束,变化率是一个加速的过程。
//DecelerateInterpolator:动画从开始到结束,变化率是一个减速的过程
rotate.setInterpolator(context, R.anim.sat_item_out_rotate_interpolator);
long duration = 100;
if(expandDuration <= 150){
duration = expandDuration / 3;
}
rotate.setDuration(expandDuration-duration);
rotate.setStartOffset(duration);
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(false);
animationSet.setFillBefore(true);
animationSet.setFillEnabled(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotate);
animationSet.addAnimation(translate);
animationSet.setStartOffset(30*index);
return animationSet;
}
这个动画弹到最高点后,我们得使itemview gone掉,cloneview visible