animation - android 2D bitmap drawing -


can any1 tell me why cant add bitmap onto surfaceview this:

    steering = new steering(bitmapfactory.decoderesource(getresources(), r.drawable.ic_launcher), getwidth()-50,getheight()-50); 

if use integers instead of "getheight()"-method, bitmap added fine. since want game run on more 1 phone without looking weird want add using 2 methods.

can any1 help?

thanks!

where adding line? if on oncreate wouldnt display image since method getwidth() , getheight() return 0. paint have wait until system have created view. test receiving value try changing code have this:

final int width = getwidth(); final int height = getheight(); final bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.ic_launcher); steering = new steering(bitmap, width-50,height-50); 

and add breakpoint steering line , debugg it. if getting 0 in width , height have wait view draw.

edit: on activity/fragment can add tree observer this:

 myview.getviewtreeobserver().addongloballayoutlistener( new viewtreeobserver.ongloballayoutlistener() {                 @override                 public void ongloballayout() {                     //do here since have width , height of view                 }             }); 

here small example on how in classes:

my steering class:

public class steering {      private bitmap mbitmap;     private int mwidth;     private int mheight;      public steering(bitmap bitmap, int width, int height) {         this.mbitmap = bitmap;         this.mwidth = width;         this.mheight = height;     }      public bitmap getbitmap() {         //reescaling anddev.org/resize_and_rotate_image_-_example-t621         final int imagewidth = mbitmap.getwidth();         final int imageheight = mbitmap.getheight();         // calculate scale -         float scalewidth = ((float) mwidth) / imagewidth;         float scaleheight = ((float) mheight) / imageheight;         // createa matrix manipulation         matrix matrix = new matrix();         // resize bit map         matrix.postscale(scalewidth, scaleheight);          bitmap resizedbitmap = bitmap.createbitmap(mbitmap, 0, 0, imagewidth, imageheight, matrix, true);         return resizedbitmap;     }  } 

my activity

public class mainactivity extends activity {      myview mview;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         mview = (myview) findviewbyid(r.id.viewid);         ongloballayoutlistener listener = new ongloballayoutlistener() {              @override             public void ongloballayout() {                 final int width = mview.getwidth();                 final int height = mview.getheight();                 final bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.android);                 //image anddev                 final steering steering = new steering(bitmap, width-50,height-50);                 mview.setobject(steering);             }         };         mview.getviewtreeobserver().addongloballayoutlistener(listener);     }  } 

and view class

public class myview extends view{      steering steering = null;      public myview(context context) {         super(context);     }      public myview(context context, attributeset attrs) {         super(context, attrs);     }      public void setobject(steering steering){         this.steering = steering;     }      final paint paint = new paint();      @override     protected void ondraw(canvas canvas) {         canvas.save();         if(steering!=null){             canvas.drawbitmap(steering.getbitmap(), 0, 0, paint);         }         canvas.restore();     }  } 

you can use normal view or surfaceview, either way works. sorry if answer bit long :p


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -