android - How to programmatically scale an image to the size of a ImaageButton that was created dynamically -


i'm new android , trying create simple game called sos. it's close complete can not figure out how scale images put in imagebuttons. game board created during runtime, user chooses board size, 6 x 6, 7 x 7, or 8 x 8 (ie: board of 36, 49 or 64 tiles(imagebuttons)). depending on screen size/boardsize images may or may not fit in imagebuttons. decided try , scale images size of imagebutton. tried scale resource image correct size in constructor of tile(imagebutton), , store result in instance variable, runtime exception, , i'm not sure how fix it. in scaleimages method if change this.getlength(), , this.getwidth() 50 runs out error no images set in imagebutton. think need way determine size of button before try , scale image.

public class tile extends imagebutton {     private boolean occupied;     private boolean iss;     private boolean iso;     int countclicks = 0;     private static drawable simagescaled;     private static drawable oimagescaled;     private static drawable emptyimagescaled;       public tile(context context) {         super(context);          scaleimages(context);      }      public tile(context context, attributeset attrs) {         super(context, attrs);          scaleimages(context);      }      public tile(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);          scaleimages(context);      }      public void setdefaults() {         occupied = false;         iss = false;         iso = false;         this.setbackground(emptyimagescaled);     }      // method set background image of tile     // instance variable keep track of image displayed ,     // change every click     public void settile() {         ++countclicks;          switch (countclicks) {         case 1:             this.setbackground(simagescaled);             iss = true;             iso = false;                         break;         case 2:             this.setbackground(oimagescaled);             iso = true;             iss = false;         break;     case 3:         this.setbackground(emptyimagescaled);         iso = false;         iss = false;      }      // reset 0     if (countclicks == 3) {         countclicks = countclicks % 3;     } }          public void setoccupied(boolean val) {     occupied = val; } private void scaleimages(context context){      // scale s image     drawable sdr = getresources().getdrawable(r.drawable.ic_simageorange);     bitmap sbitmap = bitmap.createbitmap(sdr.getintrinsicwidth(), sdr.getintrinsicheight(), config.argb_8888);     // scale      simagescaled = new bitmapdrawable(context.getresources(), bitmap.createscaledbitmap(sbitmap, this.getwidth(), this.getheight(), true));      // scale o image     drawable odr = getresources().getdrawable(r.drawable.ic_oimage);     bitmap obitmap = bitmap.createbitmap(odr.getintrinsicwidth(), odr.getintrinsicheight(), config.argb_8888);     // scale     simagescaled = new bitmapdrawable(context.getresources(), bitmap.createscaledbitmap(obitmap, this.getwidth(), this.getheight(), true));      // scale empty image     drawable edr = getresources().getdrawable(r.drawable.ic_tile);     bitmap ebitmap = bitmap.createbitmap(edr.getintrinsicwidth(), edr.getintrinsicheight(), config.argb_8888);     // scale      emptyimagescaled = new bitmapdrawable(context.getresources(), bitmap.createscaledbitmap(ebitmap, this.getwidth(), this.getheight(), true));  } 

}

here's code in activity creates views , tiles(imagebuttons)

// method adds views tableview public void showgameboard() {      int tilepadding = 1;     //galaxy s4      int tilewh = 100;      // layout parameters tablerows     // (layout params parent class of object receiving     // param)      tablelayout.layoutparams rowlp = new tablelayout.layoutparams(             (tilewh * tilepadding) * boardsize.getwidth(), tilewh                     * tilepadding, 1.0f);      // layout parameters tablecells     tablerow.layoutparams celllp = new tablerow.layoutparams(tilewh             * tilepadding, tilewh * tilepadding, 1.0f);      // every row     (int row = 0; row < tiles.length; row++) {         // create new table row         tablerow tablerow = new tablerow(this);         // set height , width of row         tablerow.setlayoutparams(rowlp);          // every column         (int col = 0; col < tiles[row].length; col++) {             // add padding tiles             tiles[row][col].setpadding(tilepadding, tilepadding,                     tilepadding, tilepadding);              // add tile table row             tablerow.addview(tiles[row][col], celllp);         }          // add row board layout         game_board.addview(tablerow);     }  }  // method set tiles , listeners public void creategameboard() {      // set total rows , columns based on size of board     int totalrows = boardsize.getwidth();     int totalcols = boardsize.getlength();      // setup tiles array     tiles = new tile[totalrows][totalcols];      // every row     (int row = 0; row < tiles.length; row++) {          // every column         (int col = 0; col < tiles[row].length; col++) {             // create tile (imagebutton)             tiles[row][col] = new tile(this);              // set tile (imagebutton) defaults             tiles[row][col].setdefaults();              final int currow = row;             final int curcol = col; 

well if can solve issue or point me in right direction it'd appreciated.

i don't know how images imagebutton looks like, maybe 9-patch you. here simple guide 9-patch. make 9-patch image simply, change image's file name foo.png foo.9.png, open android studio , draw fill area , scale area appropriately. if don't use android studio, use sdk tools in /your_sdk_dir/toos/draw9patch. because simple tool, needs no explanation.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -