0

如何将来自 http 请求的任何大小的图像放入大小为 100x100 的 ImageView 中。

    android:layout_width="100dp"
    android:layout_height="100dp"

我想避免失去准确性,我想看到完全相同的图像,但尺寸为 100x100。我试过 Bitmap.createScaledBitmap(bitmap, 100, 100, true); 但失败了。

这样做的正确步骤是什么?

4

2 回答 2

0

看看这个:
“这不能仅在 xml 中完成。不可能同时缩放图像和 ImageView,以便图像的一维始终为 100dp,而 ImageView 将具有与图像相同的尺寸。

此代码缩放 ImageView 的 Drawable 以保持在 100dp x 100dp 之类的正方形中,一维恰好为 100dp 并保持纵横比。然后调整 ImageView 的大小以匹配缩放图像的尺寸。该代码用于活动。我通过按钮单击处理程序对其进行了测试。

private void scaleImage()
{
// Get the ImageView and its bitmap
ImageView view = (ImageView) findViewById(R.id.image_box);
Drawable drawing = view.getDrawable();
if (drawing == null) {
    return; // Checking for null & return, as suggested in comments
}
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

// Get current dimensions AND the desired bounding box
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int bounding = dpToPx(250);
Log.i("Test", "original width = " + Integer.toString(width));
Log.i("Test", "original height = " + Integer.toString(height));
Log.i("Test", "bounding = " + Integer.toString(bounding));

// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.  
float xScale = ((float) bounding) / width;
float yScale = ((float) bounding) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Log.i("Test", "xScale = " + Float.toString(xScale));
Log.i("Test", "yScale = " + Float.toString(yScale));
Log.i("Test", "scale = " + Float.toString(scale));

// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);

// Create a new bitmap and convert it to a format understood by the ImageView 
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
width = scaledBitmap.getWidth(); // re-use
height = scaledBitmap.getHeight(); // re-use
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Log.i("Test", "scaled width = " + Integer.toString(width));
Log.i("Test", "scaled height = " + Integer.toString(height));

// Apply the scaled bitmap
view.setImageDrawable(result);

// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
params.width = width;
params.height = height;
view.setLayoutParams(params);

Log.i("Test", "done");
}

private int dpToPx(int dp)
{
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}

ImageView 的 xml 代码:

<ImageView a:id="@+id/image_box"
a:background="#ff0000"
a:src="@drawable/star"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_marginTop="20dp"
a:layout_gravity="center_horizontal"/>"

资源

于 2013-08-29T10:44:18.780 回答
0

查看此链接:将 图像放入 ImageView,保持纵横比,然后将 ImageView 调整为图像尺寸?

ImageView 是固定大小的,与图像大小无关

尝试 android:scaleType="fitXY" 获取更大的图像

于 2013-08-29T10:47:02.080 回答