ImageView animation inside WindowManager in Android service

I am trying to animate an ImageView created dynamically in a service, but not a way to make it work ... I think the problem is that I am using the window manager and I have to use FrameLayout or something similar and place the ImageView inside, but not I know how to do this programmatically ... In any case, does anyone know how to make this work? Help me please!

 @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);
}
+4
source share
1 answer

I had the same problem, here is the solution: Instead of ImageView you need to add a FrameLayout that has an ImageView.

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
floatingLayout = (FrameLayout) inflater.inflate(R.layout.floating_layout, null);

Save your button to deal with animations later:

mButton = (ImageView) floatingLayout.findViewById(R.id.mButton);

When adding a button, add a layout:

windowManager.addView(floatingLayout, floatingLayoutParams);

Animate!

Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
    rotate.setDuration(1000);
    mButton.startAnimation(rotate);
+7

Source: https://habr.com/ru/post/1546703/


All Articles