Activity上有ListView,点击ListView的Item会弹出dialog,点击dialog上的+号演示一个小球从+号飞到Activity底部的购物车的动画,想了好几天无法实现这个动画,要么这个动画是在dialog的后面,要么这个动画只能在dialog的范围内,求大神赐教!谢谢!!
下面贴我的代码
public class MainActivity extends Activity implements OnClickListener {
private Button bt;
private TextView cart;
private Button add;
private ImageView photoImageView;
private Dialog selectDialog;
private int[] foodAddLocation = new int[2];
private int[] cartLocation = new int[2];
private ViewGroup animLayout;//动画层
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(this);
cart = (TextView) findViewById(R.id.cart);
}
public void showSelectDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
ImageView close = (ImageView)view.findViewById(R.id.select_close);
close.setOnClickListener(this);
add = (Button)view.findViewById(R.id.add);//TODO
add.setOnClickListener(this);
TextView nameView = (TextView)view.findViewById(R.id.food_name);
nameView.setText("哈哈哈");
photoImageView = (ImageView)view.findViewById(R.id.photo);
photoImageView.setImageResource(R.drawable.ic_food_none);
selectDialog = new Dialog(this, R.style.transparentFrameWindowStyle);
selectDialog.setContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Window window = selectDialog.getWindow();
WindowManager windowManager = this.getWindowManager();
// 设置显示动画
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;
wl.y = 0;
// 设置显示位置
selectDialog.onWindowAttributesChanged(wl);
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int)(display.getWidth()*0.8); //设置宽度
selectDialog.getWindow().setAttributes(lp);
// 设置点击外围关闭
selectDialog.setCanceledOnTouchOutside(true);
selectDialog.show();
}
private void addToCartAnimation(final ImageView ball, boolean isDialog) {
animLayout = null;
animLayout = createAnimLayout(isDialog);
final View view = addViewToAnimLayout(animLayout, ball, foodAddLocation);
cart.getLocationInWindow(cartLocation);
// 计算位移
int endX = 0 - foodAddLocation[0] + cart.getWidth();
int endY = cartLocation[1] - foodAddLocation[1] - cart.getHeight()*3;
TranslateAnimation translateAnimationX = new TranslateAnimation(-cart.getHeight(),
endX+cart.getHeight(), 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());
translateAnimationX.setRepeatCount(0);
translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
-cart.getHeight()*3, endY-cart.getHeight());
translateAnimationY.setInterpolator(new AccelerateInterpolator());
translateAnimationY.setRepeatCount(0);
translateAnimationY.setFillAfter(true);
AnimationSet set = new AnimationSet(false);
set.setFillAfter(false);
set.addAnimation(translateAnimationY);
set.addAnimation(translateAnimationX);
set.setDuration(300);
view.startAnimation(set);
set.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ball.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ball.setVisibility(View.GONE);
}
});
}
private ViewGroup createAnimLayout(boolean isDialog) {
Window window;
if(isDialog){
window = selectDialog.getWindow();
}else{
window = this.getWindow();
}
ViewGroup rootView = (ViewGroup) window.getDecorView();
LinearLayout animLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
animLayout.setLayoutParams(lp);
animLayout.setId(Integer.MAX_VALUE);
animLayout.setBackgroundResource(android.R.color.transparent);
rootView.addView(animLayout);
return animLayout;
}
private View addViewToAnimLayout(final ViewGroup animLayout, final View ball,
int[] foodAddLocation) {
int x = foodAddLocation[0];
int y = foodAddLocation[1];
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin = x;
lp.topMargin = y;
ball.setLayoutParams(lp);
animLayout.addView(ball);
return ball;
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt:
showSelectDialog();
break;
case R.id.add:
add.getLocationInWindow(foodAddLocation);
ImageView ball = new ImageView(this);
ball.setImageResource(R.drawable.badge_ifaux);
addToCartAnimation(ball, true);
break;
default:
break;
}
}
}