在WindowManager中动画ImageView - Android

时间:2014-03-03 11:04:03

标签: java android animation

我想在WindowManager中为ImageView设置动画。但它没有用。 请在下面找到我的代码。在这里,您可以看到我获得了一个窗口管理器系统服务并添加了一个imageview。现在我在窗口管理器中添加imageview后尝试旋转。

它在活动中运作良好。

 

    public class FloatingImageAnimationService extends Service {

        private WindowManager windowManager;
        private static ImageView chatHead;

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }
     @Override 
         public void onCreate() {
             super.onCreate();
             windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

             chatHead = new ImageView(this);

             chatHead.setImageResource(R.drawable.image_small);


             final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                             WindowManager.LayoutParams.WRAP_CONTENT,
                             WindowManager.LayoutParams.WRAP_CONTENT,
                             WindowManager.LayoutParams.TYPE_PHONE,
                             WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                             PixelFormat.TRANSLUCENT);


             params.gravity = Gravity.TOP | Gravity.LEFT;
             params.x = 0;
             params.y = 100;

             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);
         }
    }


请让我知道我需要采取哪些其他步骤才能使其发挥作用。

谢谢你,

Bikash

2 个答案:

答案 0 :(得分:6)

您可以通过添加类似的布局来设置动画。

//..at your onCreate() method
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.image_small);

// Create an additional layout for your animation.
LinearLayout layout = new LinearLayout(this);
layout.addView(chatHead); // And attach your objects 

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                         WindowManager.LayoutParams.WRAP_CONTENT,
                         WindowManager.LayoutParams.WRAP_CONTENT,
                         WindowManager.LayoutParams.TYPE_PHONE,
                         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                         PixelFormat.TRANSLUCENT);


params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;

// Add your layout to the windowManager instead of your object.
windowManager.addView(layout, params); 

//.. Add animations what you want.

当我遇到完全相同的情况时,它对我有用。

答案 1 :(得分:0)

设置此权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />