Android 设置glsurfaceview的高度

发布于 2024-12-16 01:48:35 字数 143 浏览 4 评论 0原文

我想用自定义高度设置 glsurfaceview 的高度。 我希望宽度与高度相同。 使用 android:layout_width="fill_parent"。 我该如何让 width = height = width_of_the_screen ?

多谢

I want to set the Height of glsurfaceview with custom height.
I want that the width is the same of height.
With android:layout_width="fill_parent".
How I do to have width = height = width_of_the_screen ?

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聆听风音 2024-12-23 01:48:35

您需要重写 onMeasure 并将 setMeasuredDimension 的两个参数设置为接收到的宽度,如下所示:

class TouchSurfaceView extends GLSurfaceView {

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = View.MeasureSpec.getSize(widthMeasureSpec); 
    this.setMeasuredDimension(width, width);
}
...

布局如下:

...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_gravity="top"
    >
    <se.company.test.TouchSurfaceView android:id="@+id/glSurface"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
...

You need to override the onMeasure and set both parameters of setMeasuredDimension to the received width as seen below:

class TouchSurfaceView extends GLSurfaceView {

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = View.MeasureSpec.getSize(widthMeasureSpec); 
    this.setMeasuredDimension(width, width);
}
...

with a layout as follows:

...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_gravity="top"
    >
    <se.company.test.TouchSurfaceView android:id="@+id/glSurface"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
...
孤城病女 2024-12-23 01:48:35

您可以使用 view.getLayoutParams().height = view.getMeasuredWidth(); 以编程方式设置高度。问题是这必须在第一次绘制视图后完成,否则 < code>measuredWidth 只会返回零。 (假设您使用的是 openGL),您可以相当确定,当调用 Renderer 类中的 onDraw 时,它肯定会绘制在屏幕上。但是,您必须在 GL 线程(调用 onDraw)和(主)UI 线程之间传递消息,这是唯一允许更改视图的宽度/高度等的线程

You can set the height programatically by using view.getLayoutParams().height = view.getMeasuredWidth(); The problem is this has to be done after the view has been drawn for the first time, otherwise measuredWidth will just return zero. (Assuming you're using openGL), you can be fairly sure that by the time onDraw in your Renderer class is called, it is definitely drawn on screen. However, you'd then have to communicate a message between the GL Thread (that calls onDraw) and the (main) UI thread, which is the only one allowed to change views' width/height etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文