java - 如何显示旋转 180 度左右的 Android UI 元素

标签 java android user-interface

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

因此需要显示一些旋转 180 度的 Button。

这可能吗?我尝试了 android:gravity,但这没有用。

谢谢你的帮助, 马丁

最佳答案

我建议你看看this thread , 其中讨论了类似的问题。尽管问题是关于 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(); 
}

我还要说的是,我编写了一个实现此 onDraw() 方法的类,并且效果很好。

关于java - 如何显示旋转 180 度左右的 Android UI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3866976/

相关文章:

java - 寻找更高效的堆排序?

java - 如何识别图像文件中的文本以及如何阅读该文本?

java - 如何修改 net.schmizz.sshj 日志级别?

java - 将不同类型的对象分类到一个列表中

android - 为什么要使用 "invalidate"这个词来请求重绘 View ?

c++ - C++ Builder 6 的一些问题

java - 将批处理文件中的值分配给 Jlabel

android - Espresso Dagger 2.11

android - 无法在 root 的 android 手机上打开数据目录

java - 通过 JFrame 添加到 ArrayList 时出现 NullPointerException 错误