3

I would like to display some UI Elements on a android xml layout file. I try to make an application, where two players can sit at each end of the mobile device, and play against each other.

So need to show some Button 180 degrees rotateted.

Is this possible? I tried android:gravity, but this did not work.

Thanks for you help, Martin

Jamie Hutton
  • 260
  • 3
  • 13
Martin
  • 33
  • 1
  • 3

4 Answers4

5

I would suggest that you take a look at this thread, which discusses a similar issue. Even though the question is regarding a TextView component, Button extends TextView, so it should be trivial to adapt this to a button. The OP of that question eventually settled on the following onDraw() method:

@Override
public void onDraw(Canvas canvas) {
    //This saves off the matrix that the canvas applies to draws, so it can be restored later. 
    canvas.save(); 

    //now we change the matrix
    //We need to rotate around the center of our text
    //Otherwise it rotates around the origin, and that's bad. 
    float py = this.getHeight()/2.0f;
    float px = this.getWidth()/2.0f;
    canvas.rotate(180, px, py); 

    //draw the text with the matrix applied. 
    super.onDraw(canvas); 

    //restore the old matrix. 
    canvas.restore(); 
}

I will also say that I wrote a class which implemented this onDraw() method and it worked great.

Community
  • 1
  • 1
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
0

You can use also this sample. It is better because you don;t need to move your items by Y.

canvas.save();

canvas.scale(1f, -1f, super.getWidth() * 0.5f,
    super.getHeight() * 0.5f);

canvas.drawBitmap(arrow,
    rect.centerX() - (arrow.getWidth() * 0.5F), rect.bottom,
    null);

canvas.restore();
Gelldur
  • 11,187
  • 7
  • 57
  • 68
0

So need to show some Button 180 degrees rotateted.

This is not supported by any existing Android widgets, sorry.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But here it is implemented: http://www.androidpit.de/de/android/market/apps/app/coolcherrytrees.games.reactor/2-Spieler-Reaktor – Martin Oct 05 '10 at 20:29
  • @Martin: What proof do you have that those are Android widgets? I am guessing that is implemented using 2D graphics and the `Canvas` API. – CommonsWare Oct 05 '10 at 20:50
  • Ok, so I should also use canvas. I have never worked with it. Is it hard to draw 4 Buttons and use Click events on them? – Martin Oct 05 '10 at 21:00
  • @Martin: It should not be too bad, as there are many games that are much more complex than that. However, I have not used it myself. BTW, another possibility is to use widgets like `ImageView` and `ImageButton` and simply rotate the contents 180 degrees. – CommonsWare Oct 05 '10 at 21:02
  • Ok, but I have to create the content of the buttons by the application, so I cant use an image. – Martin Oct 05 '10 at 21:04
0

What you can do is extend the Button view and override the onDraw() method.
This gives you a canvas which you can then rotate and then call super.onDraw() to have the system draw the button after it has been rotated.

Miguel Morales
  • 1,707
  • 10
  • 10
  • My onDraw method contains: Paint myPaint = new Paint(); myPaint.setStrokeWidth(1); myPaint.setColor(0xFF097286); canvas.drawText("test", 20, 20, myPaint); canvas.drawCircle(10, 10, 10, myPaint); invalidate(); I can paint the circle, but i cannot paint the Text. What is wrong? And how can i rotate the drawing test? – Martin Oct 05 '10 at 21:31
  • Take a look at: http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2. Also, see the Canvas.rotate() documentation. – Miguel Morales Oct 05 '10 at 22:18