我有一张 480 像素 x 40 像素的图像。我想在 ImageView 中显示此图像,而不需要缩放以适应。
例如:如果我的屏幕只有 320 像素宽,我希望只在屏幕上显示 320 像素的图像,而不是将其塞入 ImageView(即使这意味着图像的其余部分被截断)。
有没有办法防止它缩放以适应?
编辑:另外,我希望图像的左侧与设备屏幕的左侧对齐。
谢谢
I have an image that is 480 pixels by 40 pixels. I would like to show this image in an ImageView without having it scale to fit.
Ex: If my screen is only 320pixels wide I would like only 320pixels of the image to show on the screen, not have it cram itself into the ImageView (even if it means that the rest of the image gets cut off).
Is there a way to prevent it from scaling to fit?
EDIT: Also, I would like the left side of the image to line up with the left side of the device's screen.
Thanks
发布评论
评论(6)
使用
ScaleType.CENTER
,就像 XML 中的这样:这将“使图像在视图中居中,但不执行缩放”
在此处阅读更多信息:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
(这是顺便说一下谷歌搜索的第一个点击“imageview 规模”)
Use
ScaleType.CENTER
, like this in XML:This will "center the image in the view, but perform no scaling"
Read more here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
(This is by the way the first hit on googling "imageview scale")
使用 MATRIX 缩放类型将其设置为不缩放且不居中
它将使用单位矩阵,除非您使用 setImageMatrix(matrix); 设置它;
或者用Java:
Use the MATRIX scaletype to set it for no scaling and no centering
It will use the identity matrix unless you set it using setImageMatrix(matrix);
Or in Java:
试试这个:
Try this one:
如果
不起作用,
检查您如何设置图像。
你必须使用 android:src,而不是 android:backgorund
所以
If
doesn't work,
check how do you set your image.
You have to use android:src, not android:backgorund
so
我遇到了类似的问题,我有一个固定大小的图形,我想缩放到屏幕,但保持其比例为 480 x 200。
我在高度上使用 Fill_parent,以便它会缩放并在宽度上包裹内容。
以及“父级左对齐”和“父级顶部对齐”。
在您的情况下,您可以只使用wrapp_content以及Align Parent Left。
然后我在正常屏幕上使用右边距 180dp 调整宽度,以确保其保持宽度。
I had a similar issue where I had a fixed size graphic that I wanted to scale to the screen but stay in proportion it was 480 x 200.
I used Fill_parent on the height so it would scale and wrap_content on the width.
As well as Align Parent Left and Align Parent Top.
In your case you could just use wrap_content on both as well as Align Parent Left.
Then I tweeked the width using Right Margin 180dp on normal screens to be sure it maintained the width.
我的问题是我在 xml 布局文件中设置了 Scaletype,通过在 Java 代码中手动设置图片。
解决方案是在 Java 代码而不是 XML 文件中设置比例类型。
这是对我有用的:
My Problem was that I set the Scaletype in the xml-layout-file, through I set the picture manually in Java-Code.
The Solution was to set the scale-type in Java-Code instead the XML-file.
Here is what has worked for me: