简体   繁体   English

如何在Android的ImageView中平均缩放图像X和Y?

[英]How to scale image X and Y within Android's ImageView equally?

I am trying to get a logo to have the same scaling no matter the size of the parent's view. 我试图使徽标具有相同的缩放比例,无论父视图的大小如何。 Right now the logo is scaled down vertically but no horizontally causing the logo to have a smushed look to it. 现在,徽标在垂直方向上按比例缩小,但在水平方向上没有缩放,从而使徽标外观模糊不清。 I'd like it to scale both x and y equally so that it maintains it's form without a smushed or stretched look. 我希望它可以同时缩放x和y,以保持其外观不模糊或拉伸。

I have tried various scaleTypes, but they haven't seemed to help thus far. 我尝试了各种scaleTypes,但是到目前为止它们似乎没有帮助。

XML CODE FOR BUTTON: 按钮的XML代码:

<ImageView
                android:id="@+id/logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/title"
                android:background="@android:color/transparent"
                android:src="@android:color/transparent" />

FULL XML CODE (GIVE CONTEXT): 完整的XML代码(提供上下文):

<RelativeLayout
            android:id="@+id/menu"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="30"
            android:background="@android:color/transparent"
            android:baselineAligned="false" >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:gravity="right|bottom"
                android:shadowColor="@android:color/transparent"
                android:text="THE BLIND GOAT"
                android:textColor="@android:color/transparent"
                android:textSize="24dp"
                android:textStyle="bold|italic"
                android:typeface="sans" />

            <Button
                android:id="@+id/help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:paddingBottom="0dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:paddingTop="0dp"
                android:text="HELP"
                android:textColor="@android:color/transparent"
                android:textSize="9dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <Button
                android:id="@+id/settings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/help"
                android:layout_below="@id/title"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:paddingBottom="0dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:paddingTop="0dp"
                android:text="SETTINGS"
                android:textColor="@android:color/transparent"
                android:textSize="9dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <Button
                android:id="@+id/logout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/settings"
                android:layout_below="@id/title"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:paddingBottom="0dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:paddingTop="0dp"
                android:text="LOGOUT"
                android:textColor="@android:color/transparent"
                android:textSize="9dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <Button
                android:id="@+id/profile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/settings"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:textColor="@android:color/transparent"
                android:textSize="15dp"
                android:textStyle="bold"
                android:typeface="serif" />

            <ImageView
                android:id="@+id/logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/title"
                android:background="@android:color/transparent"
                android:src="@android:color/transparent"
                android:scaleType="centerCrop" />
        </RelativeLayout>

Your Image display is decided by two factors: 您的图片显示取决于两个因素:

The size of your ImageView (You can think of this as a Frame or Canvas which the image can be drawn onto) and it's position. ImageView的大小(可以将其视为可以在其上绘制图像的Frame或Canvas)及其位置。 If the ImageView is small then the image can only ever fill that amount of space. 如果ImageView很小,则图像只能填充该空间量。

The second factor is the scaleType which is described here . 第二个因素是scaleType其被描述在这里 If you tell the ImageView via it's scaleType attribute to use "center" it will draw from the center at the Images native size. 如果通过它的scaleType属性告诉ImageView使用“中心”,它将从中心以“图像”本机大小进行绘制。 If you use the scaleType "fit_center" the Image will scale itself to fit inside the ImageView with the correct proportions (it won't be stretched or squashed). 如果使用scaleType “ fit_center”,则图像将自动缩放以适合ImageView并具有正确的比例(不会被拉伸或挤压)。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM