简体   繁体   English

将此图像(具有正确的尺寸)插入我的Android项目的最佳方法是什么?

[英]What is the best way to insert this image (with the correct dimension) into my Android project?

I am absolutly new in Android development and I have some doubt about the correct way to insert an header image into my first app. 我在Android开发方面绝对是新手,我对将标题图像插入第一个应用程序的正确方法有所怀疑。

So I have the following situation: I have to insert an image as header (an immage and under it there is the content, the classic web site header concept), so I have done something like this: 所以我有以下情况:我必须插入一个图像作为标题(一个immage,下面有内容,经典的网站标题概念),所以我做了这样的事情:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="0dp">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:scaleType="fitStart"
        android:layout_height="250dp"
        android:background="@drawable/carbonara" />
        <!--
        android:src="@drawable/carbonara"/>
        android:background="@drawable/carbonara" />
        -->


    <TextView android:id="@android:id/text1"
        style="@style/pastaTitleTextStyle" />


</LinearLayout>

As you can see I am using this setting to set the header image: 正如您所看到的,我正在使用此设置来设置标题图像:

android:background="@drawable/carbonara"

So I am using the android:background property to set the carbonara.jpg image as background of the LinearLayout header that have to horizontally fill the screen (because I have specified: android:layout_width="fill_parent" ). 所以我使用android:background属性将carbonara.jpg图像设置为必须水平填充屏幕的LinearLayout标题的背景(因为我指定了: android:layout_width =“fill_parent” )。

Ok, so using the android:background settings it seems works fine on all the screen (this is what I say changing the device in the Android Studio activity preview) because I am setting the image as background so I think that it is "stretched" to fill the space. 好的,所以使用android:后台设置似乎在所有屏幕上都能正常工作(这就是我说在Android Studio活动预览中更改设备),因为我将图像设置为背景所以我认为它是“拉伸”填补空间。

But I don't know if use the android:background is the right soution. 但我不知道是否使用android:background是正确的选择。 Someone say to me that it is better use: 有人对我说,最好使用:

android:src="@drawable/carbonara"

instead the android:background setting. 而是android:后台设置。 Is it true? 这是真的吗? Why if it is an header that have to be fill? 为什么如果它是一个必须填充的标题? (in HTML\\CSS it is pretty common use the background-image property to do this kind of stuff in layout...) (在HTML \\ CSS中,很常见的是使用background-image属性在布局中执行此类操作...)

So I tryied to change doing: 所以我试着改变做法:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:scaleType="fitStart"
    android:layout_height="250dp"
    android:src="@drawable/carbonara" />

The problem is that now the image don't fill the entire space for some screen size (or maybe also pixel density?) 问题是现在图像没有填满整个空间的某些屏幕尺寸(或者像素密度?)

For example this is the preview setting Nexus 5 as device: 例如,这是将Nexus 5预览设置为设备:

在此输入图像描述

As you can see on the right there is a white space. 正如您在右边看到的那样,有一个白色空间。 Why? 为什么? Is it the image not enought large for this screen or what? 这个屏幕的图像不是很大或者是什么?

Using android:background="@drawable/carbonara" instead android:src="@drawable/carbonara" I have not this problem and the image it is showed in a pretty decent way (maybe a litle horizontally stretched). 使用android:background =“@ drawable / carbonara”而不是android:src =“@ drawable / carbonara”我没有这个问题,并且它以相当不错的方式显示的图像(可能是水平伸展的痘痘)。

Is it really so horrible use android:background instead android:src for these situations? 是不是真的太可怕了用android:background而不是android:src用于这些情况?

So I really don't know what is the correct solution to handle this kind of problem: if the use of android:background is not good what have I to do? 所以我真的不知道处理这类问题的正确解决办法是什么:如果使用android:background不好我有什么办法? Have I to create different version with different size of my image and put it in the specific path of the /res/ folder? 我是否要创建具有不同大小的图像的不同版本并将其放在/ res /文件夹的特定路径中? Or what? 或者是什么? I am a bit 'confused. 我有一点困惑。

Is it really so horrible use android:background instead android:src for these situations? 是不是真的太可怕了用android:background而不是android:src用于这些情况?

Its not about background and rsc attributes. 它不是关于backgroundrsc属性。 Android runs on a variety of devices that offer different screen sizes and densities (dpi).You can't use same image for Nexus 5 and Nexus S . Android可在各种设备上运行,这些设备提供不同的屏幕尺寸和密度(dpi)。您无法为Nexus 5Nexus S使用相同的图像。

Solution is: You will have to make multiple image based on screen size and dpi 解决方案是:您必须根据屏幕大小和dpi制作多个图像

A set of six generalized densities: 一组六个广义密度:

-ldpi (low) ~120dpi -ldpi(低)~120dpi
-mdpi (medium) ~160dpi -mdpi(中)~160dpi
-hdpi (high) ~240dpi Nexus S -hdpi(高)~240dpi Nexus S
-xhdpi (extra-high) ~320dpi Moto G -xhdpi(超高)~320dpi Moto G
-xxhdpi (extra-extra-high) ~480dpi Nexus 5 -xxhdpi(超高)~480dpi Nexus 5
-xxxhdpi (extra-extra-extra-high) ~640dpi Galaxy S6 and S7 -xxxhdpi(额外超高)~640dpi Galaxy S6S7

Read more Supporting Multiple Screens 阅读更多支持多个屏幕

When using an ImageView android:src is the right choice because it helps you to get the right fitting for your image. 当使用ImageView android:src是正确的选择,因为它可以帮助您获得适合您的图像。 As already said you can use android:scaleType to tell the system how your image should be displayed. 如前所述,您可以使用android:scaleType告诉系统应该如何显示图像。 Possible values are shown here 此处显示可能的值
So if you want your image in the same aspect ratio but fitting the bounds you should choose center_crop but this could crop some parts of the image on the top/bottom. 因此,如果您希望图像具有相同的宽高比但适合边界,则应选择center_crop,但这可能会在顶部/底部裁剪图像的某些部分。 About the image size: you may have heard of the density buckets used in Android: they range from LDPI to XXXHDPI which are connected to scale factors. 关于图像大小:您可能听说过Android中使用的密度桶:它们的范围从LDPI到XXXHDPI,它们与比例因子相关联。 You can find more details about this here . 你可以在这里找到更多相关细节。 You are also guessing right that you should have your image in different solutions available in the density bucket folder under *res/**. 你也猜对了,你应该在* res / **下的密度桶文件夹中提供不同解决方案中的图像。 On a Nexus 5X your image should be 1080pixels width to fit the entire screen. 在Nexus 5X上,您的图像宽度应为1080像素,以适应整个屏幕。 The density bucket used on this phone is XXHDPI. 此手机使用的密度桶为XXHDPI。 You can find some more details about this metrics here . 您可以在此处找到有关此指标的更多详细信息。

android:background exists for all the view. 所有视图都存在android:background。 As the name suggests this is what is going to be there in the background. 顾名思义,这就是背景中的情况。

android:src exists for ImageViews and its subclasses. android:src存在于ImageViews及其子类中。 You can think of this as the foreground. 您可以将此视为前景。 Because ImageView is a subclass of View you even have android:background for that. 因为ImageView是View的子类,所以你甚至可以使用android:background。

If the foreground image is smaller than background image then the background portion which is not covered by the foreground would be visible. 如果前景图像小于背景图像,则可以看到未被前景覆盖的背景部分。

Also, you can use transparency in the foreground in which case the background would be visible(transparently). 此外,您可以在前景中使用透明度,在这种情况下,背景将是可见的(透明)。

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

相关问题 将大列表插入SQL数据库的最佳方法是什么? [ANDROID] - What is the best way to insert large lists into a SQL database? [ANDROID] 将 Spring Security 插入项目的最佳方法是什么? 我有两种不同的方法来做到这一点 - What is the best way to insert Spring Security into a project? I have 2 differents way to do it 将图像序列化(与Swing兼容)从Java到Android的最佳方法是什么? - What is the best way to serialize an image (compatible with Swing) from Java to Android? 在 Android 上切换图像捕捉和视频捕捉的最佳方式是什么? - What is the best way to switch between Image Capture and Video Capture on Android? 在 android camerax 图像分析中检测文档角的最佳方法是什么? - what is the best way to detect document corners in android camerax Image analysis? ManyToMany:采用JPA的正确或最佳方法是什么? - ManyToMany: what is the correct or best way to go in JPA? 将数据插入数据库-最好的方法是什么 - Insert data into Database - What is the best way to do it 将条目插入ElasticSearch的最佳方法是什么? - What is the Best way to insert Entries into ElasticSearch? 我的项目中正确的相对路径是什么? - What is correct relative path in my project? 在tomcat中部署模型项目的最佳方法是什么 - what is the best way to deploy model project in tomcat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM