Animate ImageView inside WindowManager - Android

2019-04-16 07:30发布

问题:

I want to animate an ImageView inside a WindowManager. But it is not working. Please find my code below. Here you can see I get a window manager system service and added an imageview. Now I am trying to rotate after adding the imageview inside window manager.

It works fine inside an activity.

 

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


Please let me know what additional steps I need to do to make it work.

Thanks you,

Bikash

回答1:

You can animate by adding a layout something like this.

//..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.

It worked for me when I suffered the exactly same case.



回答2:

set this permission:

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