无法获得屏幕宽度和宽度android中表面支架的高度?

时间:2014-08-12 08:26:18

标签: android

我是Android的新手并坚持获取屏幕尺寸。这是我的代码,

package com.piyush.droidz;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.piyush.droidz.model.boy;

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = MainGamePanel.class.getSimpleName();

private GameThread td;
private boy b1;
private int s_width;
private int s_height;

public MainGamePanel(Context context) {
    super(context);
    getHolder().addCallback(this);
    Log.d(TAG, "Screen width=" + s_width);
    b1 = new boy(BitmapFactory.decodeResource(getResources(), R.drawable.boy), 50, 50);
    td = new GameThread(getHolder(), this);
    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    s_width = getHolder().getSurfaceFrame().width();
    Log.d(TAG, "Present Screen width=" + s_width);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    td.setRunning(true);
    td.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    td.setRunning(false);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        b1.handleActionDown((int)event.getX(), (int)event.getY());

        if (event.getY()>getHeight()-50) {
            td.setRunning(false);
            ((Activity) getContext()).finish();
        }
        else {
            Log.d(TAG, "Coordinates: X="+event.getX()+", Y="+event.getY());
        }
    }

    if (event.getAction()==MotionEvent.ACTION_MOVE) {
        if (b1.isTouched()) {
            b1.setX((int)event.getX());
            b1.setY((int)event.getY());
        }
    }

    if (event.getAction()==MotionEvent.ACTION_UP) {
        b1.setTouched(false);
    }
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.parseColor("#ff0000"));
    b1.draw(canvas);
}

}

在第一个log-tag中显示0,而在第二个log-tag中它显示模拟器的确切宽度。我已尝试在构造函数之前使用getHolder().getSurfaceFrame().width()初始化“s_width”,但仍然“s_width”为0.还尝试了WindowManagergetwindowManager(),但此类中的IDE无法识别它。这是我的Activity类,

package com.piyush.droidz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class DroidzActivity extends Activity {

MainGamePanel mgp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mgp = new MainGamePanel(this);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(mgp);
}

}

请帮忙!

1 个答案:

答案 0 :(得分:0)

你试过吗?

   DisplayMetrics metrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(metrics);

您需要使用上下文来获取WindowManager。请参阅文档 http://developer.android.com/reference/android/view/WindowManager.html

优选地,上述内容应在构造函数

内完成
public MainGamePanel(Context context) {}

希望它有所帮助。 :)