Android - SVG file import - android

I want to use svg drawable with Android Studio. I use drawable->New->Vector Asset->Local File, but it doesn't import my svg file. I try this file with svg2android it throws some warnings.
Warning #1: stroke-width not found on path one or more times. Defaulting all instances to 1.
Warning #2: transforms on path are not supported, use option Bake transforms into path
Generated xml file is below:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:strokeColor="#66899a"
android:strokeWidth="1"
android:pathData="M 0 -44 C 3.31370849898 -44 6 -24.3005289926 6 0 C 6 24.3005289926 3.31370849898 44 0 44 C -3.31370849898 44 -6 24.3005289926 -6 0 C -6 -24.3005289926 -3.31370849898 -44 0 -44 Z" />
<path
android:strokeColor="#e1d85d"
android:strokeWidth="1"
android:pathData="M 0 -44 C 3.31370849898 -44 6 -24.3005289926 6 0 C 6 24.3005289926 3.31370849898 44 0 44 C -3.31370849898 44 -6 24.3005289926 -6 0 C -6 -24.3005289926 -3.31370849898 -44 0 -44 Z" />
<path
android:strokeColor="#80a3cf"
android:strokeWidth="1"
android:pathData="M 0 -44 C 3.31370849898 -44 6 -24.3005289926 6 0 C 6 24.3005289926 3.31370849898 44 0 44 C -3.31370849898 44 -6 24.3005289926 -6 0 C -6 -24.3005289926 -3.31370849898 -44 0 -44 Z" />
<path
android:strokeColor="#4b541f"
android:strokeWidth="1"
android:pathData="M 0 -44 C 24.3005289926 -44 44 -24.3005289926 44 0 C 44 24.3005289926 24.3005289926 44 0 44 C -24.3005289926 44 -44 24.3005289926 -44 0 C -44 -24.3005289926 -24.3005289926 -44 0 -44 Z" />
Does android support fully svg? If yes, how can I import this file? Also, I want to set background drawable programmatically. Is it possible without using R.drawable.svgXml? Can I generate bitmap from xml?

Does android support fully svg?
Obviously not, otherwise it would not show you those warnings that state "X not supported".
You have to change the SVG if you want to use it.
Can I generate bitmap from xml?
See here How to get a Bitmap from a drawable defined in a xml?

Related

Make an arrow shape both side with xml

I want to make a button shape like this
Is there a way to do this with XML?
Actually I am looking for like this
It is not exactly what you are looking for, because it is not with realized with a XML. However with the new MaterialButton it is very simple to obtain it:
Just define the Button in your layout:
<com.google.android.material.button.MaterialButton
app:cornerRadius="0dp"
android:paddingLeft="24dp"
android:paddingRight="12dp"
.../>
Then just use the ShapeAppearanceModel to define a custom shape.
float size = getResources().getDimension(R.dimen.cutout_size); //18dp
TriangleEdgeTreatment triangleEdgeTreatment = new TriangleEdgeTreatment(size,true);
CutCornerTreatment cutCornerTreatment = new CutCornerTreatment(size);
button.setShapeAppearanceModel(button.getShapeAppearanceModel()
.toBuilder()
.setLeftEdge(triangleEdgeTreatment)
.setTopRightCorner(cutCornerTreatment)
.setBottomRightCorner(cutCornerTreatment)
.build());
Using more buttons (use button.setBackgroundTintList(..) or app:backgroundTint="..." in xml to change the backgroundColor) you can obtain something like:
I have achieved this by vector.
#KinjalShah Please look into this.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="138.25dp"
android:height="23dp"
android:viewportWidth="138.25"
android:viewportHeight="23">
<path android:pathData="M132.53,22.48l-132.27,0l5.36,-10.95l-5.36,-10.94l132.27,0l5.36,10.94z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="137.8946"
android:endY="11.535"
android:startX="0.2646"
android:startY="11.535"
android:type="linear">
<item
android:color="#3b0b7b"
android:offset="0" />
<item
android:color="#6244A5"
android:offset="1" />
</gradient>
</aapt:attr>
</path>
</vector>
You can make drawable for this, this is below code which exactly you want, for the process how i have created this, you can check here in the link https://stackoverflow.com/a/57443999/7216511
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="1870"
android:viewportHeight="490"
android:width="187dp"
android:height="49dp">
<path
android:pathData="M0 245l0 -245 935 0 935 0 0 245 0 245 -935 0 -935 0 0 -245z"
android:fillColor="#F99E3F" />
<path
android:pathData="M1 245l0 -245 934 0 935 0 0 245 0 245 -935 0 -934 0 0 -245z"
android:fillColor="#E4CFBA" />
<path
android:pathData="M4 378c6 -136 6 -117 0 -260l-5 -118 935 0 936 0 0 245 0 245 -936 0 -935 0 5 -112z"
android:fillColor="#D5D5D5" />
<path
android:pathData="M4 378c6 -136 6 -117 0 -260l-5 -118 935 0 936 0 0 50c0 28 -5 50 -10 50 -7 0 -7 6 0 19 6 12 10 92 9 195l-1 176 -934 0 -935 0 5 -112zm1811 -57l34 -69 -35 -76 -35 -76 -879 0c-484 0 -880 2 -880 5 0 3 15 36 34 73l34 68 -33 72 -34 72 879 0 880 0 35 -69z"
android:fillColor="#E5DBCF" />
<path
android:pathData="M4 378c7 -138 7 -128 0 -265l-6 -113 936 0 936 0 0 50c0 28 -5 50 -10 50 -7 0 -7 6 0 19 13 25 13 227 0 252 -7 13 -7 19 0 19 5 0 10 22 10 50l0 50 -936 0 -936 0 6 -112zm1811 -58l35 -70 -34 -75 -35 -75 -880 0c-485 0 -881 3 -881 8 1 4 16 36 34 72l34 65 -34 67c-19 37 -34 70 -34 73 0 3 396 5 880 5l880 0 35 -70z"
android:fillColor="#EEEEEE" />
</vector>

Android animated vector in one file compilation error

I have export a animated vector from https://romannurik.github.io/AndroidIconAnimator/.
the generated file is
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="510dp"
android:height="510dp"
android:viewportWidth="510"
android:viewportHeight="510"
android:alpha="1">
<group android:name="group">
<group android:name="play_circle_outline">
<path
android:name="path"
android:pathData="M 204 369.75 L 357 255 L 204 140.25 L 204 369.75 Z M 255 0 C 114.75 0 0 114.75 0 255 C 0 395.25 114.75 510 255 510 C 395.25 510 510 395.25 510 255 C 510 114.75 395.25 0 255 0 Z M 255 459 C 142.8 459 51 367.2 51 255 C 51 142.8 142.8 51 255 51 C 367.2 51 459 142.8 459 255 C 459 367.2 367.2 459 255 459 Z"
android:strokeColor="#000000"/>
</group>
</group>
</vector>
</aapt:attr>
<target android:name="path">
<aapt:attr name="android:animation">
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="path"
android:propertyName="pathData"
android:startOffset="200"
android:duration="1000"
android:valueFrom="M 204 369.75 L 357 255 L 204 140.25 L 204 369.75 Z M 255 0 C 114.75 0 0 114.75 0 255 C 0 395.25 114.75 510 255 510 C 395.25 510 510 395.25 510 255 C 510 114.75 395.25 0 255 0 Z M 255 459 C 142.8 459 51 367.2 51 255 C 51 142.8 142.8 51 255 51 C 367.2 51 459 142.8 459 255 C 459 367.2 367.2 459 255 459 Z"
android:valueTo="M 204 369.75 L 360 180 L 204 369.75 L 150 336.75 Z M 255 0 C 114.75 0 0 114.75 0 255 C 0 395.25 114.75 510 255 510 C 395.25 510 510 395.25 510 255 C 510 114.75 395.25 0 255 0 Z M 255 459 C 142.8 459 51 367.2 51 255 C 51 142.8 142.8 51 255 51 C 367.2 51 459 142.8 459 255 C 459 367.2 367.2 459 255 459 Z"
android:valueType="pathType"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"/>
</aapt:attr>
</target>
</animated-vector>
But it is giving multiple error when im adding the file in android studio.
first error is :
Element animated-vector doesnt ave required attrbute android:drawable.
Second error in line 4 is :
attribute is missing the android namespace prefix
and online 22 animation attribute should be defined.
Why this errors are occuring ?
i ave seen https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html and here the one part file is almost same.
This is android studio editor bug Issue 227137. Btw on android studio 2.3 this files compiles and runs just fine, I tried it with your drawable)

Android Vector drawable from psd has empty preview

I was trying to add a vector drawable to Android studio
Using the vector asset in Android studio, I've imported a psd file, it but it seems like there is a rendering problem as the preview and the result xml file are empty, please check the
output xml screenshot.
In your screenshot there is no pathdata in the file. Hence, nothing is drawn on screen. I am showing contents of a VectorDrawable xml file. Copy the contents into a blank xml file in android studio and see the shape drawn in blue on screen.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="600dp"
android:height="600dp"
android:viewportWidth="800.0"
android:viewportHeight="800.0">
<path
android:pathData="M 162 8
q -07 00 -41 26
q -34 27 -50 64
q -25 59 -19 117
q 07 70 53 121
q 57 63 151 62
q 87 -01 140 -66
q 46 -55 48 -142
q 01 -56 -34 -105
q -38 -52 -77 -70
l -29 -11
q 16 -01 31 -02
q 59 -01 119 -02
"
android:strokeLineCap="round"
android:strokeColor="#f00f"
android:fillColor="#00000000"
android:strokeWidth="32"/>
</vector>

How to make border of LinearLayout in android as per following design

I have uses shape drawable to get rounded coreners but adding a semicircle in between seems tricky.
you can use vector asset studio to draw a custom shape and use it as background for your layout
https://developer.android.com/studio/write/vector-asset-studio.html
You can use Vector Drawable to achieve your end result. I used potrace to convert your image into svg format which is included at the bottom.
Use the Android Studio to create a vector drawable from this svg file.
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="271.000000pt" height="263.000000pt" viewBox="0 0 271.000000 263.000000"
preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.13, written by Peter Selinger 2001-2015
</metadata>
<g transform="translate(0.000000,263.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M225 2546 c-40 -17 -84 -63 -101 -103 -12 -27 -14 -228 -14 -1140 0
-1093 0 -1107 20 -1149 12 -24 39 -54 62 -70 l41 -29 456 -3 c251 -2 471 0
489 3 23 4 32 11 32 25 0 11 7 20 15 20 8 0 19 16 25 39 14 54 79 124 142 152
74 33 191 33 265 0 61 -27 125 -92 142 -144 7 -21 17 -37 22 -37 5 0 9 -18 9
-40 0 -40 7 -49 25 -31 6 6 151 11 345 13 l335 3 36 24 c20 14 47 41 60 60
l24 35 3 1105 c1 608 0 1121 -3 1139 -8 42 -59 100 -110 123 -36 17 -73 19
-392 19 l-353 0 0 -35 c0 -24 -5 -35 -15 -35 -8 0 -24 -20 -36 -45 -25 -53
-74 -99 -138 -128 -36 -16 -66 -21 -136 -21 -79 -1 -97 3 -147 27 -90 44 -148
127 -148 211 l0 26 -462 0 c-361 -1 -470 -4 -493 -14z m920 -93 c13 -44 28
-67 70 -109 182 -177 521 -113 592 112 l17 54 335 0 c375 0 383 -1 428 -69
l23 -34 -2 -1113 -3 -1112 -25 -27 c-51 -55 -54 -55 -399 -55 l-319 0 -7 32
c-19 86 -84 160 -180 205 -52 24 -73 28 -150 28 -82 0 -97 -3 -157 -33 -84
-41 -143 -105 -164 -179 l-15 -53 -460 0 c-497 0 -487 -1 -539 55 l-25 27 -3
1111 c-2 1096 -2 1112 18 1145 11 18 34 41 52 52 32 19 52 20 465 20 l432 0
16 -57z"/>
</g>
</svg>

Color selector for vector image doesn't work

I have a vector image (svg converted to xml):
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:better="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
tools:ignore="NewApi"
android:viewportWidth="24"
better:viewportWidth="24"
android:viewportHeight="24"
better:viewportHeight="24"
android:width="24dp"
android:height="24dp">
<path
android:pathData="M10.09 15.59l1.41 1.41 5 -5 -5 -5 -1.41 1.41 2.58 2.59 -9.67 0 0 2 9.67 0 -2.58 2.59zM19 3L5 3C3.89 3 3 3.9 3 5l0 4 2 0 0 -4 14 0 0 14 -14 0 0 -4 -2 0 0 4c0 1.1 0.89 2 2 2l14 0c1.1 0 2 -0.9 2 -2L21 5C21 3.9 20.1 3 19 3Z"
better:pathData="M10.09 15.59l1.41 1.41 5 -5 -5 -5 -1.41 1.41 2.58 2.59 -9.67 0 0 2 9.67 0 -2.58 2.59zM19 3L5 3C3.89 3 3 3.9 3 5l0 4 2 0 0 -4 14 0 0 14 -14 0 0 -4 -2 0 0 4c0 1.1 0.89 2 2 2l14 0c1.1 0 2 -0.9 2 -2L21 5C21 3.9 20.1 3 19 3Z"
android:fillColor="#color/menu_color_selector"
better:fillColor="#color/menu_color_selector" />
</vector>
But the color selector doesn't work. Is it possible to achieve what I am trying to do?
Try to use StateListDrawable with different VectorDrawable for each required state.

Categories

Resources