我应该用什么来更好的性能,九补丁或绘制XML资源? [英] What should i use for better performance, nine-patch or drawable xml resource?
问题描述
什么是设置背景有些人认为最好的方法是什么?底色。例如2变种:
What is the best way to set background for some view? For example 2 variants of backround:
- 背景渐变,圆角和边框
- 在后台只有一个颜色和圆润的边角
所以这些变种会更好,九补丁或绘制XML资源?
So which of variants would be better, nine-patch or drawable xml resource?
推荐答案
我的猜测是, NinePatch
将略快于大多数情况。下面是我发现。
My guess is, NinePatch
would be slightly faster in most cases. Here's what I found.
GradientDrawable
(在XML用于rects一)使用<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3_r1/android/graphics/drawable/GradientDrawable.java#249">this code 调用通过对画布
从而使用本地通话导致的 SkCanvas
,的 SkDraw
,最终的 SkScan
和的 SkBlitter
。
GradientDrawable
(the one used for rects in xml) uses this code to call through to Canvas
which in turn uses native call leading to SkCanvas
, SkDraw
and eventually SkScan
and SkBlitter
.
在另一方面,<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3_r1/android/graphics/NinePatch.java#103"><$c$c>NinePatch$c$c>'s画()几乎为零的Java code 本地<一前href="https://android.googlesource.com/platform/frameworks/base/+/master/core/jni/android/graphics/NinePatch.cpp">call以 NinePatch.cpp
其中<一个href="https://android.googlesource.com/platform/frameworks/base/+/master/core/jni/android/graphics/NinePatchImpl.cpp">shortly电话 NinePatchImpl.cpp
- NinePatch_draw()
---而这也正是神奇的是。在code有遍历标记区和后使用大致相同的逻辑一些后续调用绘制的东西,在 SkDraw
(仅的drawRect( )
而不是 drawPath()
),但最后却是相同的 SkScan
和 SkBlitter
来完成这些工作。
On the other hand, NinePatch
's draw() has almost zero Java code before the native call to NinePatch.cpp
which shortly calls NinePatchImpl.cpp
-- NinePatch_draw()
--- and that's where the magic is. The code there iterates over the marked regions and after a number of subsequent calls draws stuff using roughly the same logic in SkDraw
(only drawRect()
instead of drawPath()
) but in the end it's the same SkScan
and SkBlitter
that do the work.
所有的code是pretty的硬包我的头周围瞬间,但是没有赶上我的眼睛是 GradientDrawable
进行两次调用全如果有两个背景和中风(<本地栈href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3_r1/android/graphics/drawable/GradientDrawable.java#318">look这里),而在任何情况下一个 NinePatch
只会让一个人。
All that code is pretty hard to wrap my head around instantly, but what did catch my eye is that GradientDrawable
makes two calls to the whole native stack if it has both background and stroke (look here), while in any scenario a NinePatch
only makes one.
所以,没有实际测量时间两种方法我得到了大多数情况下的感觉 NinePatch
赢得比赛:如果我们的 [不亦乐乎] 的大致假设本机调用栈的drawRect()
和 drawPath()
使用pretty的大致相同的逻辑和 [另一种可怕的简化] 的是得到传来传去那里,由 NinePatch
参数设置和 GradientDrawable
不影响的方法的复杂性多,那么 NinePatch
原来是比 GradientDrawable
与灌装和大纲。好了,只要你使用常规的,9节9修补程序(即不撕碎你9修补程序通过一个可怕的很多指标,使得迭代件过于努力贵)。
So, without actually measuring times for both approaches I get a feeling in most cases NinePatch
wins the race: if we [awfully] roughly assume that native call stacks for drawRect()
and drawPath()
use pretty much the same logic and [another awful simplification] the parameter sets that get passed around there and are created by NinePatch
and GradientDrawable
don't affect complexity of the methods that much, then NinePatch
turns out to be roughly 2 times faster than GradientDrawable
with filling and outline. Well, provided you use a regular, 9-section 9-Patch (i.e. don't shred you 9-Patch by an awful lot of markers, making the iteration over the pieces overly effort-expensive).
任何人谁就会绊倒在此,知道更多关于这个问题(和/或更好地估计本地code复杂性),请纠正我,如果我错了。的
PS是的,我知道这是没有太大的直接的答案的
这篇关于我应该用什么来更好的性能,九补丁或绘制XML资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!