Martin
Martin

Reputation: 33

How to display Android UI Elements about 180 degrees rotated

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

Upvotes: 3

Views: 2965

Answers (4)

Gelldur
Gelldur

Reputation: 11558

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();

Upvotes: 0

eldarerathis
eldarerathis

Reputation: 36213

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.

Upvotes: 5

Miguel Morales
Miguel Morales

Reputation: 1707

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.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006914

So need to show some Button 180 degrees rotateted.

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

Upvotes: 0

Related Questions