简体   繁体   English

根据XML中Android-ImageView的大小进行图像缩放

[英]Making an image scale according to the Android-ImageView's size in XML

I am working on an Android application and since I am a beginner in Android development, I have a difficulty about designing my UI. 我正在开发一个Android应用程序,并且由于我是Android开发的初学者,所以在设计UI时遇到了困难。

The UI consists of many images which acts as small widgets on the screen. UI由许多图像组成,这些图像充当屏幕上的小控件。 I want that these images to always cover a certain portion of the screen, such that the layout is compatible with different devices of different screen sizes. 我希望这些图像始终覆盖屏幕的特定部分,以使布局与具有不同屏幕尺寸的不同设备兼容。 For example, I have a panel, which I want to be aligned with the top edge of the screen and cover %30 of the screen beginning from the top edge. 例如,我有一个面板,我想使其与屏幕的顶部边缘对齐,并从顶部边缘开始覆盖屏幕的%30。 I know that I am able to achieve this by mixing Relative and LinearLayouts appropriately in the layout xml, but what I fail is to make an image I am putting into an ImageView resize according to the ImageView's size on the screen. 我知道我可以通过在布局xml中适当地混合Relative和LinearLayouts来实现这一点,但是我做不到的是根据ImageView在屏幕上的大小来制作要放入ImageView的图像。 In my case it seems that the ImageView itself resizes itself according to the image I am putting in it. 在我看来,ImageView本身会根据我放入的图像调整其大小。 In other words, if I have an ImageView object which covers the upper %30 of the screen, I want it to remain the same after I put an image into it. 换句话说,如果我有一个ImageView对象覆盖了屏幕的上方%30,则我希望在将图像放入屏幕后该对象保持不变。 (The image may be distorted a little, it is not much a concern for me in this case). (图像可能会有些变形,在这种情况下,我不必担心)。

First of all, for Android SDK there is guideline available to create an app for different screen size. 首先,对于Android SDK,有一些指南可用于创建适用于不同屏幕尺寸的应用。 Follow this: http://developer.android.com/guide/practices/screens_support.html 请遵循以下步骤: http : //developer.android.com/guide/practices/screens_support.html

If you want to have a view, which appears with specific amount of area like 30% or 80%, you can use android:layout_weight attribute in the layout instead of defining the specific height or width. 如果您想要一个视图,该视图以特定的面积(例如30%或80%)显示,则可以在布局中使用android:layout_weight属性,而不用定义特定的高度或宽度。

If you want your image will always fill the ImageView , you should use android:scaleType="fitXY" attribute within your ImageView. 如果您希望图像始终填充ImageView ,则应在ImageView使用android:scaleType="fitXY"属性。

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

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