【问题标题】:How can I create HUD (mirrored) layout/screen Android?如何创建 HUD(镜像)布局/屏幕 Android?
【发布时间】:2014-10-21 12:12:41
【问题描述】:

嗯,我想创建一个简单的应用程序。 即使小部件是 DigitalClock 或其他东西,也会反转布局的外观。 我已经用 android:scaleX="-1"; 试过了。 但它仅适用于文本和图像。

如何创建一个反转整个屏幕的布局,使其看起来像一个镜像视图?

谢谢。

普通视图:

我想要的视图:

【问题讨论】:

    标签: android layout view mirror hud


    【解决方案1】:

    只需创建一个自定义 ViewGroup(或扩展现有的)并在画布绘制子对象之前缩放画布:

    public class MirrorRelativeLayout extends RelativeLayout {
        public MirrorRelativeLayout(Context context) {
            super(context);
        }
    
        public MirrorRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MirrorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
            canvas.save();
            // Scale the canvas in reverse in the x-direction, pivoting on
            // the center of the view
            canvas.scale(-1f, 1f, getWidth() / 2f, getHeight() / 2f);
            super.dispatchDraw(canvas);
            canvas.restore();
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.save();
            // Scale the canvas in reverse in the x-direction, pivoting on
            // the center of the view
            canvas.scale(-1f, 1f, getWidth() / 2f, getHeight() / 2f);
            super.dispatchDraw(canvas);
            canvas.restore();
        }
    }
    

    然后将 ViewGroup 用作布局的根:

    <com.your.packagename.MirrorRelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:text="Mirror Text!"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </com.your.packagename.MirrorRelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 2014-01-27
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多