2015-04-25 86 views
0

我試圖從一個spritesheet裁剪精靈,但是裁剪的圖片是不是我指定的圖像的一部分。安卓:從位圖裁剪位圖,並設置爲ImageView的SRC

spritesheet包含大小爲32 x 32像素的精靈,因此第一個精靈應該在像素0到32(水平)和0到32(垂直),假設最左上角的像素是[0,0 ]。

Bitmap spritesheet = BitmapFactory.decodeResource(getResources(), R.drawable.spritesheet_32x32); 
Bitmap sprite = Bitmap.createBitmap(spritesheet, 0, 0, 32, 32); 

ImageView只是一個普通的ImageView,沒有設置src,width或height。

ImageView view = (ImageView)findViewById(R.id.player1); 
view.setImageBitmap(sprite); 

但顯示的圖像只是第一個精靈的一小部分。 我怎麼監督?是view.setImageBitmap(sprite);這個正確的方法?是否需要ImageView的設置位圖前修復寬度和高度?

編輯:也許我應該對我的佈局更加清晰。 LinearLayout上排列着兩個ImageView。這裏的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> 
+0

http://stackoverflow.com/questions/18232034/how-can-i-crop-a-bitmap-for-imageview –

回答

0
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

public class ImageResizer { 

public static Bitmap decodeSampledBitmapFromFile(String filename, 
    int reqWidth, int reqHeight) { 
// First decode with inJustDecodeBounds=true to 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 with 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 and 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 the largest inSampleSize value that is a power of 2 and keeps both 
    // height and width larger than the requested height and width. 
    while ((halfHeight/inSampleSize) > reqHeight 
      && (halfWidth/inSampleSize) > reqWidth) { 
     inSampleSize *= 2; 
    } 

    // This offers some additional logic in case the image has a strange 
    // aspect ratio. For example, a panorama may have a much larger 
    // width than height. In these cases the total pixels might still 
    // end up being too large to fit comfortably in memory, so we should 
    // be more aggressive with sample down the image (=larger inSampleSize). 

    long totalPixels = width * height/inSampleSize; 

    // Anything more than 2x the 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) 
} 

} 

呼叫我還沒有嘗試過的方法

Bitmap bmp = ImageResizer.decodeSampledBitmapFromFile(new File(filePath).getAbsolutePath(), 512, 342);    

但如下可能工作傳遞方法的參數一樣
imageView.getWidth(),ImageView的。 getHeight()

+0

我必須使用此方法設置ImageView的高度和寬度?現在,我只是路過的spritesheet的原始尺寸,結果看起來是一樣的。 – Cipher