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

2023-09-06 08:04:14 作者:还能怎样

好吧,我想创建一个简单的应用程序。要彻底扭转布局的外观,即使部件是DigitalClock或别的东西。我已经与Android试了一下:将scaleX = - 1;但它仅适用于文本和图像。

Well, I want to create a simple app. that reverse the look of the layout, even if the widget is DigitalClock or something else. I've tried it with the android:scaleX="-1"; but it works only on text and images.

我怎样才能创建一个扭转了整个屏幕的布局,看起来像一个镜像的看法?

How can I create a layout that reversed the whole screen, to look like a mirrored view?

由于前方。

普通视图:

认为我想要的:

the view I want:

推荐答案

只是做一个自定义的的ViewGroup (或者扩展现有的一个)和缩放画布它借鉴前的儿童:

Just make a custom ViewGroup (or extend an existing one) and scale the canvas before it draws its children:

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>
 
精彩推荐
图片推荐