我正在尝试动画在服务中动态创建的ImageView,但无法让它工作......我认为问题是因为我使用的是Windows管理器,我应该使用FrameLayout或其他东西类似并将ImageView放在里面,但不知道如何以编程方式进行...无论如何,有人知道如何使这个工作?帮助pleaseee!
@Override public void onCreate() {
super.onCreate();
final GestureDetector mGestureDetector = new GestureDetector(getApplicationContext(), new MyGestureDetector());
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.btn_enable_floating);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
100,
107,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
params.x = 0;
params.y = 100;
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
chatHead.setImageResource(R.drawable.btn_enable_floating_hover);
return mGestureDetector.onTouchEvent(event);
case MotionEvent.ACTION_UP:
chatHead.setImageResource(R.drawable.btn_enable_floating);
return mGestureDetector.onTouchEvent(event);
case MotionEvent.ACTION_MOVE:
//params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, params);
return mGestureDetector.onTouchEvent(event);
}
return mGestureDetector.onTouchEvent(event);
}
});
windowManager.addView(chatHead, params);
RotateAnimation ra = new RotateAnimation(
0,
-80,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
chatHead.startAnimation(ra);
}
答案 0 :(得分:7)
我刚遇到同样的问题,这是一个解决方案: 您需要添加具有ImageView的FrameLayout而不是ImageView。
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
floatingLayout = (FrameLayout) inflater.inflate(R.layout.floating_layout, null);
保存按钮以便稍后处理动画:
mButton = (ImageView) floatingLayout.findViewById(R.id.mButton);
添加按钮,添加布局:
windowManager.addView(floatingLayout, floatingLayoutParams);
动画!
Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
rotate.setDuration(1000);
mButton.startAnimation(rotate);