ANDROID: Pixel Coordinates Differ in phones when drawing a simple shape. FIX THIS BUT HOW? Adjust hardcoded pixels so it looks the same on each phone -


so near noob @ programming android, , still learning it.

i have created small 2d spaceship game, have dodge oncoming stars moving finger. game works on emulator, when played on other phones (like friend's), messed up.

first of spaceship looks big on phone, , stars, (not on emulator). , written text coordinates seem off well.

i believe problem has pixels, , adjusting them each phone how?

thank helping me,

below long code, if want go through it, though it's unnecessary...

please if can, show me example of how it, show me put in coordinates when drawing simple rectangle example...thanks lot

package com.zunairgames.zunair;  import java.util.random;  import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.matrix; import android.graphics.paint; import android.os.bundle; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.ontouchlistener; import android.view.window; import android.view.windowmanager;  public class gfxsurface extends activity implements ontouchlistener {  mybringbacksurface oursurfaceview; float x, y; boolean testingfinger=false; @override protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     oursurfaceview = new mybringbacksurface(this);     oursurfaceview.setontouchlistener(this);     x=0;     y=0;       requestwindowfeature(window.feature_no_title);         getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);     setcontentview(oursurfaceview);//oursurfaceview }  @override protected void onpause() {     // todo auto-generated method stub     super.onpause();     oursurfaceview.pause(); }  @override protected void onresume() {     // todo auto-generated method stub     super.onresume();     oursurfaceview.resume(); }  public boolean ontouch(view v, motionevent event) {     // todo auto-generated method stub      x= event.getx();     y= event.gety();       if (event.getaction()==motionevent.action_down) {          testingfinger=true;          return true;      }      if(event.getaction()==motionevent.action_up){          testingfinger=false;          return false;      }        return false; }  public class mybringbacksurface extends surfaceview implements runnable{ //vertical surfaceholder ourholder; canvas canvas = (canvas) ourholder; thread ourthread = null; boolean isrunning=false; int screenheight; int screenwidth;  random random = new random (); boolean loadstuff = false;  int posx=0; int posy=0; int poswidth=100; int posheight=100;   int numstars=4; int starx[]=new int[numstars]; int stary[]=new int[numstars]; int starspeed[]=new int[numstars];   int score=0; int backgroundy=0;   bitmap spaceship; bitmap background; bitmap starpic;   public mybringbacksurface(context context) {     // todo auto-generated constructor stub     super(context);     ourholder= getholder();   }  public void pause(){     isrunning=false;     while(true){         try {             ourthread.join();         } catch (interruptedexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         break;     }     ourthread=null; } public void resume(){     isrunning=true;     ourthread=new thread(this);     ourthread.start(); }    public int round(double d){     double dabs = math.abs(d);     int = (int) dabs;     double result = dabs - (double) i;     if(result<0.5){         return d<0 ? -i : i;                 }else{         return d<0 ? -(i+1) : i+1;               } }  public bitmap getresizedbitmap(bitmap bm, int newheight, int newwidth) {     int width = bm.getwidth();      int height = bm.getheight();      float scalewidth = ((float) newwidth) / width;      float scaleheight = ((float) newheight) / height;      // create matrix manipulation      matrix matrix = new matrix();      // resize bit map         matrix.postscale(scalewidth, scaleheight);        // recreate new bitmap      bitmap resizedbitmap = bitmap.createbitmap(bm, 0, 0, width, height, matrix, false);      return resizedbitmap;      } public void run() {     // todo auto-generated method stub       while(isrunning){         if(!ourholder.getsurface().isvalid())continue;           canvas = ourholder.lockcanvas();           if(loadstuff==false){                 (int i=0; <numstars; i++){                     stary[i]=-random.nextint(200);                     starx[i]=random.nextint(canvas.getwidth()-50);                     starspeed[i]=1+random.nextint(5);                 }                  spaceship = bitmapfactory.decoderesource(getresources(),r.drawable.spaceship);                  background  = bitmapfactory.decoderesource(getresources(),r.drawable.background);                  starpic  = bitmapfactory.decoderesource(getresources(),r.drawable.tile);                   spaceship = getresizedbitmap(spaceship,100,100);                  background= getresizedbitmap(background,(canvas.getheight())*2,(canvas.getwidth()));                  starpic= getresizedbitmap(starpic,50,50);                       backgroundy=-(canvas.getheight());                       x=canvas.getwidth()/2;                      y=canvas.getheight()/2;                 loadstuff=true;                 }           paint paint = new paint();         paint.setcolor(color.green);         paint.settextsize(40);          (int i=0; <numstars; i++){             stary[i]+=starspeed[i];              if(stary[i]>canvas.getheight()){                 stary[i]=-random.nextint(200);                 starx[i]=random.nextint(canvas.getwidth()-50);                 starspeed[i]=starspeed[i]+random.nextint(2);             }              if(x+poswidth>starx[i]&&x<starx[i]+50 && y+posheight>stary[i]&&y<stary[i]+50){                 stary[i]=-random.nextint(200);                 starx[i]=random.nextint(canvas.getwidth()-50);                 starspeed[i]=1+random.nextint(10);                 score++;             }         }          backgroundy++;         if(backgroundy>-10){             backgroundy=-canvas.getheight();     }         canvas.drawrgb( 3, 120, 12);         canvas.drawbitmap(background, 0,backgroundy, null);          (int i=0; <numstars; i++){               canvas.drawbitmap(starpic,starx[i] , stary[i],null);          }         canvas.drawtext("score : "+ score  + "density :"+ canvas.getdensity(),0, 100, paint);              canvas.drawbitmap(spaceship, x,y, null);           ourholder.unlockcanvasandpost(canvas);      //72     } 

when programatically setting widths , pixel locations need make sure take account screen density varies device device.

so figure calculations in "density independent pixels" (dip or dp) , convert them real pixels using formula below before setting on component.

public int dptopx(int dp) {     displaymetrics displaymetrics = getcontext().getresources().getdisplaymetrics();     int px = math.round(dp * (displaymetrics.xdpi / displaymetrics.density_default));            return px; } 

further reading @ http://developer.android.com/guide/practices/screens_support.html#dips-pels


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -