Android – surfaceview (width, height) problem
•
Android
I'm new to Android and I face some problems
I use surfaceview to expand my course. In my class, I can't get the width and height of surfaceview. It's always 0. I try to flick the image on the whole screen, but I can't get its width and height
Any suggestions?
The following is the code of 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>
resolvent:
I tried something similar and posted it on this post Android: UI elements over canvas layer
In addition to the above, you also need it 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 don't want to extract the code in OnDraw, you can try the following code
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
But this gives you the width and height of the entire visible area
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码