简体   繁体   English

Nine-Patch Drawable vs Shape Drawable。哪个应该是首选?

[英]Nine-Patch Drawable vs Shape Drawable. Which should be preferred?

My co-worker and I developed two apps in parallel, each with similar styling. 我和我的同事并行开发了两个应用程序,每个应用程序都有类似的样式。 The main view background of these apps is a radial gradient. 这些应用程序的主视图背景是径向渐变。 He implemented his as a nine-patch image and I did it with a shape drawable. 他实现了他的九个补丁图像,我做了一个形状drawable。 Both generate similar and acceptable results. 两者都产生类似且可接受的结果

So my question is, which should we use? 所以我的问题是,我们应该使用哪个? Are there are trade-offs between memory consumption and performance? 内存消耗和性能之间是否需要权衡? I imagine that the image could take time to load, but that the shape drawable takes more time to draw (due to calculations). 我想图像可能需要一些时间来加载,但是可绘制的形状需要更多的时间来绘制(由于计算)。 Are these then stored in a cache and those penalties only happen the first time they are displayed or are these issues on-going??? 然后将它们存储在缓存中,这些处罚只会在它们第一次显示时发生,或者这些问题是否正在进行中?

Shape Drawable: 形状可绘:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="#003472"
        android:endColor="#000034"
        android:gradientRadius="350"
        android:type="radial"/>
</shape>

Nine Patch: 九补丁:

在此输入图像描述

Shape drawable is great for gradient kind of images with simple constant color changes. 可绘制的形状非常适用于具有简单恒定颜色变化的渐变类图像。 On the other hand, 9-patch images are great for images with lots of detail, constant color in streching regions. 另一方面,9-patch图像非常适用于具有大量细节的图像,在拉伸区域中具有恒定的颜色。

Was just bumping into this problem earlier. 刚刚碰到这个问题。

DO NOT USE SHAPE IN XML IF YOU HAVE ANIMATION THAT INVOLVES IT. 如果您有涉及它的动画,请勿在XML中使用形状。 The reason is that the caching (especially if you are using hardware acceleration on) will make it really seem "laggy" as the screen will not refresh as often. 原因是缓存(特别是如果你正在使用硬件加速)将使它看起来真的“滞后”,因为屏幕不会经常刷新。

Take a look at the answer given by @Ivan Bartsov. 看看@Ivan Bartsov给出的答案。 But through careful code analysis he concludes that for most general cases, using a 9 patch requires less process uses less memory than generated gradients. 但是通过仔细的代码分析,他得出结论,对于大多数一般情况,使用9补丁需要较少的进程使用比生成的渐变更少的内存。 Therefore for most cases it's best to use 9 patches. 因此,对于大多数情况,最好使用9个补丁。

Take a look at his answer for mode details: What should i use for better performance, nine-patch or drawable xml resource? 看看他对模式细节的回答: 我应该使用什么来获得更好的性能,9补丁或可绘制的xml资源?

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

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