如何创建HUD(镜像)布局/屏幕Android?

时间:2014-08-27 19:07:23

标签: android layout view mirror hud

好吧,我想创建一个简单的应用程序。 这反转了布局的外观,即使小部件是DigitalClock或其他东西。 我用android实现了它:scaleX =“ - 1”; 但它只适用于文字和图像。

如何创建一个颠倒整个屏幕的布局,看起来像一个镜像视图?

非常感谢。

普通视图:

normal view 我想要的观点:

the view I want

1 个答案:

答案 0 :(得分:5)

只需制作自定义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>