Wednesday, October 16, 2013

Create a translate animation in Java code.

This is the method witch you want pass only your view.
In this animation view animated from center to top.
you can change the value of -yDest  as per your require.

private void moveViewToScreenCenter(View view)
    {
        this.tempView = view;
        this.tempView.bringToFront();
        // RelativeLayout root = (RelativeLayout)
        // this.findViewById(R.id.rootLayout); this for display animation.
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        // int statusBarOffset = dm.heightPixels -
        // root.getMeasuredHeight();//its also require

        int originalPos[] = new int[2];
        this.tempView.getLocationOnScreen(originalPos);

        int xDest = dm.widthPixels / 2;
        xDest -= (this.tempView.getMeasuredWidth() / 2);
        // int yDest = dm.heightPixels / 2 - (view.getMeasuredHeight() / 2) -
        // statusBarOffset;

        int yDest = dm.heightPixels / 2 - (this.tempView.getMeasuredHeight() / 2);

        // TranslateAnimation anim = new TranslateAnimation(0, xDest -
        // originalPos[0], 0, yDest - originalPos[1]);

        this.translateAnimation = new TranslateAnimation(0, 0, 0, -yDest);// Runnig
                                                                          // Perfectly.
        this.translateAnimation.setDuration(1100);
        this.translateAnimation.setFillAfter(false);// this for hide the view
                                                    // and display the new view.
                                                    // Apply true if stop
                                                    // animation from header.
        this.translateAnimation.setAnimationListener(this);
        if (Build.VERSION.SDK_INT > 10)
        {
            this.translateAnimation.setBackgroundColor(Color.TRANSPARENT);
        }
        this.tempView.startAnimation(this.translateAnimation);
    }
Happy Coding.

No comments:

Post a Comment