java - Algorithm to calculate values based on width and height (coordinates and size) -


what want might separated several or 1 methods, whatever best.

i have 4 values set (as fields):

  1. width of image whole image (canvas speak)
  2. height of whole image (height of canvas)
  3. padding (how many pixels want have padding)
  4. how many images in want in width , height separately (can separate values)

what want have whole image, i'm putting small images inside (these bitmap sizes want method).

i want have size of images inside, both width , height. , coordinates in of small images in whole image view.

a small example might be:

  • the width of image 200 , want padding of 4, , want have 3 images on 1 row of image.
  • the calculation might this:

    (200 - (4*(3+1))) / 3 = 61,33;

  • the 3+1 because want have padding on either side of small images.

but width, have universal solution applies height well.

and calculate height , width of every image inside canvas-image.

and put sugar on top. have x , y coordinates of every image inside.

how can done? , how every value? bitmaps (i'm developing android) stored in arraylist.

basically want this, basic grid layout: http://developer.android.com/images/ui/gridview.png values have based on image:

  • i have size of light-blue area,
  • including space between dark-blue images (padding)
  • and how many dark blue spots want in every row , column.

what want size (width , height of dark-blue spots), , coordinates of images (where should placed in light-blue area, x , y coordinates).

anybody have solution this?

updated text clarity

this code of i'm doing , wanted do. if has ideas how improve code change accepted answer.

public class classname {  public static int bitmapsizewidth; public static int bitmapsizeheight; public static final int bitmappadding = 8; public static final int howmanyimagescolumn = 3; public static final int howmanyimagesrows = 2;  public static bitmap folderbitmap(arraylist<bitmap> bitmaparray, int imageviewwidth, int imageviewheight) {     bitmapsizewidth = imageviewwidth;     bitmapsizeheight = imageviewheight;      bitmap b = bitmap.createbitmap(bitmapsizewidth, bitmapsizeheight, bitmap.config.rgb_565);     canvas c = new canvas(b);      //lets in set coordinate system     if (bitmaparray.size() >= 1)         c.drawbitmap(bitmaparray.get(0), bitmappadding, bitmappadding, paint);      if (bitmaparray.size() >= 2)         c.drawbitmap(bitmaparray.get(1), calculatesecondcoord().xpos, bitmappadding, paint);      if (bitmaparray.size() >= 3)         c.drawbitmap(bitmaparray.get(2), calculatethirdcoord().xpos, bitmappadding, paint);      if (bitmaparray.size() >= 4)         c.drawbitmap(bitmaparray.get(3), bitmappadding, calculatesecondcoord().ypos, paint);      if (bitmaparray.size() >= 5) {         c.drawbitmap(bitmaparray.get(4), calculatesecondcoord().xpos, calculatesecondcoord().ypos, paint);     }      if (bitmaparray.size() >= 6) {         c.drawbitmap(bitmaparray.get(5), calculatethirdcoord().xpos, calculatesecondcoord().ypos, paint);     }      return b; }  public static bitmapsize calculatesinglebitmapsize(int imageviewwidth, int imageviewheight) {     bitmapsizewidth = imageviewwidth;     bitmapsizeheight = imageviewheight;     bitmapsize bsize = new bitmapsize();     bsize.widthsize = (int) (bitmapsizewidth -             (bitmappadding * (howmanyimagescolumn + 1))) / howmanyimagescolumn;     bsize.heightsize = (int) (imageviewheight - (bitmappadding * (howmanyimagesrows + 1))) / howmanyimagesrows;     return bsize; }  /*  * first coord = padding  * second coord (bitmappadding) + calculatesinglebitmapsize(bitmapsizewidth);  * third coord (bitmappadding * 3) + (calculatesinglebitmapsize(bitmapsizewidth) * 2)  * math supposed correct can perhaps done in more efficient way  */ private static bitmapcoord calculatesecondcoord() {     bitmapcoord bcoord = new bitmapcoord();     bcoord.xpos = (int) (bitmappadding * (howmanyimagescolumn - 1)) + calculatesinglebitmapsize(bitmapsizewidth, bitmapsizeheight).widthsize;     bcoord.ypos = (int) (bitmappadding * (howmanyimagesrows)) + calculatesinglebitmapsize(bitmapsizewidth, bitmapsizeheight).heightsize;      return bcoord; }  private static bitmapcoord calculatethirdcoord() {     bitmapcoord bcoord = new bitmapcoord();     bcoord.xpos = (int) (bitmappadding * howmanyimagescolumn) + calculatesinglebitmapsize(bitmapsizewidth, bitmapsizeheight).widthsize * 2;     bcoord.ypos = (int) (bitmappadding * howmanyimagesrows - 1) + calculatesinglebitmapsize(bitmapsizewidth, bitmapsizeheight).heightsize * 2;      return bcoord; }  public static class bitmapsize {     public int widthsize;     public int heightsize; }  static class bitmapcoord {     int xpos;     int ypos; } } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -