How to rotate an ViewGroup

Viewed 46

I have an FrameLayout and I use WindowManager.addView to show this FrameLayout as a float window. In this FrameLayout there are many subviews such as ImageView and SeekBar. Then I need rotate this FrameLayout through View.setRotate. After this method called, the FrameLayout seems rotated but when I want to interact it by some subviews in it, I found all subviews still in the origin place before it rotated. So how do I rotate an ViewGroup with all its subview?

1 Answers

You can try this:

RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
int w = mainLayout.getWidth();
int h = mainLayout.getHeight();

mainLayout.setRotation(270.0f);
mainLayout.setTranslationX((w - h) / 2);
mainLayout.setTranslationY((h - w) / 2);

ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mainLayout.getLayoutParams();
lp.height = w;
lp.width = h;
mainLayout.requestLayout();

This should help.

Related