在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.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));
其他回答
你不需要任何java代码。你只需要:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
键在width和height的匹配父项中
以编程方式应用宽高比到Imageview:
aspectRatio = imageWidth/imageHeight
ratioOfWidth = imageWidth/maxWidth
ratioOfHeight = imageHeight/maxHeight
if(ratioOfWidth > ratioOfHeight){
imageWidth = maxWidth
imageHeight = imageWidth/aspectRatio
} else if(ratioOfHeight > ratioOfWidth){
imageHeight = maxHeight
imageWidth = imageHeight * aspectRatio
}
之后,您可以使用缩放位图图像视图
Bitmap scaledBitmap= Bitmap.createScaledBitmap(bitmap, (int) imageWidth , (int) imageHeight , true);
看一下ImageView。ScaleType来控制和理解在ImageView中发生大小调整的方式。当图像被调整大小时(同时保持其纵横比),图像的高度或宽度可能会小于ImageView的维度。
对于你们中的任何人,想要图像精确地适应imageview适当缩放,不裁剪使用
imageView.setScaleType(ScaleType.FIT_XY);
imageView是代表你的imageView的视图
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.