最近在做一個類似於購物的動畫。先上最終的效果的Demo。
public void startBuyAnim(final View view)
{
int start_location[] = new int[2];
view.getLocationInWindow(start_location);
ImageView startView = new ImageView(context);
startView.setBackgroundColor(Color.RED);
startView.setLayoutParams(new LayoutParams(context.getResources().getDimensionPixelSize(R.dimen.red_point_size),
context.getResources().getDimensionPixelSize(R.dimen.red_point_size)));
Item item = new Item();
item.view = startView;
viewHolder.add(item);
animLayout.addView(startView);
// 目的位子
int endX = MainActivity.END_LOCATION[0];
int endY = MainActivity.END_LOCATION[1];
TranslateAnimation translateAnimationX = new TranslateAnimation(start_location[0], endX, 0.0f, 0.0f);
translateAnimationX.setInterpolator(new LinearInterpolator());
translateAnimationX.setRepeatCount(0);
translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0.0f, 0.0f, start_location[1], endY);
translateAnimationY.setInterpolator(new Interpolator()
{
@Override
public float getInterpolation(float input)
{
return ((1.5f * (input - 0.16666667f)) * (input - 0.16666667f)) - 0.041666668f;
}
});
translateAnimationY.setRepeatCount(0);
translateAnimationX.setFillAfter(true);
// ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.05f, 1,
// 0.05f, Animation.RELATIVE_TO_SELF, 0,
// Animation.RELATIVE_TO_SELF, 0);
// scaleAnimation.setRepeatCount(0);
// scaleAnimation.setFillAfter(true);
AnimationSet set = new AnimationSet(false);
set.setFillAfter(false);
// set.addAnimation(scaleAnimation);
set.addAnimation(translateAnimationX);
set.addAnimation(translateAnimationY);
set.setDuration(600);
startView.startAnimation(set);
set.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
AnimUtil.this.handler.post(new Runnable()
{
@Override
public void run()
{
AnimUtil.this.removeView();
}
});
}
});
}
private List<Item> viewHolder;
private class Item
{
public View view;
private Item()
{
}
}
private void removeView()
{
if (this.viewHolder != null && this.viewHolder.size() > 0)
{
Item item0 = this.viewHolder.get(0);
item0.view.setVisibility(View.GONE);
this.viewHolder.remove(item0);
this.animLayout.removeView(item0.view);
}
}
}
下載鏈接:http://download.csdn.net/detail/a38017032/9424067