在Android中,我定义了一个ImageView的layout_width为fill_parent(它占用了手机的全宽度)。
如果我放入ImageView的图像大于layout_width, Android会缩放?那么高度呢?当Android缩放图像时,它会保持纵横比吗?
我发现在ImageView的顶部和底部有一些空白当Android缩放一个比ImageView大的图像时。这是真的吗?如果是,我如何消除空白?
在Android中,我定义了一个ImageView的layout_width为fill_parent(它占用了手机的全宽度)。
如果我放入ImageView的图像大于layout_width, Android会缩放?那么高度呢?当Android缩放图像时,它会保持纵横比吗?
我发现在ImageView的顶部和底部有一些空白当Android缩放一个比ImageView大的图像时。这是真的吗?如果是,我如何消除空白?
当前回答
我用这个:
<ImageView
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="@drawable/logo" />
其他回答
如果你想要一个ImageView在保持适当的纵横比的情况下,既能放大又能缩小,把这个添加到你的XML中:
android:adjustViewBounds="true"
android:scaleType="fitCenter"
在代码中添加:
// We need to adjust the height if the width of the bitmap is
// smaller than the view width, otherwise the image will be boxed.
final double viewWidthToBitmapWidthRatio = (double)image.getWidth() / (double)bitmap.getWidth();
image.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
我花了一段时间才让它工作,但这似乎在图像小于屏幕宽度和大于屏幕宽度的情况下都可以工作,而且它不会将图像框起来。
你可以计算屏幕宽度。你可以缩放位图。
public static float getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float pxWidth = outMetrics.widthPixels;
return pxWidth;
}
计算屏幕宽度和按屏幕宽度缩放的图像高度。
float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}
之后就可以缩放位图了。
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);
Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.) You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView. Then as Samuh wrote, you can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.
您可以缩放图像,这也将减少图像的大小。 你可以下载并使用它的库。 https://github.com/niraj124124/Images-Files-scale-and-compress.git
如何使用 1)导入压缩器-v1.0。Jar到您的项目。 2)添加下面的示例代码进行测试。 ResizeLimiter resize = ImageResizer.build(); 调整大小。scale("inputImagePath", "outputImagePath",imageWidth, imageHeight); 根据您的要求,还有更多的方法
你不需要任何java代码。你只需要:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
键在width和height的匹配父项中