76 lines
2.7 KiB
Java
76 lines
2.7 KiB
Java
package com.xscm.moduleutil.widget;
|
|
|
|
import android.animation.Animator;
|
|
import android.animation.AnimatorSet;
|
|
import android.animation.ObjectAnimator;
|
|
import android.animation.PropertyValuesHolder;
|
|
import android.app.Activity;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.Window;
|
|
import android.widget.ImageView;
|
|
|
|
import com.blankj.utilcode.util.ScreenUtils;
|
|
import com.xscm.moduleutil.utils.GiftAnimatorUtil;
|
|
import com.xscm.moduleutil.utils.ImageUtils;
|
|
|
|
public class WheatGiftAnim {
|
|
|
|
public static void addGift(View targetView, String imageUrl) {
|
|
final int[] location = new int[2];
|
|
targetView.getLocationOnScreen(location);
|
|
ImageView imageView = new ImageView(targetView.getContext());
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(targetView.getWidth(), targetView.getHeight());
|
|
imageView.setLayoutParams(params);
|
|
ImageUtils.loadImageView(imageUrl, imageView);
|
|
attach2Activity((Activity) targetView.getContext(), imageView);
|
|
ObjectAnimator tada = GiftAnimatorUtil.tada(imageView);
|
|
PropertyValuesHolder objectAnimatorY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, ScreenUtils.getScreenHeight(), location[1]);
|
|
PropertyValuesHolder objectAnimatorX = PropertyValuesHolder.ofFloat(View.TRANSLATION_X, ScreenUtils.getScreenWidth() / 2f, location[0]);
|
|
ObjectAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView, objectAnimatorY, objectAnimatorX).
|
|
setDuration(1200);
|
|
AnimatorSet set = new AnimatorSet();
|
|
set.addListener(new Animator.AnimatorListener() {
|
|
@Override
|
|
public void onAnimationStart(Animator animator) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationEnd(Animator animator) {
|
|
removeFromActivity(imageView);
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationCancel(Animator animator) {
|
|
removeFromActivity(imageView);
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationRepeat(Animator animator) {
|
|
|
|
}
|
|
});
|
|
set.playSequentially(valueAnimator, tada);
|
|
set.start();
|
|
}
|
|
|
|
/**
|
|
* 将创建的ExplosionField添加到Activity上
|
|
*/
|
|
private static void attach2Activity(Activity activity, View view) {
|
|
ViewGroup rootView = activity.findViewById(Window.ID_ANDROID_CONTENT);
|
|
rootView.addView(view);
|
|
}
|
|
|
|
/**
|
|
* 将ExplosionField从Activity上移除
|
|
*
|
|
* @param imageView
|
|
*/
|
|
private static void removeFromActivity(ImageView imageView) {
|
|
ViewGroup rootView = ((Activity) imageView.getContext()).findViewById(Window.ID_ANDROID_CONTENT);
|
|
rootView.removeView(imageView);
|
|
}
|
|
}
|