gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:37:14 26 4
gpt4 key购买 nike

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

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

提前致谢。

普通 View :

normal view我想要的 View :

the view I want

最佳答案

只需制作一个自定义 ViewGroup(或扩展现有的)并在绘制其子项之前缩放 Canvas :

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>

关于android - 如何创建 HUD(镜像)布局/屏幕 Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25535128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com