【android任意view移动缩放至消失到任何位置的通用动画】要求将界面中显示的任意一个view(已经Measure好了)缩放移动到任意位置直至消失
可以用图片这样描述:
文章图片
实现思路:使用系统缩放动画ScaleAnimation进行缩放和移动,缩放倍数和移动距离根据移动的view的xy宽高和目标xy计算,任意位置的动画将受到不同的布局限制,但activity有个windows里面包含decorView,decorView里面又包含rootview,decorView不包含通知栏,但包含通知栏的高度,所以选择使用rootview,即xml布局的上一层FrameLayout布局,你可以在rootview(通过getwindows().getdecorview.findViewById(android.R.id.content)或 activity.getWindow().getDecorView().getRootView();
获得)添加移动的view,便可以在任意位置移动
代码实现:
public static void animToTagOnWindows (Activity activity,View tagView,View toView,float scale) {
int[] toXY = new int[2];
toView.getLocationOnScreen(toXY);
int centerX = (int) (toXY[0] + toView.getMeasuredWidth()/2f);
int centerY = (int) (toXY[1] + toView.getMeasuredHeight()/2f);
animToTagOnWindows(activity,tagView,centerX,centerY,scale);
}public static void animToTagOnWindows (Activity activity, View tagView, int toCenterX, int toCenterY, float scale) {
int[] winXY = new int[2];
tagView.getLocationOnScreen(winXY);
float toX = tagView.getMeasuredWidth()*scale;
float toY = tagView.getMeasuredHeight()*scale;
float pivotX = (toCenterX-winXY[0])*1f/tagView.getMeasuredWidth();
float pivotY = (toCenterY-winXY[1])*1f/tagView.getMeasuredHeight();
ScaleAnimation scaleAnimation =new ScaleAnimation(1.0f,toX, 1f, toY, Animation.RELATIVE_TO_SELF,pivotX, Animation.RELATIVE_TO_SELF, pivotY);
final ImageView tempMoveView = new ImageView(activity);
tempMoveView.setScaleType(ImageView.ScaleType.FIT_XY);
Bitmap tempBm = getViewBitmap(tagView);
tempMoveView.setImageBitmap(tempBm);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(tagView.getMeasuredWidth(), tagView.getMeasuredHeight());
params.setMargins(winXY[0],winXY[1],winXY[0] +tagView.getMeasuredWidth(),winXY[1]+tagView.getMeasuredHeight());
tempMoveView.setLayoutParams(params);
final FrameLayout frameLayout = (FrameLayout) activity.getWindow().getDecorView().getRootView();
frameLayout.addView(tempMoveView);
scaleAnimation.setDuration(1000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}@Override
public void onAnimationEnd(Animation animation) {
//移除临时显示动画的view
frameLayout.removeView(tempMoveView);
}@Override
public void onAnimationRepeat(Animation animation) {}
});
tempMoveView.startAnimation(scaleAnimation);
}
博客出处
最终效果如下:
文章图片