Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate view added on WindowManager

I have a view (customView) added to the WindowManager.

WindowManager mWm = (WindowManager)activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, 0, PixelFormat.TRANSPARENT);
mWl.dimAmount = 0.0f;
mWm.addView(customView, mWl);

Inside the custom view, I will call a translate animation when close button is pressed.

//// This is the handler for the animation ////

final Handler translateHandler = new Handler();

final Runnable mtranslateUp = new Runnable() {
    public void run() {
        Log.v("TEST","mtranslateUp Runnable");
        startAnimation(translateUp);
    }
};

//// This is the listener for the close button////

View.OnClickListener closeButtonListener = new View.OnClickListener() {         

    public void onClick(View v) {
        translateHandler.post(mtranslateUp);
    }
};

//// This is the translate up animation ////

translateUp = new TranslateAnimation(0,0,0,-200);
translateUp.setFillAfter(true);
translateUp.setDuration(1000);
translateUp.setAnimationListener(new AnimationListener(){
        @Override
        public void onAnimationEnd(Animation animation) {
            Log.v("TEST","translateUp onAnimationEnd");
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationStart(Animation animation) {
            Log.v("TEST","translateUp onAnimationStart");
        }}
    );

If the customView is added to an activity, these code works fine!

When the customView is added to a WindowManager, the Log inside the onAnimationStart didn't show but the Log inside the Runnable can be shown.

Can anybody tells how to do animation on a view that is added to the WindowManager?

like image 413
Mr.1 Avatar asked Dec 29 '11 07:12

Mr.1


2 Answers

You should animate the view LayoutParameters. For example I use a method to update the view layout:

    public void updateViewLayout(View view, Integer x, Integer y, Integer w, Integer h){
    if (view!=null) {
        WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();

        if(x != null)lp.x=x;
        if(y != null)lp.y=y;
        if(w != null && w>0)lp.width=w;
        if(h != null && h>0)lp.height=h;

        mWindowService.updateViewLayout(view, lp);
    }
}

Obviously mWindowService is context.getSystemService(Context.WINDOW_SERVICE). I trigger this method in the animation:

    public static void overlayAnimation(final View view2animate, int viewX, int endX) {
    ValueAnimator translateLeft = ValueAnimator.ofInt(viewX, endX);
    translateLeft.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            updateViewLayout(view2animate, val, null, null, null);

        }
    });
    translateLeft.setDuration(ANIMATION_DURATION);
    translateLeft.start();
}
like image 68
Santacrab Avatar answered Nov 01 '22 15:11

Santacrab


I was facing similar problem with a View attached to WindowManager.Try adding ViewGroup to WindoManager than View directly. It should work.

like image 42
Dusi Avatar answered Nov 01 '22 13:11

Dusi