WindowManager中的imageView动画在android 4.xx中不起作用
嗨,我需要一个漂浮的气泡在我的活动之上,我决定为此使用一个服务。除了在安卓4.xx设备中,imageView没有被设置动画外,一切都很好。我已将侦听器添加到动画中,动画将运行(它将启动、重复…并且值也会更改),但imageView不会移动。它在安卓5.x设备上运行良好,但在安卓4.x设备上运行不好。有人知道为什么吗 代码如下:WindowManager中的imageView动画在android 4.xx中不起作用,android,android-animation,window-managers,Android,Android Animation,Window Managers,嗨,我需要一个漂浮的气泡在我的活动之上,我决定为此使用一个服务。除了在安卓4.xx设备中,imageView没有被设置动画外,一切都很好。我已将侦听器添加到动画中,动画将运行(它将启动、重复…并且值也会更改),但imageView不会移动。它在安卓5.x设备上运行良好,但在安卓4.x设备上运行不好。有人知道为什么吗 代码如下: public class SellFloatingBubbleService extends Service { private WindowManager window
public class SellFloatingBubbleService extends Service {
private WindowManager windowManager;
private ImageView imageView;
@Override
public void onCreate() {
super.onCreate();
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.tabbar_tooltip_v3);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
layoutParams.y = (int) TypedValue.applyDimension(TypedValue
.COMPLEX_UNIT_PX, getResources().getDimensionPixelOffset(R.dimen
.sell_button_height), getResources().getDisplayMetrics());
windowManager.addView(imageView, layoutParams);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
stopSelf();
return true;
}
});
/**
* When these animations will end they will revert and then play endlessly.
*/
AnimatorSet bouncer = new AnimatorSet();
ObjectAnimator animX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.8f);
animX.setRepeatMode(ValueAnimator.REVERSE);
animX.setRepeatMode(-1);
ObjectAnimator animY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.8f);
animY.setRepeatMode(ValueAnimator.REVERSE);
animY.setRepeatCount(-1);
bouncer.playTogether(animX, animY);
bouncer.setDuration(1000);
bouncer.setInterpolator(new AccelerateDecelerateInterpolator());
bouncer.start();
}
@Override
public void onDestroy() {
imageView.setVisibility(View.GONE);
windowManager.removeView(imageView);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
public class SellFloatingBubbleService extends Service {
public static final int ANIMATION_DURATION = 500;
private WindowManager windowManager;
private ImageView imageView;
private int animationDistance;
private LinearLayout layout;
private static SellFloatingBubbleService instance = null;
public static boolean isInstanceCreated() {
return instance != null;
}
@Override
public void onCreate() {
super.onCreate();
BusProvider.getInstance().post(new BubbleWillBeShownEvent());
instance = this;
/**
* Animations do not work in Android 4.XX if view is not added inside a view group
*/
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
imageView = new ImageView(this);
layout.addView(imageView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setImageResource(R.drawable.tabbar_tooltip_v);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
animationDistance = (int) getResources().getDimension(R.dimen.sell_bubble_animation_height);
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
(int) getResources().getDimension(R.dimen.sell_bubble_window_height),
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_TOUCH_MODAL, //touch events are transmitted to windows below
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
layoutParams.windowAnimations = android.R.style.Animation_Toast;
int sellButtonHeight = getResources().getDimensionPixelOffset(R.dimen
.sell_button_height);
layoutParams.y = sellButtonHeight / 2;
windowManager.addView(layout, layoutParams);
createAndStartAnimation();
}
private void createAndStartAnimation() {
/**
* When these animations will end they will revert and then play endlessly.
*/
float startValue = imageView.getY();
float endValue = imageView.getY() - animationDistance;
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animYUp = ObjectAnimator.ofFloat(imageView, "y", startValue,
endValue);
animYUp.setRepeatMode(ValueAnimator.REVERSE);
animYUp.setRepeatCount(ValueAnimator.INFINITE);
animYUp.setDuration(ANIMATION_DURATION);
animYUp.setInterpolator(new DecelerateInterpolator()); //decelerate interpolator for up
ObjectAnimator animYDown = ObjectAnimator.ofFloat(imageView, "y", endValue,
startValue);
animYDown.setRepeatCount(ValueAnimator.INFINITE);
animYDown.setDuration(ANIMATION_DURATION);
animYDown.setInterpolator(new AccelerateInterpolator()); //accelerate interpolator for down
animatorSet.play(animYDown).after(animYUp);
animatorSet.start();
}
@Override
public void onDestroy() {
BusProvider.getInstance().post(new BubbleWillBeHiddenEvent());
instance = null;
imageView.setVisibility(View.INVISIBLE);
windowManager.removeView(layout);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
我曾在另一个视图上尝试过相同的动画,但没有在服务中尝试过,效果很好。我还尝试用旧的动画替换ObjectAnimator,问题仍然是一样的
代码有问题吗?将ImageView放入LinearLayout中修复了ImageView未设置动画的问题。 代码如下:
public class SellFloatingBubbleService extends Service {
private WindowManager windowManager;
private ImageView imageView;
@Override
public void onCreate() {
super.onCreate();
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.tabbar_tooltip_v3);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
layoutParams.y = (int) TypedValue.applyDimension(TypedValue
.COMPLEX_UNIT_PX, getResources().getDimensionPixelOffset(R.dimen
.sell_button_height), getResources().getDisplayMetrics());
windowManager.addView(imageView, layoutParams);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
stopSelf();
return true;
}
});
/**
* When these animations will end they will revert and then play endlessly.
*/
AnimatorSet bouncer = new AnimatorSet();
ObjectAnimator animX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.8f);
animX.setRepeatMode(ValueAnimator.REVERSE);
animX.setRepeatMode(-1);
ObjectAnimator animY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.8f);
animY.setRepeatMode(ValueAnimator.REVERSE);
animY.setRepeatCount(-1);
bouncer.playTogether(animX, animY);
bouncer.setDuration(1000);
bouncer.setInterpolator(new AccelerateDecelerateInterpolator());
bouncer.start();
}
@Override
public void onDestroy() {
imageView.setVisibility(View.GONE);
windowManager.removeView(imageView);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
public class SellFloatingBubbleService extends Service {
public static final int ANIMATION_DURATION = 500;
private WindowManager windowManager;
private ImageView imageView;
private int animationDistance;
private LinearLayout layout;
private static SellFloatingBubbleService instance = null;
public static boolean isInstanceCreated() {
return instance != null;
}
@Override
public void onCreate() {
super.onCreate();
BusProvider.getInstance().post(new BubbleWillBeShownEvent());
instance = this;
/**
* Animations do not work in Android 4.XX if view is not added inside a view group
*/
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
imageView = new ImageView(this);
layout.addView(imageView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setImageResource(R.drawable.tabbar_tooltip_v);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
animationDistance = (int) getResources().getDimension(R.dimen.sell_bubble_animation_height);
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
(int) getResources().getDimension(R.dimen.sell_bubble_window_height),
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_TOUCH_MODAL, //touch events are transmitted to windows below
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
layoutParams.windowAnimations = android.R.style.Animation_Toast;
int sellButtonHeight = getResources().getDimensionPixelOffset(R.dimen
.sell_button_height);
layoutParams.y = sellButtonHeight / 2;
windowManager.addView(layout, layoutParams);
createAndStartAnimation();
}
private void createAndStartAnimation() {
/**
* When these animations will end they will revert and then play endlessly.
*/
float startValue = imageView.getY();
float endValue = imageView.getY() - animationDistance;
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animYUp = ObjectAnimator.ofFloat(imageView, "y", startValue,
endValue);
animYUp.setRepeatMode(ValueAnimator.REVERSE);
animYUp.setRepeatCount(ValueAnimator.INFINITE);
animYUp.setDuration(ANIMATION_DURATION);
animYUp.setInterpolator(new DecelerateInterpolator()); //decelerate interpolator for up
ObjectAnimator animYDown = ObjectAnimator.ofFloat(imageView, "y", endValue,
startValue);
animYDown.setRepeatCount(ValueAnimator.INFINITE);
animYDown.setDuration(ANIMATION_DURATION);
animYDown.setInterpolator(new AccelerateInterpolator()); //accelerate interpolator for down
animatorSet.play(animYDown).after(animYUp);
animatorSet.start();
}
@Override
public void onDestroy() {
BusProvider.getInstance().post(new BubbleWillBeHiddenEvent());
instance = null;
imageView.setVisibility(View.INVISIBLE);
windowManager.removeView(layout);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}