Realizzo giochi per Android e utilizzo SurfaceView.(Android) Come ottengo SmartPhone ScreenWidth e Height?
Voglio sapere SmartPhone schermata Inf (altezza, larghezza, ect ..)
ho Uesd questo codice, ma .. Non è corretto valore della visualizzazione larghezza e l'altezza,
E 'sempre stampato "0" di Red Color
Cosa mi manca?
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);
} // Try
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Questo codice è stato sconsigliato da alcuni anni –