如何使任意大小的图像适合ImageView
在以下情况下:
例如,对于100*150的图像,图像和ImageView
应为166*250。
例如,对于150*100的图像,图像和ImageView
应为250*166。
如果我将边界设置为
<ImageView
android:id="@+id/picture"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />
图像适合ImageView
,但ImageView
始终为250dp * 250dp。
发布于 2011-11-23 04:06:55
(在澄清了原问题后,对答案进行了大量修改)
澄清后:
这个不能只在xml的中完成。不可能同时缩放图像和ImageView
,这样图像的一个维度将始终是250dp,而ImageView
将具有与图像相同的维度。
此代码将 Drawable
of a ImageView
缩放为一个正方形,如250dp x 250dp,其中一个维度恰好为250dp,并保持纵横比不变。然后调整ImageView
的大小以匹配缩放图像的尺寸。代码在活动中使用。我通过按钮点击处理程序进行了测试。
好好享受吧。:)
private void scaleImage(ImageView view) throws NoSuchElementException {
// Get bitmap from the the ImageView.
Bitmap bitmap = null;
try {
Drawable drawing = view.getDrawable();
bitmap = ((BitmapDrawable) drawing).getBitmap();
} catch (NullPointerException e) {
throw new NoSuchElementException("No drawable on given view");
} catch (ClassCastException e) {
// Check bitmap is Ion drawable
bitmap = Ion.with(view).getBitmap();
}
// Get current dimensions AND the desired bounding box
int width = 0;
try {
width = bitmap.getWidth();
} catch (NullPointerException e) {
throw new NoSuchElementException("Can't find bitmap on given view/drawable");
}
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"/>
多亏了这篇关于缩放代码的讨论:
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
更新7,2012年11月:
添加了注释中建议的空指针检查
发布于 2013-10-11 19:15:58
可能不是这个特定问题的答案,但是如果有人,像我一样,正在寻找如何在保持宽高比的同时将图像以有限的大小(例如,maxWidth
)适合于ImageView,然后摆脱ImageView占用的过多空间,那么最简单的解决方案就是在XML中使用以下属性:
android:scaleType="centerInside"
android:adjustViewBounds="true"
发布于 2013-12-10 15:15:47
<ImageView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
https://stackoverflow.com/questions/8232608
复制相似问题