简体   繁体   中英

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. 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.

Was just bumping into this problem earlier.

DO NOT USE SHAPE IN XML IF YOU HAVE ANIMATION THAT INVOLVES IT. 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. 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. Therefore for most cases it's best to use 9 patches.

Take a look at his answer for mode details: What should i use for better performance, nine-patch or drawable xml resource?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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