I am making an Android game and am using SurfaceView.
I want to know the information on the SmartPhone screen (height, width, etc.)
I used this code, but .. This is an incorrect value for the width and height of the display,
He always printed “0” with red
What am I missing?
public class MainActivity extends Activity { private int width; private int height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); width = metrics.widthPixels; height = metrics.heightPixels; setContentView(new GameView(this)); } public class Painter extends Thread public void run(){ Canvas canvas = null; long start, end; int sleep; while(true) { if (runnable) { start = System.currentTimeMillis(); try { canvas = holder.lockCanvas(null); paint.setColor(Color.RED); paint.setTextSize(200); synchronized (holder) { canvas.drawText(Integer.toString(main.getHeight()), 600, 800, paint); canvas.drawText(Integer.toString(main.getWidth()), 300, 400, paint); } } catch (Exception e) { e.printStackTrace(); } finally { holder.unlockCanvasAndPost(canvas); } end = System.currentTimeMillis(); sleep = 1000 / FPS - (int) (end - start); try { Thread.sleep(sleep > 0 ? sleep : 0); }
source share