Android - SurfaceView issue (width, height)

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

Im using SurfaceView to extend my class. In my class, I cannot get the width and height of the SurfaceView. This is always 0. Im trying to click the image full 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>
+3
source share
4 answers

I tried a similar thing and posted in this Android theme : user interface elements on top of the canvas layer

Besides the above, you will also need this to get the 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 do not want to pull your code in OnDraw, you can try under the code

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

+4

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;
}
+4

, , , getHeight() getWidth() SurafaceCreated , . , - .

+3

surfaceView.getHolder().getSurfaceFrame(), Rect

+2

Source: https://habr.com/ru/post/1794070/


All Articles