Android - SurfaceView(宽度,高度)问题

发布于 2024-10-18 22:40:12 字数 644 浏览 2 评论 0原文

我是 android 新手,面临一些问题...

我使用 SurfaceView 来扩展我的类。 在我的班级中,我无法获取 SurfaceView 的宽度和高度。 它始终为 0。我试图在整个屏幕(surfaceView)上轻拂图像,但我无法获取其宽度和高度。

有什么建议吗?

以下是 main.xml 中 SurfaceView 的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <test.MyClass android:id="@+id/SurfaceView01"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    </test.MyClass>
</LinearLayout>

Im new to android and im facing some issues...

Im using a SurfaceView to extend my class.
In my class i cant get width and height of the SurfaceView.
It is always 0. Im trying to flick an image on the entire screen (surfaceView) but im unable to get its width and height.

Any suggestions?

Here is the code for SurfaceView in main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <test.MyClass android:id="@+id/SurfaceView01"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    </test.MyClass>
</LinearLayout>

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

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

发布评论

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

评论(4

许仙没带伞 2024-10-25 22:40:12

您可以尝试实现 surfaceChanged 方法(在调整表面大小时调用该方法,包括初始化时):

http://developer.android.com/reference/android/view/SurfaceHolder.Callback.html#surfaceChanged(android.view.SurfaceHolder, int, int, int)

只需执行以下操作:

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // do whatever you want here with width and height;
}

You could try implementing the surfaceChanged method (which is called any time the surface is resized, including when it is initialized):

http://developer.android.com/reference/android/view/SurfaceHolder.Callback.html#surfaceChanged(android.view.SurfaceHolder, int, int, int)

Just do:

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // do whatever you want here with width and height;
}
初相遇 2024-10-25 22:40:12

我尝试过类似的事情并在此线程上发布Android:画布上的UI元素 除了上面之外,

您还需要它来获取高度和宽度,

public void onDraw(Canvas canvas)
{
    this.canvas = canvas;

    if (!oneTime)
    {
        oneTime = true;
        screenWidth = getWidth();
        screenHeight = getHeight();
        Bitmap red = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        red = Bitmap.createScaledBitmap(red, screenWidth , screenHeight , true);
    }
}

如果您不想在 OnDraw 中提取代码,

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;

您可以尝试下面的代码,但这将为您提供整个可见区域的宽度和高度

I have tried similar thing and posted on this thread Android: UI elements over canvas layer

Apart from above you would also need this to get height and width

public void onDraw(Canvas canvas)
{
    this.canvas = canvas;

    if (!oneTime)
    {
        oneTime = true;
        screenWidth = getWidth();
        screenHeight = getHeight();
        Bitmap red = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        red = Bitmap.createScaledBitmap(red, screenWidth , screenHeight , true);
    }
}

if you dont want to pull your code in OnDraw you can try below code

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;

But this will give you width height of whole visible area

对你而言 2024-10-25 22:40:12

我发现对我有用的解决方案是在我启动线程后在我的 SurafaceCreated 方法中使用 getHeight() 和 getWidth() 。这使我能够在进行任何更新或绘制之前获得表面视图的高度和宽度。

The solution I found worked for me with this was to use getHeight() and getWidth() inside my SurafaceCreated method, after I started my thread. This allowed me to have the Height and Width of the surface view before any updating or drawing has occured.

§对你不离不弃 2024-10-25 22:40:12

使用 SurfaceView.getHolder().getSurfaceFrame() 获取具有尺寸的 Rect 对象

Use surfaceView.getHolder().getSurfaceFrame() to get the Rect object with the sizes

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