no9*_*no9 3 height android width surfaceview
我是新手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>
Run Code Online (Sandbox Code Playgroud)
您可以尝试实现surfaceChanged方法(在表面调整大小时调用,包括初始化时):
做就是了:
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// do whatever you want here with width and height;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的事情并发布在这个线程Android: UI elements over canvas layer
除了上面之外,您还需要它来获取高度和宽度
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不想在 OnDraw 中提取代码,您可以尝试下面的代码
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Run Code Online (Sandbox Code Playgroud)
但这会给你整个可见区域的宽度和高度
| 归档时间: |
|
| 查看次数: |
22823 次 |
| 最近记录: |