gpt4 book ai didi

android - 无法在 Android 的 Surface Holder 中获取屏幕宽度和高度?

转载 作者:行者123 更新时间:2023-11-29 00:17:54 25 4
gpt4 key购买 nike

我是 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);
}

在第一个日志标签中它显示 0,而在第二个日志标签中它显示模拟器的确切宽度。我已经尝试使用 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);
}

}

请帮忙!

最佳答案

你试过吗?

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) {}

希望对您有所帮助。 :)

关于android - 无法在 Android 的 Surface Holder 中获取屏幕宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com