Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate ImageView inside WindowManager in a Android Service

I'm trying to animate an ImageView created dynamically in a service, but no way to get it working... I think the problem is because I'm using a windows manager and I should have to use a FrameLayout or something similar and place the ImageView inside, but no idea how to do it programatically... Anyway, someone know how to get this working? Help 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);
}
like image 969
dark_pajuva Avatar asked Jul 01 '14 14:07

dark_pajuva


1 Answers

I've just had the same problem, here is a solution: Instead of an ImageView, you need to add a FrameLayout, that has your 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);

Instaed of 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);
like image 154
Dmitry Mina Avatar answered Oct 20 '22 12:10

Dmitry Mina