如何显示大约180度旋转的Android UI元素

Mar*_*tin 3 java user-interface android

我想在android xml布局文件上显示一些UI元素.我尝试制作一个应用程序,其中两个玩家可以坐在移动设备的每一端,并互相对战.

所以需要显示一些180度旋转的按钮.

这可能吗?我试过android:gravity,但是这没用.

马丁,谢谢你的帮助

eld*_*his 5

我建议你看一下这个讨论类似问题的线程.即使问题是关于TextView组件,Button也扩展了TextView,因此将它调整为按钮应该是微不足道的.该问题的OP最终确定了以下onDraw()方法:

@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(); 
}
Run Code Online (Sandbox Code Playgroud)

我还会说我写了一个实现了这个onDraw()方法的类,它运行得很好.