简体   繁体   English

背景图像或背景颜色?

[英]background image or background color?

If you have an activity and you need to set the background color of the activity, simply you would set the background color as 如果您有一个活动,并且需要设置活动的背景色,只需将背景色设置为

android:background="#0000FF"

or prefer to use an image as 或更喜欢将图像用作

android:background="@drawable/bgimg"

, taking performance and space requirement into consideration? ,考虑性能和空间要求?

I think as using image consume space, when ever possible this approach should be avoided. 我认为使用图像会占用空间,因此应尽可能避免这种方法。 Is it so, or this really does not make a significant difference? 是这样,还是真的没有太大的不同? Just need to be sure from the experts. 只需从专家那里确定即可。

thanks. 谢谢。

First of all this depends upon your need. 首先,这取决于您的需要。 If you only want a plain Red background then applying background color should be the preferred one. 如果只希望使用纯红色背景,则应首选使用背景色。

As working with images may have few odds, 由于处理图片的几率很小,

  1. Scaling of the image for various screen sizes. 针对各种屏幕尺寸缩放图像。
  2. They definitely consume memory. 他们肯定会消耗内存。
  3. May not give the desired effect (blurry at times) if re-sized by the OS automatically. 如果由操作系统自动调整大小,则可能无法达到预期的效果(有时会模糊)。

On the other hand if you need something like a combination of colors then Gradient will help us. 另一方面,如果您需要类似颜色的组合,那么Gradient将为我们提供帮助。 (not for the complex ones though :)) (虽然不是复杂的:)

At last if you have an image type of background then drawable images will certainly be the choice. 最后,如果您具有背景图像类型,则可以选择可绘制图像。

color would be better, since you might have to use different image sizes to suit different screens, which in turn will increase the app size and you might not be very sure that the images are well displayed. 颜色会更好,因为您可能必须使用不同的图像大小以适合不同的屏幕,这反过来会增加应用程序的大小,并且您可能不确定图像是否能正确显示。 And if you opt for higher resolution images, chances are that the app might crash with a fatal signal error just because it runs out of memory. 而且,如果您选择更高分辨率的图像,则该应用程序可能会由于内存不足而崩溃,并出现致命的信号错误。

And @Atul O Holic's answer is complete too. @Atul O Holic的回答也很完整。

As little as the difference could be, the color approach is always going to be cheaper when we think in the space required, because you don't need to have a file with the image resource. 尽可能少的差异是,当我们在所需的空间内考虑时,彩色方法总是会更便宜,因为您不需要具有图像资源的文件。 Also, you can always use a native color from android using android:color/[color] . 另外,您始终可以使用android:color / [color]从android使用本机颜色。 Using an image as background is only better when you can't have the same effect by just using a single color as background. 仅当您仅使用单一颜色作为背景而无法获得相同效果时,才将图像用作背景更好。

如果是一种纯色,则只需设置背景色(您也可以在资源中设置颜色,这样就无需继续设置它!)。

Totally agree with joa2fast4u, when targeting multiple devices, especially ones with low memory, any optimizations you can make to save memory/reads/writes is preferred. 完全同意joa2fast4u,当针对多个设备(尤其是内存较低的设备)时,可以进行任何优化以节省内存/读取/写入的操作都是首选。 Also, you can specify many preset styles in your styles/themes resource files to propagate and control the entire theme of your app from the default background to the text color on your widgets. 另外,您可以在样式/主题资源文件中指定许多预设样式,以传播和控制应用程序的整个主题,从默认背景到小部件上的文本颜色。

1) Solid Color - Apply a background color 1)纯色-应用背景色

2) Multiple Colors - Use a Gradient 2)多种颜色-使用渐变

3) Anything more complex - Use images 3)更复杂-使用图片

Please note that the usage of images involves optimization of multiple screen resolutions & sizes. 请注意,图像的使用涉及多种屏幕分辨率和尺寸的优化。 File size & device form factor is also an important factor to be considered here. 文件大小和设备尺寸也是此处要考虑的重要因素。

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

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