在android中在位图中拟合图像

在android中在位图中拟合图像,android,android-bitmap,Android,Android Bitmap,我想创建一个具有定义大小的新位图,例如200x200 Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); 现在我的理解是将sd卡的图像放入位图中,但将sd卡的图像调整为我创建的位图的大小 使用以下工具对ImageView执行我可以执行的操作: <ImageView android:src="@drawable/landscape" android:adjustViewBounds="false" /

我想创建一个具有定义大小的新位图,例如200x200

Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
现在我的理解是将sd卡的图像放入位图中,但将sd卡的图像调整为我创建的位图的大小

使用以下工具对ImageView执行我可以执行的操作:

<ImageView android:src="@drawable/landscape"
    android:adjustViewBounds="false" />
--


BitmapFactory中的
outWidth
outHeight
。选项
不设置生成图像的宽度和高度。只有在您更改采样大小时,才会更改它们,
inSampleSize
,并且将
inJustDecodeBounds
设置为
false
,否则它们将用于读取图像的高度和宽度

您可以尝试缩放位图并返回新位图的
createScaledBitmap

public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId), reqWidth, reqHeight, true);
}

我已经测试了此文档代码的许多变体:…我删除了calculateInSampleSize的部分。。。但是我无法将容器位图设置为200x200px发布您的代码。我已将代码添加到POST。我希望使用位图,而不是IMAGEVIEW。是的,经过这么多测试后,在代码中我没有定义位图的大小,。但事实上,经过这么多的测试,我不太确定该把它放在哪里。你能帮助我吗?这是imageView的xml:您的解决方案是可行的,但我有一个问题,创建一个1000x1000不调整图像,如上图。这是当前的结果:…想法?感谢什么问题?将
ImageView
android:layout\u width
android:layout\u height
设置为
wrap\u content
的xml格式。最后,我通过这个链接解决了这个问题:。。不管怎样,你的帮助我一直很有帮助,谢谢你。
imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.paisaje, 200, 200));
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId), reqWidth, reqHeight, true);
}