java - Android: Crop Bitmap from Bitmap and set as ImageView src -


i'm trying crop sprite spritesheet, cropped image not part of image specified.

the spritesheet contains sprites size of 32 x 32 px , therefore first sprite should @ pixels 0 32 (horizontally) , 0 32 (vertically), supposed upper left pixel [0, 0].

bitmap spritesheet = bitmapfactory.decoderesource(getresources(), r.drawable.spritesheet_32x32); bitmap sprite = bitmap.createbitmap(spritesheet, 0, 0, 32, 32); 

the imageview plain imageview without src, width or height set.

imageview view = (imageview)findviewbyid(r.id.player1); view.setimagebitmap(sprite); 

but displayed image smaller part of first sprite. oversee? view.setimagebitmap(sprite); right method this? imageview need fix width , height before setting bitmap?

edit: maybe should clearer layout. there 2 imageviews arranged on linearlayout. here's xml:

<linearlayout         android:orientation="horizontal"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:baselinealigned="false">     <imageview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:scalex="-1"         android:id="@+id/player1"         android:layout_weight="1"         android:layout_gravity="center_vertical" />     <imageview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/player2"         android:layout_weight="1"         android:layout_gravity="center_vertical" /> </linearlayout> 

import android.graphics.bitmap; import android.graphics.bitmapfactory;  public class imageresizer {  public static bitmap decodesampledbitmapfromfile(string filename,     int reqwidth, int reqheight) { // first decode injustdecodebounds=true check dimensions final bitmapfactory.options         options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(filename, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; return bitmapfactory.decodefile(filename, options); }  public static int calculateinsamplesize(bitmapfactory.options options,     int reqwidth, int reqheight) { // begin_include (calculate_sample_size) // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1;  if (height > reqheight || width > reqwidth) {      final int halfheight = height / 2;     final int halfwidth = width / 2;      // calculate largest insamplesize value power of 2 , keeps both     // height , width larger requested height , width.     while ((halfheight / insamplesize) > reqheight             && (halfwidth / insamplesize) > reqwidth) {         insamplesize *= 2;     }      // offers additional logic in case image has strange     // aspect ratio. example, panorama may have larger     // width height. in these cases total pixels might still     // end being large fit comfortably in memory, should     // more aggressive sample down image (=larger insamplesize).      long totalpixels = width * height / insamplesize;      // more 2x requested pixels we'll sample down further     final long totalreqpixelscap = reqwidth * reqheight * 2;      while (totalpixels > totalreqpixelscap) {         insamplesize *= 2;         totalpixels /= 2;     } } return insamplesize; // end_include (calculate_sample_size)  }  } 

call method

bitmap bmp = imageresizer.decodesampledbitmapfromfile(new  file(filepath).getabsolutepath(), 512, 342);                

i haven't tried follows might work pass method parameter like
imageview.getwidth() , imageview.getheight()


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -