How do I prevent scaling in Android Imageview?

守給你的承諾、 提交于 2019-11-30 06:46:59
nhaarman

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

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

Try this one:

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

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

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!