如何防止 Android Imageview 缩放?

发布于 2025-01-05 21:09:30 字数 215 浏览 4 评论 0 原文

我有一张 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

说好的呢 2025-01-12 21:09:31

使用 ScaleType.CENTER,就像 XML 中的这样:

android:scaleType="center"

这将“使图像在视图中居中,但不执行缩放”

在此处阅读更多信息:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

(这是顺便说一下谷歌搜索的第一个点击“imageview 规模”)

Use ScaleType.CENTER, like this in XML:

android:scaleType="center"

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")

泼猴你往哪里跑 2025-01-12 21:09:31

使用 MATRIX 缩放类型将其设置为不缩放且不居中

它将使用单位矩阵,除非您使用 setImageMatrix(matrix); 设置它;

android:scaleType="matrix"

或者用Java:

view.setScaleType(ImageView.ScaleType.MATRIX);

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);

android:scaleType="matrix"

Or in Java:

view.setScaleType(ImageView.ScaleType.MATRIX);
孤寂小茶 2025-01-12 21:09:31

试试这个:

                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"

Try this one:

                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
你对谁都笑 2025-01-12 21:09:31

如果

android:scaleType="center"

不起作用,
检查您如何设置图像。
你必须使用 android:src,而不是 android:backgorund

所以

 android:scaleType="center"
 android:src="@drawable/yourDrawable"

If

android:scaleType="center"

doesn't work,
check how do you set your image.
You have to use android:src, not android:backgorund

so

 android:scaleType="center"
 android:src="@drawable/yourDrawable"
心凉怎暖 2025-01-12 21:09:31

我遇到了类似的问题,我有一个固定大小的图形,我想缩放到屏幕,但保持其比例为 480 x 200。

我在高度上使用 Fill_parent,以便它会缩放并在宽度上包裹内容。
以及“父级左对齐”和“父级顶部对齐”。

在您的情况下,您可以只使用wrapp_content以及Align Parent Left。
然后我在正常屏幕上使用右边距 180dp 调整宽度,以确保其保持宽度。

<ImageView
    android:id="@+id/imageLayer"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="180dp"
    tools:ignore="ContentDescription" />

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.

<ImageView
    android:id="@+id/imageLayer"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="180dp"
    tools:ignore="ContentDescription" />
梓梦 2025-01-12 21:09:31

我的问题是我在 xml 布局文件中设置了 Scaletype,通过在 Java 代码中手动设置图片。
解决方案是在 Java 代码而不是 XML 文件中设置比例类型。

这是对我有用的:

// set image
backgroundImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

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:

// set image
backgroundImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文