No sooner said than done. So, under katom history of introduction of vector images in one of our applications. In article we will impart experience also features of use of vector images in format SVG in applications Android.
Adobe Illustrator 、Inkscape work.
GitHub: pents90/svg-android
他们项目通过底层封装的接口方式:
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.filename); Drawable drawable = svg.createPictureDrawable(); imageView.setImageDrawable(drawable);
URL: svg-android-2
izorbrazhenija with a shade and without: 118 KB vs 1 KB
The problem with gradients has dared removal of superfluous tags from svg (it is described further in article). But basically, and with it it would be possible to live and in our simple images to replace a gradient with homogeneous pouring, if not other nuance — considerable load time of images.
at the left — the black sky in the form of a gradient, on the right — a correct picture.
that the most interesting, — is analyzed only attribute xlink:href which is a semblance of hyperlinks in the document. In our problem images just there were such links, and they conducted anywhere.
@Override public void setColorFilter(ColorFilter colorFilter) {} @Override public void setAlpha(int alpha) {}
public void setFilterColor(int filterColor) { fillPaint.setColorFilter(new PorterDuffColorFilter(filterColor, Mode.MULTIPLY)); }
接口调整为:
SVG svg = SVGParser.getSVGFromResource(getResources(), rawSvgId, filterColor);
因此我们能够在多张图片上通过引用一张图片使用不同的阴影颜色(As a result we could receive some images of different shades from one picture.)
对于透明度,建议并不适用setAlpha去实现(实际上是可以的通过fillPaint):
Also it is possible to establish and Alpha for fillPaint, but in games this property is required in the dynamic form (have pressed an element — has become translucent), and podgruzhat each time the new image is inconvenient. Therefore this effect have replaced with scaling (have pressed — the element has decreased).
java.lang.UnsupportedOperationException at android.view.GLES20Canvas.drawPicture(GLES20Canvas.java:895) at android.graphics.drawable.PictureDrawable.draw(PictureDrawable.java:73)
低版本不支持gpu(api < 11)
public static void setSoftwareLayerType(View view) { try { view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (NoSuchMethodError e) { //Do nothing - this happens on API < 11 } }