Android - 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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试实现 surfaceChanged 方法(在调整表面大小时调用该方法,包括初始化时):
http://developer.android.com/reference/android/view/SurfaceHolder.Callback.html#surfaceChanged(android.view.SurfaceHolder, int, int, int)
只需执行以下操作:
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:
我尝试过类似的事情并在此线程上发布Android:画布上的UI元素 除了上面之外,
您还需要它来获取高度和宽度,
如果您不想在 OnDraw 中提取代码,
您可以尝试下面的代码,但这将为您提供整个可见区域的宽度和高度
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
if you dont want to pull your code in OnDraw you can try below code
But this will give you width height of whole visible area
我发现对我有用的解决方案是在我启动线程后在我的 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.
使用 SurfaceView.getHolder().getSurfaceFrame() 获取具有尺寸的 Rect 对象
Use
surfaceView.getHolder().getSurfaceFrame()
to get theRect
object with the sizes