How to rotate a RelativeLayout 180 degrees?

I am trying to make an application that is designed for two people, and both see one half, so I need to flip one half vertically. I am using LinearLayout with two RelativeLayout inside it with layout_weight="1" .

The fact is, I'm not sure how to do this. Apparently android:rotate is only available in version 11+ (3.0+), but I would like it to support at least 2.2.

After reading other related questions on SO, I tried various things, none of which seem to work. I tried to extend the RelativeLayout and override the onDraw function, but it does nothing. Here is my code:

 public class FlippedRelativeLayout extends RelativeLayout { public FlippedRelativeLayout(Context context) { super(context); } public FlippedRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public FlippedRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { canvas.save(); canvas.rotate(180); super.onDraw(canvas); canvas.restore(); } } 

I will be happy for any help, thanks!

+7
source share
2 answers

Try the following:

 public class MyRelativeLayout extends RelativeLayout { public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public MyRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyRelativeLayout(Context context) { super(context); init(); } private void init() { setStaticTransformationsEnabled(true); } @Override protected boolean getChildStaticTransformation(View child, Transformation t) { t.setTransformationType(Transformation.TYPE_MATRIX); Matrix m = t.getMatrix(); m.reset(); m.postRotate(180, child.getWidth() / 2.0f, child.getHeight() / 2.0f); return true; } } 

Result: enter image description here

+9
source

Very interesting question!

Perhaps you will try to create two partially transparent Activity -s, showing your own copy of the same xml layout, and then switching the "z-order" of the active Activity depending on the movement.

Activity A will be “your own” activity, and it will have a transparent upper half, and RelativeLayout lower half. It would also have a normal screen orientation, for example: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) .

Activity In will be "your opponents activity." It will also have a transparent top half and a copy of the same RelativeLayout as the bottom. However, it would have an inverted screen orientation, for example: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) . This would mean that the transparent part of Activity B would overlap the part of RelativeLayout Activity A, and the transparent part of Activity A would overlap the part of RelativeLayout Activity B.

You can also put the corresponding Activity -s startup mode on the “one top” or some other suitable value, so you don’t create a new instance of your Activity when you “start it again”, i.e. pass "make-a-move-ticket" to the adversary.

Unfortunately, the orientation ...REVERSE_PORTRAIT not added up to API level 9 (Android 2.3.something), and you are explicitly requesting API level 8.

The optimal part of this approach will be that, since only one action can have focus (and, therefore, take input data) at a time, you automatically get a statemachine for user input: the opponent would not have the opportunity to interact with his board, until you have made your move, and vice versa.

Hope this gives you at least a few ideas.

Hooray!

+1
source

All Articles