在SurfaceView 中调用getWidth()方法,获取的宽度与实际情况不符。肿么了?

龙胤 发布于 2012/09/13 11:07
阅读 5K+
收藏 0

在SurfaceView 中调用getWidth()方法,获取的宽度与实际情况不符。谁能告诉我怎么回事?

虚拟机的设置如下:

实际的的结果是320,不是480.

代码如下:

package com.msi.manning.chapter9.OpenGLSquare;

import android.app.Activity;
import android.os.Bundle;


public class SquareActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
      
        setContentView(R.layout.main);
          }

   }

--------------------------------------------------------------------------------------------------------------

package com.msi.manning.chapter9.OpenGLSquare;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class Test extends SurfaceView implements Callback {
 public Test(Context context) {
  super(context);
  h=getHolder();
  h.addCallback(this);
 }
 public Test(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  h=getHolder();
  h.addCallback(this);
 }
 public Test(Context context, AttributeSet attrs) {
  super(context, attrs);
  h=getHolder();
  h.addCallback(this);
 }

 private SurfaceHolder h;

 
 

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  // TODO Auto-generated method stub

 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  // TODO Auto-generated method stub
Log.d("TEST","Width="+ this.getWidth()+"|Height="+this.getHeight());

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub

 }

}

------------------------------------------------------------------------------------------

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

 
    <com.msi.manning.chapter9.OpenGLSquare.Test
        android:layout_width="fill_parent"
      
        android:layout_height="fill_parent" />

</LinearLayout>

--------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.msi.manning.chapter9.OpenGLSquare">
  
    <application android:icon="@drawable/icon">
        <activity android:name=".SquareActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

---------------------------------------------------------------------------------------------------

运行结果:



虚拟机屏幕宽度是480,但实际结果却是320.

那个异常是什么?似乎不影响运行。

加载中
0
小小街
小小街

你得用真机测试下

0
倔强的兔兔
因为用的单位不一样,一个是px,也就是像素,一个用的dp,px=1.5dp.还要看是 什么屏 ,WVGA   HVGA   等等
返回顶部
顶部