In Android, I defined an ImageView
's layout_width
to be fill_parent
(which takes up the full width of the phone).
在Android中,我将ImageView的layout_width定义为fill_parent(占整个手机宽度)。
If the image I put to ImageView
is bigger than the layout_width
, Android will scale it, right? But what about the height? When Android scales the image, will it keep the aspect ratio?
如果我放到ImageView的图像大于layout_width, Android会缩放它,对吧?但是高度呢?当Android扩展图像时,它会保持纵横比吗?
What I find out is that there is some white space at the top and bottom of the ImageView
when Android scales an image which is bigger than the ImageView
. Is that true? If yes, how can I eliminate that white space?
我发现,当Android缩放一个比ImageView大的图像时,在ImageView的顶部和底部都有一些空白。这是真的吗?如果是,我如何消除空白?
21 个解决方案
#1
671
-
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 thanandroid:background="..."
.src=
makes it scale the image maintaining aspect ratio, butbackground=
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.)是的,默认情况下,Android会将你的图像缩小到适合ImageView,保持纵横比。但是,请确保使用android:src="…"而不是android:background="…"将图像设置为ImageView。src=缩放图像保持长宽比,而background=缩放和扭曲图像,使其与ImageView的大小一致。(可以同时使用背景和源代码,这对于像在主图像周围显示框架这样的事情很有用,只需使用一个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.您还应该看到android:调整视图边界以使ImageView调整自身以适应被重新扫描的图像。例如,如果在通常的方形ImageView中有一个矩形图像,那么调整视图边界=true将使它将ImageView的大小也调整为矩形。这将影响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.然后,正如Samuh所写的,您可以使用android:scaleType参数改变默认的缩放图像的方式。顺便说一句,发现这是如何工作的最简单的方法就是自己尝试一下!只要记住查看模拟器本身(或实际的电话)中的布局,因为Eclipse中的预览通常是错误的。
#2
244
看到android:adjustViewBounds。
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
如果您希望ImageView调整其范围以保持其可绘制的方面比率,请将其设置为true。
#3
142
To anyone else having this particular issue. You have an ImageView
(or other View
) that you want to have a width of fill_parent
and a height scaled proportionately:
对于任何有这个问题的人。您有一个ImageView(或其他视图),您希望设置fill_parent的宽度和按比例缩放的高度:
Add these two attributes to your ImageView
:
将这两个属性添加到ImageView中:
android:adjustViewBounds="true"
android:scaleType="centerCrop"
And set the ImageView
width to fill_parent
and height to wrap_content
.
并将ImageView宽度设置为fill_parent,高度设置为wrap_content。
Also, if you don't want your image to be cropped, try this:
此外,如果你不希望你的形象被裁剪,试试这个:
android:adjustViewBounds="true"
android:layout_centerInParent="true"
#4
46
If you want an ImageView
that both scales up and down while keeping the proper aspect ratio, add this to your XML:
如果您想要一个图像视图,在保持适当的纵横比的同时上下伸缩,请将其添加到您的XML中:
android:adjustViewBounds="true"
android:scaleType="fitCenter"
Add this to your code:
将此添加到代码中:
// 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);
It took me a while to get this working, but this appears to work in the cases both where the image is smaller than the screen width and larger than the screen width, and it does not box the image.
我花了一段时间来完成这个工作,但这似乎是在两种情况下工作的:图像比屏幕宽度小,比屏幕宽度大,而且它没有框出图像。
#5
9
This worked for me:
这工作对我来说:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="39dip"
android:scaleType="centerCrop"
android:adjustViewBounds ="true"
#6
8
Below code Working for scale image as aspect ratio:
下面的代码工作的比例图像的纵横比:
Bitmap bitmapImage = BitmapFactory.decodeFile("Your path");
int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
your_imageview.setImageBitmap(scaled);
#7
7
Take a look at ImageView.ScaleType to control and understand the way resizing happens in an ImageView
. When the image is resized (while maintaining its aspect ratio), chances are that either the image's height or width becomes smaller than ImageView
's dimensions.
看看ImageView。ScaleType用于控制和理解在ImageView中调整大小的方式。当图像被调整大小(同时保持高宽比)时,图像的高度或宽度都可能小于ImageView的尺寸。
#8
7
this solved my problem
这解决了我的问题
android:adjustViewBounds="true"
android:scaleType="fitXY"
#9
4
I have an image smaller than the screen. To have it stretched proportionally to the max and centered in the view I had to use the following code:
我有一个比屏幕小的图像。为了使它成比例地延伸到最大,并以视图为中心,我必须使用以下代码:
<ImageView
android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
Have in mind though, that if you have a relative layout and have some elements set to be above or below the ImageView, they will be most likely overlapped by the image.
但是请记住,如果您有一个相对布局,并且有一些元素设置在ImageView的上方或下方,那么它们很可能会被图像重叠。
#10
3
For anyone of you who wants the image to fit exact the imageview with proper scaling and no cropping use
对于任何想要图像符合精确的imageview的人来说,适当的缩放和不使用裁剪
imageView.setScaleType(ScaleType.FIT_XY);
where imageView is the view representing your ImageView
表示imageView的视图在哪里
#11
3
You can calculate screen width. And you can scale bitmap.
你可以计算屏幕宽度。你可以缩放位图。
public static float getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float pxWidth = outMetrics.widthPixels;
return pxWidth;
}
calculate screen width and scaled image height by screen width.
计算屏幕宽度和按屏幕宽度缩放的图像高度。
float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}
After you can scale bitmap.
在你可以缩放位图之后。
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);
#12
3
Use these properties in ImageView to keep aspect ratio:
在ImageView中使用这些属性来保持长宽比:
android:adjustViewBounds="true"
android:scaleType="fitXY"
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
#13
3
When doing this programmatically, be sure to call the setters in the correct order:
在以编程方式进行此操作时,请确保以正确的顺序调用setter:
imageView.setAdjustViewBounds(true)
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
#14
2
If you want your image occupy the maximum possible space then the best option would be
如果你想让你的图像占据尽可能大的空间,那么最好的选择就是
android:layout_weight="1"
android:scaleType="fitCenter"
#15
1
Try using android:layout_gravity
for ImageView:
尝试使用android:layout_gravity用于ImageView:
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
The example above worked for me.
上面的例子对我有用。
#16
1
I have an algorithm to scale a bitmap to bestFit the container dimensions, maintaining its aspect ratio. Please find my solution here
我有一个算法来缩放位图,使之最适合容器的尺寸,保持它的纵横比。请在这里找到我的答案
Hope this helps someone down the lane!
希望这能帮到别人!
#17
1
Yo don't need any java code. You just have to :
您不需要任何java代码。你只需要:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
The key is in the match parent for width and height
键在匹配父元素中表示宽度和高度
#18
0
I use this:
我用这个:
<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" />
#19
0
Pass your imageview and based on screen height and width you can make it
public void setScaleImage(EventAssetValueListenerView view){
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float xScale = ((float) 4) / width;
float yScale = ((float) 4) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
view.setImageDrawable(result);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
#20
-1
myImageView.setAdjustViewBounds(true);
#21
-3
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));
#1
671
-
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 thanandroid:background="..."
.src=
makes it scale the image maintaining aspect ratio, butbackground=
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.)是的,默认情况下,Android会将你的图像缩小到适合ImageView,保持纵横比。但是,请确保使用android:src="…"而不是android:background="…"将图像设置为ImageView。src=缩放图像保持长宽比,而background=缩放和扭曲图像,使其与ImageView的大小一致。(可以同时使用背景和源代码,这对于像在主图像周围显示框架这样的事情很有用,只需使用一个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.您还应该看到android:调整视图边界以使ImageView调整自身以适应被重新扫描的图像。例如,如果在通常的方形ImageView中有一个矩形图像,那么调整视图边界=true将使它将ImageView的大小也调整为矩形。这将影响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.然后,正如Samuh所写的,您可以使用android:scaleType参数改变默认的缩放图像的方式。顺便说一句,发现这是如何工作的最简单的方法就是自己尝试一下!只要记住查看模拟器本身(或实际的电话)中的布局,因为Eclipse中的预览通常是错误的。
#2
244
看到android:adjustViewBounds。
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
如果您希望ImageView调整其范围以保持其可绘制的方面比率,请将其设置为true。
#3
142
To anyone else having this particular issue. You have an ImageView
(or other View
) that you want to have a width of fill_parent
and a height scaled proportionately:
对于任何有这个问题的人。您有一个ImageView(或其他视图),您希望设置fill_parent的宽度和按比例缩放的高度:
Add these two attributes to your ImageView
:
将这两个属性添加到ImageView中:
android:adjustViewBounds="true"
android:scaleType="centerCrop"
And set the ImageView
width to fill_parent
and height to wrap_content
.
并将ImageView宽度设置为fill_parent,高度设置为wrap_content。
Also, if you don't want your image to be cropped, try this:
此外,如果你不希望你的形象被裁剪,试试这个:
android:adjustViewBounds="true"
android:layout_centerInParent="true"
#4
46
If you want an ImageView
that both scales up and down while keeping the proper aspect ratio, add this to your XML:
如果您想要一个图像视图,在保持适当的纵横比的同时上下伸缩,请将其添加到您的XML中:
android:adjustViewBounds="true"
android:scaleType="fitCenter"
Add this to your code:
将此添加到代码中:
// 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);
It took me a while to get this working, but this appears to work in the cases both where the image is smaller than the screen width and larger than the screen width, and it does not box the image.
我花了一段时间来完成这个工作,但这似乎是在两种情况下工作的:图像比屏幕宽度小,比屏幕宽度大,而且它没有框出图像。
#5
9
This worked for me:
这工作对我来说:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="39dip"
android:scaleType="centerCrop"
android:adjustViewBounds ="true"
#6
8
Below code Working for scale image as aspect ratio:
下面的代码工作的比例图像的纵横比:
Bitmap bitmapImage = BitmapFactory.decodeFile("Your path");
int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
your_imageview.setImageBitmap(scaled);
#7
7
Take a look at ImageView.ScaleType to control and understand the way resizing happens in an ImageView
. When the image is resized (while maintaining its aspect ratio), chances are that either the image's height or width becomes smaller than ImageView
's dimensions.
看看ImageView。ScaleType用于控制和理解在ImageView中调整大小的方式。当图像被调整大小(同时保持高宽比)时,图像的高度或宽度都可能小于ImageView的尺寸。
#8
7
this solved my problem
这解决了我的问题
android:adjustViewBounds="true"
android:scaleType="fitXY"
#9
4
I have an image smaller than the screen. To have it stretched proportionally to the max and centered in the view I had to use the following code:
我有一个比屏幕小的图像。为了使它成比例地延伸到最大,并以视图为中心,我必须使用以下代码:
<ImageView
android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
Have in mind though, that if you have a relative layout and have some elements set to be above or below the ImageView, they will be most likely overlapped by the image.
但是请记住,如果您有一个相对布局,并且有一些元素设置在ImageView的上方或下方,那么它们很可能会被图像重叠。
#10
3
For anyone of you who wants the image to fit exact the imageview with proper scaling and no cropping use
对于任何想要图像符合精确的imageview的人来说,适当的缩放和不使用裁剪
imageView.setScaleType(ScaleType.FIT_XY);
where imageView is the view representing your ImageView
表示imageView的视图在哪里
#11
3
You can calculate screen width. And you can scale bitmap.
你可以计算屏幕宽度。你可以缩放位图。
public static float getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float pxWidth = outMetrics.widthPixels;
return pxWidth;
}
calculate screen width and scaled image height by screen width.
计算屏幕宽度和按屏幕宽度缩放的图像高度。
float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}
After you can scale bitmap.
在你可以缩放位图之后。
Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);
#12
3
Use these properties in ImageView to keep aspect ratio:
在ImageView中使用这些属性来保持长宽比:
android:adjustViewBounds="true"
android:scaleType="fitXY"
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
#13
3
When doing this programmatically, be sure to call the setters in the correct order:
在以编程方式进行此操作时,请确保以正确的顺序调用setter:
imageView.setAdjustViewBounds(true)
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
#14
2
If you want your image occupy the maximum possible space then the best option would be
如果你想让你的图像占据尽可能大的空间,那么最好的选择就是
android:layout_weight="1"
android:scaleType="fitCenter"
#15
1
Try using android:layout_gravity
for ImageView:
尝试使用android:layout_gravity用于ImageView:
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
The example above worked for me.
上面的例子对我有用。
#16
1
I have an algorithm to scale a bitmap to bestFit the container dimensions, maintaining its aspect ratio. Please find my solution here
我有一个算法来缩放位图,使之最适合容器的尺寸,保持它的纵横比。请在这里找到我的答案
Hope this helps someone down the lane!
希望这能帮到别人!
#17
1
Yo don't need any java code. You just have to :
您不需要任何java代码。你只需要:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
The key is in the match parent for width and height
键在匹配父元素中表示宽度和高度
#18
0
I use this:
我用这个:
<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" />
#19
0
Pass your imageview and based on screen height and width you can make it
public void setScaleImage(EventAssetValueListenerView view){
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float xScale = ((float) 4) / width;
float yScale = ((float) 4) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
view.setImageDrawable(result);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
#20
-1
myImageView.setAdjustViewBounds(true);
#21
-3
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));