Android:从位图裁剪位图并设置为 ImageView src
Posted
技术标签:
【中文标题】Android:从位图裁剪位图并设置为 ImageView src【英文标题】:Android: Crop Bitmap from Bitmap and set as ImageView src 【发布时间】:2015-04-25 10:34:51 【问题描述】:我正在尝试从 spritesheet 中裁剪一个 sprite,但是裁剪后的图像不是我指定的图像的一部分。
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、宽度或高度。
ImageView view = (ImageView)findViewById(R.id.player1);
view.setImageBitmap(sprite);
但显示的图像只是第一个精灵的一小部分。
我监督什么? view.setImageBitmap(sprite);
是正确的方法吗? ImageView在设置位图之前需要固定宽高吗?
编辑:也许我应该更清楚我的布局。在一个 LinearLayout 上排列了两个 ImageView。这是 XML:
<LinearLayout
android:orientation="horizontal"
android:layout_
android:layout_
android:baselineAligned="false">
<ImageView
android:layout_
android:layout_
android:scaleX="-1"
android:id="@+id/player1"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
<ImageView
android:layout_
android:layout_
android:id="@+id/player2"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
【问题讨论】:
***.com/questions/18232034/… 【参考方案1】: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()
【讨论】:
我必须用这个方法设置ImageView的高度和宽度吗?现在我只是传递 spritesheet 的原始尺寸,结果看起来是一样的。以上是关于Android:从位图裁剪位图并设置为 ImageView src的主要内容,如果未能解决你的问题,请参考以下文章