views:

68

answers:

3

Hi,

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

A: 

So need to show some Button 180 degrees rotateted.

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

CommonsWare
But here it is implemented: http://www.androidpit.de/de/android/market/apps/app/coolcherrytrees.games.reactor/2-Spieler-Reaktor
Martin
@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
@CommonsWare: 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
@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
Ok, but I have to create the content of the buttons by the application, so I cant use an image.
Martin
A: 

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
@Miguel Morales: 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
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
+2  A: 

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.

eldarerathis
It works, thank you very much!
Martin