Related
How can I convert my android vector drawable to SVG?
Don't mark it as duplicate question. I have already tried those methods but didn't work, what I have tried https://shapeshifter.design/ website, but actually it is good, but it gives me wrong input and output.
Suppose I have a vector
<vector android:height="80dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="80dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#color/colorLightYellow" android:pathData="M150.561,144.549c-1.315,0 -2.647,-0.341 -3.86,-1.06L52.164,87.532c-3.609,-2.136 -4.803,-6.793 -2.667,-10.402c2.137,-3.608 6.793,-4.802 10.402,-2.667l94.537,55.957c3.609,2.136 4.803,6.793 2.667,10.402C155.685,143.217 153.156,144.549 150.561,144.549z"/>
<path android:fillColor="#color/colorLightYellow" android:pathData="M150.568,144.548H47.842c-4.194,0 -7.593,-3.399 -7.593,-7.593s3.4,-7.593 7.593,-7.593h102.727c4.194,0 7.593,3.399 7.593,7.593S154.762,144.548 150.568,144.548z"/>
<path android:fillColor="#color/colorLightOrange" android:pathData="M342.693,335.833L207.961,136.955l51.811,-74.838c10.849,-15.671 -0.367,-37.077 -19.426,-37.077H118.183c-19.059,0 -30.275,21.406 -19.426,37.077l51.811,74.838L15.836,335.833C5.516,351.066 0,369.043 0,387.443l0,0c0,50.82 41.198,92.018 92.017,92.018h174.495c50.82,0 92.017,-41.198 92.017,-92.018l0,0C358.529,369.043 353.013,351.066 342.693,335.833z"/>
<path android:fillColor="#color/colorLightOrange" android:pathData="M342.693,335.833L207.961,136.955l51.811,-74.838c10.849,-15.671 -0.367,-37.077 -19.426,-37.077h-22.144c17.303,0 27.486,21.406 17.637,37.077l-47.038,74.838L311.12,335.833c9.369,15.233 14.377,33.211 14.377,51.61c0,50.82 -37.402,92.018 -83.539,92.018h24.555c50.82,0 92.017,-41.198 92.017,-92.018C358.529,369.043 353.013,351.066 342.693,335.833z"/>
<path android:fillColor="#color/colorLightYellow" android:pathData="M214.129,144.548h-71.883c-4.194,0 -7.593,-3.399 -7.593,-7.593s3.4,-7.593 7.593,-7.593h71.883c4.194,0 7.593,3.399 7.593,7.593S218.323,144.548 214.129,144.548z"/>
<path android:fillColor="#FCAB29" android:pathData="M393.083,249.127c-65.571,0 -118.917,53.346 -118.917,118.917c0,65.57 53.346,118.916 118.917,118.916S512,433.614 512,368.044C512,302.473 458.654,249.127 393.083,249.127z"/>
<path android:fillColor="#DD8D19" android:pathData="M458.128,268.543c22.753,21.675 36.953,52.25 36.953,86.081c0,65.57 -53.346,118.916 -118.917,118.916c-23.991,0 -46.341,-7.148 -65.045,-19.417c21.347,20.336 50.223,32.836 81.964,32.836C458.654,486.96 512,433.614 512,368.044C512,326.464 490.544,289.807 458.128,268.543z"/>
<path android:fillColor="#F2DF33" android:pathData="M393.08,368.04m-80.17,0a80.17,80.17 0,1 1,160.34 0a80.17,80.17 0,1 1,-160.34 0"/>
<path android:fillColor="#FCAB29" android:pathData="M403.037,360.544h-19.908c-5.535,0 -10.038,-4.503 -10.038,-10.038s4.503,-10.038 10.038,-10.038h29.192c4.142,0 7.5,-3.357 7.5,-7.5s-3.358,-7.5 -7.5,-7.5h-11.738v-7.827c0,-4.143 -3.358,-7.5 -7.5,-7.5s-7.5,3.357 -7.5,7.5v7.827h-2.454c-13.806,0 -25.038,11.232 -25.038,25.038s11.232,25.038 25.038,25.038h19.908c5.535,0 10.038,4.503 10.038,10.037c0,5.535 -4.503,10.038 -10.038,10.038h-29.192c-4.142,0 -7.5,3.357 -7.5,7.5s3.358,7.5 7.5,7.5h11.739v7.827c0,4.143 3.358,7.5 7.5,7.5s7.5,-3.357 7.5,-7.5v-7.827h2.454c13.806,0 25.038,-11.232 25.038,-25.038S416.843,360.544 403.037,360.544z"/>
<path android:fillColor="#color/colorLightYellow" android:pathData="M368.669,144.262l-18.046,-14.437c-0.019,-0.016 -0.042,-0.025 -0.061,-0.041c-0.315,-0.248 -0.648,-0.473 -1.001,-0.668c-0.007,-0.003 -0.013,-0.008 -0.02,-0.012c-0.339,-0.186 -0.696,-0.339 -1.064,-0.472c-0.05,-0.018 -0.1,-0.038 -0.15,-0.055c-0.347,-0.116 -0.704,-0.207 -1.071,-0.272c-0.065,-0.011 -0.129,-0.02 -0.193,-0.029c-0.368,-0.056 -0.741,-0.093 -1.124,-0.093s-0.756,0.038 -1.124,0.093c-0.065,0.01 -0.129,0.018 -0.193,0.029c-0.367,0.065 -0.725,0.156 -1.071,0.272c-0.051,0.017 -0.1,0.037 -0.15,0.055c-0.368,0.132 -0.725,0.286 -1.064,0.472c-0.007,0.004 -0.013,0.009 -0.02,0.012c-0.353,0.195 -0.686,0.421 -1.001,0.668c-0.02,0.016 -0.042,0.025 -0.061,0.041l-18.046,14.437c-3.234,2.588 -3.759,7.307 -1.171,10.542c2.587,3.233 7.306,3.759 10.542,1.171l5.861,-4.688v68.76c0,4.143 3.358,7.5 7.5,7.5s7.5,-3.357 7.5,-7.5v-68.76l5.861,4.688c1.383,1.106 3.037,1.644 4.68,1.644c2.2,0 4.38,-0.963 5.861,-2.814C372.429,151.568 371.904,146.85 368.669,144.262z"/>
<path android:fillColor="#color/colorLightYellow" android:pathData="M462.959,104.039l-18.046,-14.437c-0.019,-0.016 -0.042,-0.025 -0.061,-0.041c-0.315,-0.248 -0.648,-0.473 -1.001,-0.668c-0.007,-0.003 -0.013,-0.008 -0.02,-0.012c-0.339,-0.186 -0.696,-0.339 -1.064,-0.472c-0.05,-0.018 -0.1,-0.038 -0.15,-0.055c-0.347,-0.116 -0.704,-0.207 -1.071,-0.272c-0.065,-0.011 -0.129,-0.02 -0.193,-0.029c-0.368,-0.056 -0.741,-0.093 -1.124,-0.093s-0.756,0.038 -1.124,0.093c-0.065,0.01 -0.129,0.018 -0.193,0.029c-0.367,0.065 -0.725,0.156 -1.071,0.272c-0.051,0.017 -0.1,0.037 -0.15,0.055c-0.368,0.132 -0.725,0.286 -1.064,0.472c-0.007,0.004 -0.013,0.009 -0.02,0.012c-0.353,0.195 -0.686,0.421 -1.001,0.668c-0.02,0.016 -0.042,0.025 -0.061,0.041l-18.046,14.437c-3.234,2.588 -3.759,7.307 -1.171,10.542c2.587,3.233 7.306,3.758 10.542,1.171l5.861,-4.688v68.76c0,4.143 3.358,7.5 7.5,7.5s7.5,-3.357 7.5,-7.5v-68.76l5.861,4.688c1.383,1.106 3.037,1.644 4.68,1.644c2.2,0 4.38,-0.963 5.861,-2.814C466.718,111.346 466.193,106.627 462.959,104.039z"/>
</vector>
than this website shows me this:
but my actual vector is this:
The website doesn't show the knapsack and those 2 arrows and after exporting also, it only shows the coins only.
I need to make this vector into PNG, that why I am trying it to make SVG then PNG, and I tried few more websites but either those shows deprecated.
I have converted it without of any programm. Here is the SVG for you:
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" viewBox="0 0 512 512">
<path fill="#fcab29" d="M150.561,144.549c-1.315,0 -2.647,-0.341 -3.86,-1.06L52.164,87.532c-3.609,-2.136 -4.803,-6.793 -2.667,-10.402c2.137,-3.608 6.793,-4.802 10.402,-2.667l94.537,55.957c3.609,2.136 4.803,6.793 2.667,10.402C155.685,143.217 153.156,144.549 150.561,144.549z"/>
<path fill="#fcab29" d="M150.568,144.548H47.842c-4.194,0 -7.593,-3.399 -7.593,-7.593s3.4,-7.593 7.593,-7.593h102.727c4.194,0 7.593,3.399 7.593,7.593S154.762,144.548 150.568,144.548z"/>
<path fill="#ed664c" d="M342.693,335.833L207.961,136.955l51.811,-74.838c10.849,-15.671 -0.367,-37.077 -19.426,-37.077H118.183c-19.059,0 -30.275,21.406 -19.426,37.077l51.811,74.838L15.836,335.833C5.516,351.066 0,369.043 0,387.443l0,0c0,50.82 41.198,92.018 92.017,92.018h174.495c50.82,0 92.017,-41.198 92.017,-92.018l0,0C358.529,369.043 353.013,351.066 342.693,335.833z"/>
<path fill="#ed664c" d="M342.693,335.833L207.961,136.955l51.811,-74.838c10.849,-15.671 -0.367,-37.077 -19.426,-37.077h-22.144c17.303,0 27.486,21.406 17.637,37.077l-47.038,74.838L311.12,335.833c9.369,15.233 14.377,33.211 14.377,51.61c0,50.82 -37.402,92.018 -83.539,92.018h24.555c50.82,0 92.017,-41.198 92.017,-92.018C358.529,369.043 353.013,351.066 342.693,335.833z"/>
<path fill="#fcab29" d="M214.129,144.548h-71.883c-4.194,0 -7.593,-3.399 -7.593,-7.593s3.4,-7.593 7.593,-7.593h71.883c4.194,0 7.593,3.399 7.593,7.593S218.323,144.548 214.129,144.548z"/>
<path fill="#FCAB29" d="M393.083,249.127c-65.571,0 -118.917,53.346 -118.917,118.917c0,65.57 53.346,118.916 118.917,118.916S512,433.614 512,368.044C512,302.473 458.654,249.127 393.083,249.127z"/>
<path fill="#DD8D19" d="M458.128,268.543c22.753,21.675 36.953,52.25 36.953,86.081c0,65.57 -53.346,118.916 -118.917,118.916c-23.991,0 -46.341,-7.148 -65.045,-19.417c21.347,20.336 50.223,32.836 81.964,32.836C458.654,486.96 512,433.614 512,368.044C512,326.464 490.544,289.807 458.128,268.543z"/>
<path fill="#F2DF33" d="M393.08,368.04m-80.17,0a80.17,80.17 0,1 1,160.34 0a80.17,80.17 0,1 1,-160.34 0"/>
<path fill="#FCAB29" d="M403.037,360.544h-19.908c-5.535,0 -10.038,-4.503 -10.038,-10.038s4.503,-10.038 10.038,-10.038h29.192c4.142,0 7.5,-3.357 7.5,-7.5s-3.358,-7.5 -7.5,-7.5h-11.738v-7.827c0,-4.143 -3.358,-7.5 -7.5,-7.5s-7.5,3.357 -7.5,7.5v7.827h-2.454c-13.806,0 -25.038,11.232 -25.038,25.038s11.232,25.038 25.038,25.038h19.908c5.535,0 10.038,4.503 10.038,10.037c0,5.535 -4.503,10.038 -10.038,10.038h-29.192c-4.142,0 -7.5,3.357 -7.5,7.5s3.358,7.5 7.5,7.5h11.739v7.827c0,4.143 3.358,7.5 7.5,7.5s7.5,-3.357 7.5,-7.5v-7.827h2.454c13.806,0 25.038,-11.232 25.038,-25.038S416.843,360.544 403.037,360.544z"/>
<path fill="#fcab29" d="M368.669,144.262l-18.046,-14.437c-0.019,-0.016 -0.042,-0.025 -0.061,-0.041c-0.315,-0.248 -0.648,-0.473 -1.001,-0.668c-0.007,-0.003 -0.013,-0.008 -0.02,-0.012c-0.339,-0.186 -0.696,-0.339 -1.064,-0.472c-0.05,-0.018 -0.1,-0.038 -0.15,-0.055c-0.347,-0.116 -0.704,-0.207 -1.071,-0.272c-0.065,-0.011 -0.129,-0.02 -0.193,-0.029c-0.368,-0.056 -0.741,-0.093 -1.124,-0.093s-0.756,0.038 -1.124,0.093c-0.065,0.01 -0.129,0.018 -0.193,0.029c-0.367,0.065 -0.725,0.156 -1.071,0.272c-0.051,0.017 -0.1,0.037 -0.15,0.055c-0.368,0.132 -0.725,0.286 -1.064,0.472c-0.007,0.004 -0.013,0.009 -0.02,0.012c-0.353,0.195 -0.686,0.421 -1.001,0.668c-0.02,0.016 -0.042,0.025 -0.061,0.041l-18.046,14.437c-3.234,2.588 -3.759,7.307 -1.171,10.542c2.587,3.233 7.306,3.759 10.542,1.171l5.861,-4.688v68.76c0,4.143 3.358,7.5 7.5,7.5s7.5,-3.357 7.5,-7.5v-68.76l5.861,4.688c1.383,1.106 3.037,1.644 4.68,1.644c2.2,0 4.38,-0.963 5.861,-2.814C372.429,151.568 371.904,146.85 368.669,144.262z"/>
<path fill="#fcab29" d="M462.959,104.039l-18.046,-14.437c-0.019,-0.016 -0.042,-0.025 -0.061,-0.041c-0.315,-0.248 -0.648,-0.473 -1.001,-0.668c-0.007,-0.003 -0.013,-0.008 -0.02,-0.012c-0.339,-0.186 -0.696,-0.339 -1.064,-0.472c-0.05,-0.018 -0.1,-0.038 -0.15,-0.055c-0.347,-0.116 -0.704,-0.207 -1.071,-0.272c-0.065,-0.011 -0.129,-0.02 -0.193,-0.029c-0.368,-0.056 -0.741,-0.093 -1.124,-0.093s-0.756,0.038 -1.124,0.093c-0.065,0.01 -0.129,0.018 -0.193,0.029c-0.367,0.065 -0.725,0.156 -1.071,0.272c-0.051,0.017 -0.1,0.037 -0.15,0.055c-0.368,0.132 -0.725,0.286 -1.064,0.472c-0.007,0.004 -0.013,0.009 -0.02,0.012c-0.353,0.195 -0.686,0.421 -1.001,0.668c-0.02,0.016 -0.042,0.025 -0.061,0.041l-18.046,14.437c-3.234,2.588 -3.759,7.307 -1.171,10.542c2.587,3.233 7.306,3.758 10.542,1.171l5.861,-4.688v68.76c0,4.143 3.358,7.5 7.5,7.5s7.5,-3.357 7.5,-7.5v-68.76l5.861,4.688c1.383,1.106 3.037,1.644 4.68,1.644c2.2,0 4.38,-0.963 5.861,-2.814C466.718,111.346 466.193,106.627 462.959,104.039z"/>
</svg>
I can say also why you had bad luck on the converting site:
https://shapeshifter.design
It is because you have in your code not convertable color values like #color/colorLightYellow. If you change android:fillColor="#color/colorLightYellow" to android:fillColor="#fcab29" and android:fillColor="#color/colorLightOrange" to android:fillColor="#ed664c" overall in your code then you will be able to convert your Android vector drawable image into SVG on this site without any mistakes.
You can use the https://shapeshifter.design/
Import the vector and use export button
Someone created this https://vd.floo.app/ - very simple and easy to use, but I think that problem is caused by usage of Android resource link #color/colorLightYellow, bcz none of converters know about what the color it is)
I tried making background using coral draw or photoshop but its not working properly.
How should I draw the shape like this? Don't expect complete code, just give me idea or point me in the right direction.
making rectangle is easy with shape but how to add that bottom right side small rectangle and make them in one shape and also it i want to make it nine patch image because different screen size issue.
Try this .. I used https://www.autotracer.org/ to convert png to svg, then https://svg2vector.com/ to convert the svg to vector.
I would suggest make the burger image as an image background for a CardView with rounded corners and use ConstraintLayout to line up the Tab Badge drawable and the TextView
Bottom Tab Badge Drawable
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="129"
android:viewportHeight="45"
android:width="129dp"
android:height="45dp">
<path
android:pathData="M0 0L0 44L7 44L9 44L10 44L12 44L13 43.9722L14 43.8889L15.9722 43.2778L17.75 41.8889L19.0833 40L19.9722 38L20.5833 36L21 34L21.3889 32L21.8611 30L22.1389 29L22.3611 28L22.6389 27L22.8611 26L23.4722 24L24.5 22L25.25 21.0278L26.0833 20.1111L27.0278 19.3056L29 18.1111L30 17.7222L31 17.4167L33 17.0833L34 17.0278L35 17L37 17L38 17L41 17L50 17L82 17L93 17L96 17L98 17L100 17.0833L101 17.25L102 17.5L103 17.9444L104.917 19.25L106.5 21.0278L107.528 23L108.139 25L108.361 26L108.611 27L108.944 29L109.056 30L109.389 32L109.611 33L110 35L110.417 37L110.694 38L111.417 40L112.5 41.8889L114.083 43.2778L116 43.8889L118 44L119 44L120 44L122 44L129 44L129 0L0 0z"
android:fillColor="#00FFFFFF" />
<path
android:pathData="M30 17L30.7778 17.7222L31.3056 17.75L32.1111 17.9444L33.0278 17.9722L35 18L36 18L38 18L51 18L101 18L100.222 17.2778L99.6944 17.25L98.8889 17.0556L97.9722 17.0278L96 17L95 17L93 17L80 17L30 17z"
android:fillColor="#00FFFFFF" />
<path
android:pathData="M101.667 17.3333L102 17.6667L102.333 17.6667L102.333 17.3333L101.667 17.3333z"
android:fillColor="#00FFFFFF" />
<path
android:pathData="M26 19L26.7778 19.6389L27.25 19.5278L28.0556 19.5278L30 19L29.2222 18.3056L28.75 18.3056L27.4722 18.3056L26 19z"
android:fillColor="#00FFFFFF" />
<path
android:pathData="M17 45L113 45L112.306 43.5L111.944 42.9444L110.917 41L110.472 40L110.167 39L109.639 37L109.389 36L109.194 35L108.806 33L108.611 32L108.389 31L108.056 29L107.944 28L107.583 26L107.306 25L106.583 23L106.083 22L104.75 20.1111L103.917 19.3333L102 18.3333L101 18.1111L100 18.0278L99 18L97 18L96 18L93 18L86 18L44 18L39 18L37 18L36 18L34 18L33 18.0278L32 18.0833L30 18.4722L28.0833 19.2778L27.25 19.8611L26.4722 20.4722L25.2778 22.0833L24.3889 24L24.0278 25L23.3889 27L23.1389 28L22.8611 29L22.3889 31L22.1944 32L22 33L21.5833 35L21 37L20.6667 38L19.8889 40L18.8056 41.9444L18.1944 42.9444L17.75 43.5L17 45z"
android:fillColor="#FFFFFF" />
<path
android:pathData="M103 18L103.75 19.5L104.222 20.0556L105.528 22L106.056 22.9444L106.722 24.5L107 26L107.722 25.2222L107.722 24.6944L107.861 23.8889L107.5 22L106.5 20.1111L105.806 19.3333L104.472 18.3611L103 18M25 20L24.0278 21.5L23.4444 23.0556L23.0833 24.9444L23 27L23.8056 26.2222L24 25.6944L24.4167 24.8889L25.1389 23.0278L25.4722 22.1111L25.5 21.3056L25.6389 20.7778L25 20M108 26L108 32L108.722 31.2222L108.75 30.6944L108.944 29.8889L108.944 28.1111L108.75 27.3056L108.722 26.7778L108 26M22 27L22 31L22.7222 30.2222L22.8889 29L22.7222 28.2778L22.7222 27.7778L22 27M21 31L21 36L21.7222 35.2222L21.75 34.6944L21.9167 33.0833L21.75 32.3056L21.7222 31.7778L21 31M109 32L109 37L109.722 36.2222L109.75 35.6944L109.917 34.0833L109.75 33.3056L109.722 32.7778L109 32M20 36L20 39L20.6944 38.25L20.6944 37.1944L20.6944 36.75L20 36M110 37L110.028 38.5L110.25 40.0556L110.5 41L110.944 41.9722L111.5 42.8889L113.083 44.2778L115 44.8889L117 45L118 45L119 45L122 45L129 45L128.222 44.2778L127.694 44.25L126.889 44.0556L125 44L124 44L121 44L120 44L118 43.9722L117 43.8889L116 43.6667L115.028 43.2778L114.083 42.6667L112.5 40.9722L111.444 39.1111L111 38.3056L110.806 37.7778L110 37M19 39L18.2222 40.5L17.6944 41.0278L16.8889 41.9444L15 43.2778L13 43.8889L12 43.9722L11 44L9 44L8 44L6 44L0 44L0.777778 44.7222L1.30556 44.75L2.11111 44.9444L3.02778 44.9722L5 45L7 45L10 45L11 45L13 45L14 44.9722L16 44.6667L17.8889 43.6667L18.6667 42.8889L19.25 42L19.6111 41.1111L19.6944 39.7778L19 39z"
android:fillColor="#00FFFFFF" />
I have created the following vector asset and inserted it into an Imagebutton.
I had like that the heart will change its color to red once it was clicked and return to transparent once it was clicked again.
I couldn't find any place that describes how to change the color of the whole heart. The closest solution I found was:
ibWhishlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ibWhishlist.setColorFilter(getResources().getColor(R.color.colorRed));
}
});
However, it changes only the border of the heart and not the whole shape.
Thank you
SOLUTION:
Eventually what I used to solve this problem is to create as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/ic_favorite_red_24dp" />
<item
android:drawable="#drawable/ic_favorite_black_24dp" />
</selector>
and inside my code:
ibWhishlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ibWhishlist.setSelected(!ibWhishlist.isSelected());
if (ibWhishlist.isSelected()) {
//Handle selected state change
} else {
//Handle de-select state change
}
}
});
The simplest way to solve this problem, if you want to change minor things like fill/stroke color or stroke width at runtime, is two have two versions of the same vector image with these changes, for example, one of them has only stroke color and the other one is filled:
ic_heart_stroked.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"
android:strokeWidth="1"
android:strokeColor="#000" />
</vector>
ic_heart_filled.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>
Then in your code, switch between the two files when the user clicks:
ibWhishlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ibWhishlist.setImageResource(R.drawable.ic_heart_filled);
}
});
Another way is to use one of the ready-made libraries.
RichPathAnimator is one of the best libraries, it supports changing vector color, stroke width, and even animate vector graphics at runtime.
How to use it?
First, add the library dependency to your app build.gradle. Then in your vector.xml give a name to the path that you want to change so:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:name="heartPath1"
android:fillColor="#00000000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"
android:strokeWidth="1"
android:strokeColor="#000" />
</vector>
In your layout replace Imagebutton with com.richpath.RichPathView and inside the vector, attribute pass your vector resource in that tag:
<com.richpath.RichPathView
android:id="#+id/heartView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:vector="#drawable/ic_heart" />
In your Activity get a reference to the richpathview then call findRichPathByName(YOUR_PATH_NAME)
RichPathView heartView = findViewById(R.id.heartView);
//Providing the path name that you add to your vector,
//in our example it's 'heartPath1'.
RichPath path= heartView.findRichPathByName("heartPath1");
//Set on heart path click listener.
path.setOnPathClickListener(new RichPath.OnPathClickListener() {
#Override
public void onClick(RichPath richPath) {
//Change the path color.
richPath.setFillColor(Color.RED);
//Change stroke width to zero.
richPath.setStrokeWidth(0);
}
});
If you don't want to use RichPathView instead of your ImageView or ImageButton, and you want to change some things in your vector path, then consider using VectorChildFinder.
Programmatically you can change the color of vector icon, but you can use only a single color. Since in your case heart icon filling background color, that's why your code is changing the border of icon.
you can take 2 vectors drawable to solve your problem (just one possibility)
You can check this link as well,
How to change color of vector drawable path on button click
public static Drawable updateDrawableVector(int resId, Context context, int color) {
Drawable drawable = context.getResources().getDrawable(resId);
drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
return drawable;
}
I've added an image as logo in my first page of application. First place logo is full screen with 3x of its size after 3 seconds it will changes size to 1x.
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="3"
android:toXScale="1"
android:fromYScale="3"
android:toYScale="1"
android:duration="3000"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="3000">
</scale>
logo image is a Vector file (SVG) so it is expected not to lose quality on sizing process, but logo in 3x size (first time), has low quality like a Raster image. How could I keep the quality high on scale animation?
UPDATE:
This is the way I'm using my vector:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/ic_logo"/>
And This is SVG path imported from Android Studio Vector Asset:
<vector android:height="24dp" android:viewportHeight="10418.0"
android:viewportWidth="11006.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#536F96" android:pathData="M980,2554c83,185 592,871 588,968 -1,30 -210,427 -251,522 -78,186 -159,412 -198,612l-1114,237 -5,1310 1115,233c70,179 122,410 197,597 351,872 417,195 -330,1484l914,927c1254,-773 739,-660 1486,-338 199,86 412,123 604,208l242,1104 1294,-3 248,-1107c367,-124 854,-256 1114,-471 145,52 350,212 486,297 132,82 393,276 537,307l868,-884c-73,-234 -456,-728 -599,-982 84,-247 280,-471 360,-815l-765,2c-1057,2387 -4178,2532 -5524,497 -1386,-2097 179,-5082 2981,-4837 -83,134 -143,82 -143,289l5,1031 393,-1c20,-379 -59,-855 158,-1117l595,-600c36,-50 8,-2 40,-66 -576,-371 -1922,-337 -2701,-51 -284,104 -507,255 -734,337 -165,-95 -298,-189 -462,-295 -146,-95 -345,-235 -480,-302 -113,59 -821,774 -919,907z"/>
<path android:fillColor="#536F96" android:pathData="M7617,5574l-346,1c-150,921 -284,1445 -1178,2020 -681,438 -1864,444 -2503,-51 79,-121 147,-121 221,-274 -498,9 -600,63 -697,-146l1831,2c265,2 289,-77 414,-203 210,-212 400,-400 612,-612 423,-423 494,-389 467,-764l-386,-5c-1,91 -1,107 -172,270l-707,706c-274,276 -217,199 -732,199 -312,0 -624,-1 -936,1 54,-128 879,-909 1045,-1074l800,-800c92,-93 133,-121 139,-407 -454,-13 -274,24 -682,397 -134,133 -244,243 -377,376 -126,127 -629,664 -760,725 5,-491 -69,-662 113,-837l722,-724c152,-143 836,-72 1380,-91l12,-387c-122,-19 -1338,-27 -1447,-8 -93,17 -1031,951 -1137,1084 -161,202 54,1163 -78,1425 -20,39 -144,152 -186,194 -36,37 -56,62 -92,96 -77,70 -27,40 -116,78 -464,-554 -453,-1761 8,-2477 202,-315 485,-607 805,-794 431,-253 695,-282 1253,-341l-3,-349c-531,-76 -1232,227 -1573,460 -1273,869 -1605,2628 -664,3920 989,1359 3190,1608 4450,-40 236,-308 570,-1062 500,-1570z"/>
<path android:fillColor="#2590C8" android:pathData="M9715,2826c696,-211 1035,867 328,1092 -722,231 -1094,-860 -328,-1092zM8973,3966l-937,959c-147,129 -620,61 -864,61 -325,0 -649,0 -973,0 -158,0 -315,-2 -473,-2 -227,0 -225,37 -335,151 -218,227 -1332,1304 -1418,1435 266,9 529,45 651,-68 131,-121 245,-250 374,-379 120,-120 643,-681 761,-729 112,-21 1977,35 2290,2 132,-14 228,-159 311,-242 184,-184 789,-763 870,-887 293,43 370,224 810,150 1207,-203 1141,-1973 -74,-2096 -302,-31 -572,76 -762,225 -149,115 -314,330 -374,604 -84,383 32,527 143,816z"/>
<path android:fillColor="#2590C8" android:pathData="M10101,386c349,-173 684,228 512,557 -322,613 -1225,-202 -512,-557zM9589,1144c-151,105 -2165,2152 -2517,2503 -199,199 -122,222 -479,243l-7,401c529,12 375,62 971,-535 269,-270 516,-516 785,-785 263,-264 505,-505 769,-769l791,-782c893,355 1445,-649 868,-1209 -256,-248 -700,-298 -990,-39 -358,321 -288,500 -191,972z"/>
<path android:fillColor="#536F96" android:pathData="M8054,1110c-109,-414 494,-519 565,-152 77,397 -480,474 -565,152zM7825,1236c-438,459 -893,894 -1346,1347 -118,118 -210,213 -331,328 -168,161 -112,218 -112,584 0,423 -25,933 7,1345l397,0c16,-399 2,-821 2,-1219 -1,-289 -53,-367 111,-524l1560,-1556c947,95 833,-666 614,-887 -349,-352 -1030,-171 -902,582z"/>
You could try using native SVG animation:
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 300 300">
<title>Downscale to Center</title>
<rect width="100" height="100" fill="lightgray" transform="scale(3)">
<title>Reference Rectangle</title>
</rect>
<rect width="100" height="100" transform="scale(3)">
<animateTransform attributeName="transform" type="translate" to="100 100" begin="3s" dur="3s" fill="freeze"/>
<animateTransform attributeName="transform" type="scale" additive="sum" from="3" to="1" begin="3s" dur="3s" fill="freeze"/>
</rect>
</svg>
I would like to add text to a layer-list xml file that is used as a drawable background for a Switch element. The problem is the text in the switch element changes before the background does so to sync these I thought I could just put the text into the background instead of setting android:textOn and android:textOff. Or if you know of another attribute besides android:state_checked that I can use that will change when the text in the Switch element does, I will accept that as an answer.
My current layer list, which contains an icon and a background color looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true">
<shape>
<solid
android:color="#color/btn_switch_electricity" />
<corners
android:radius="5dp" />
</shape>
</item>
<item android:right="10dp">
<bitmap
android:gravity="right"
android:src="#drawable/ic_toggle_electricity" />
</item>
</layer-list>
So to summarize, is it possible to render some text in this layer-list?
Thanks in advance for your helpful comments/answers.
With the new api21 you can use VectorDrawables to do something like you want. Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:drawable="#drawable/vector"/>
</layer-list>
That will use a drawable vector like the following:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="32dp"
android:width="32dp"
android:viewportHeight="200"
android:viewportWidth="200">
<path android:name="text"
android:fillColor="#android:color/black"
android:pathData="M 28.53,92.12
C 27.92,92.12 27.56,91.64 27.56,91.09
27.56,90.52 27.92,90.00 28.50,90.00
28.50,90.00 41.73,90.00 41.73,90.00
42.31,90.00 42.67,90.52 42.67,91.09
42.67,91.64 42.28,92.12 41.73,92.12
41.73,92.12 36.31,92.12 36.31,92.12
36.31,92.12 36.31,108.02 36.31,108.02
36.31,108.67 35.78,109.00 35.12,109.00
34.48,109.00 33.94,108.67 33.94,108.02
33.94,108.02 33.94,92.12 33.94,92.12
33.94,92.12 28.53,92.12 28.53,92.12 Z
M 55.80,108.11
C 55.80,108.72 55.28,109.00 54.66,109.00
54.02,109.00 53.55,108.72 53.55,108.11
53.55,108.11 53.55,100.94 53.55,100.94
53.55,99.03 52.73,97.97 50.94,97.97
49.59,97.97 48.56,98.56 47.77,99.55
47.17,100.31 46.92,100.95 46.92,101.83
46.92,101.83 46.92,108.11 46.92,108.11
46.92,108.72 46.47,109.00 45.84,109.00
45.17,109.00 44.69,108.72 44.69,108.11
44.69,108.11 44.69,91.09 44.69,91.09
44.69,90.38 45.17,90.00 45.84,90.00
46.47,90.00 46.92,90.34 46.92,91.09
46.92,91.09 46.92,97.77 46.92,97.77
46.92,98.06 46.81,98.58 46.77,98.92
46.77,98.92 46.70,98.89 46.70,98.89
47.50,97.25 48.84,96.00 51.38,96.00
54.22,96.00 55.80,97.73 55.80,100.52
55.80,100.52 55.80,108.11 55.80,108.11 Z
M 60.25,103.00
C 60.30,105.59 61.72,107.20 64.31,107.20
66.05,107.20 66.97,106.42 67.55,105.31
67.55,105.31 67.55,105.34 67.55,105.34
67.77,104.97 68.11,104.81 68.44,104.81
68.61,104.81 68.78,104.86 68.94,104.89
69.36,105.00 69.72,105.25 69.72,105.64
69.72,105.77 69.69,105.89 69.64,105.95
68.84,107.42 67.41,109.00 64.28,109.00
60.36,109.00 58.03,106.34 58.03,102.67
58.03,98.80 60.23,96.00 64.14,96.00
67.66,96.00 69.66,98.64 69.89,101.50
69.89,101.56 69.89,101.61 69.89,101.69
69.89,102.55 69.41,103.00 68.66,103.00
68.66,103.00 60.25,103.00 60.25,103.00 Z
M 67.31,101.25
C 67.58,101.25 67.64,101.17 67.64,100.97
67.64,100.95 67.64,100.91 67.64,100.88
67.34,99.33 66.36,97.77 64.14,97.77
61.84,97.77 60.62,99.14 60.31,101.25
60.31,101.25 67.31,101.25 67.31,101.25 Z
M 82.88,108.17
C 82.88,108.67 82.47,109.00 81.78,109.00
81.11,109.00 80.66,108.72 80.66,107.83
80.66,107.83 80.66,91.11 80.66,91.11
80.66,90.38 81.11,90.00 81.78,90.00
82.41,90.00 82.88,90.38 82.88,91.11
82.88,91.11 82.88,96.98 82.88,96.98
82.88,97.50 82.81,98.16 82.75,98.66
82.75,98.66 82.69,98.66 82.69,98.66
83.58,97.05 85.02,96.00 87.22,96.00
90.83,96.00 92.92,98.58 92.92,102.44
92.92,106.25 90.70,109.00 87.16,109.00
84.94,109.00 83.47,107.92 82.70,106.33
82.70,106.33 82.77,106.33 82.77,106.33
82.81,106.66 82.88,107.09 82.88,107.41
82.88,107.41 82.88,108.17 82.88,108.17 Z
M 82.81,102.47
C 82.81,105.16 84.34,107.11 86.86,107.11
89.30,107.11 90.64,105.25 90.64,102.61
90.64,99.70 89.34,97.88 86.94,97.88
84.30,97.88 82.81,99.88 82.81,102.47 Z
M 95.66,96.91
C 95.66,96.31 96.11,96.00 96.78,96.00
97.42,96.00 97.92,96.31 97.92,96.91
97.92,96.91 97.92,108.09 97.92,108.09
97.92,108.70 97.42,109.00 96.78,109.00
96.11,109.00 95.66,108.70 95.66,108.09
95.66,108.09 95.66,96.91 95.66,96.91 Z
M 98.44,91.61
C 98.44,92.55 97.69,93.25 96.80,93.25
95.89,93.25 95.14,92.55 95.14,91.61
95.14,90.70 95.89,90.00 96.80,90.00
97.69,90.00 98.44,90.70 98.44,91.61 Z
M 111.00,96.88
C 111.00,96.38 111.38,96.00 112.06,96.00
112.72,96.00 113.12,96.31 113.12,97.14
113.12,97.14 113.12,107.69 113.12,107.69
113.12,113.56 110.19,114.81 107.03,114.81
104.44,114.81 102.75,113.94 101.84,112.75
101.72,112.56 101.64,112.38 101.64,112.17
101.64,111.83 101.81,111.48 102.11,111.25
102.30,111.09 102.58,110.98 102.83,110.98
103.09,110.98 103.36,111.09 103.55,111.33
104.44,112.45 105.47,113.00 106.92,113.00
109.92,113.00 110.97,111.31 111.00,107.56
111.00,107.30 111.05,106.77 111.09,106.45
111.09,106.45 111.16,106.48 111.16,106.48
110.36,107.95 108.97,109.00 106.86,109.00
103.45,109.00 101.03,106.42 101.03,102.64
101.03,98.73 103.20,96.00 106.91,96.00
109.09,96.00 110.50,97.20 111.17,98.69
111.17,98.69 111.09,98.69 111.09,98.69
111.06,98.44 111.00,98.02 111.00,97.77
111.00,97.77 111.00,96.88 111.00,96.88 Z
M 103.31,102.64
C 103.31,105.52 104.91,107.25 107.14,107.25
109.59,107.25 111.05,105.28 111.05,102.47
111.05,99.78 109.61,97.83 107.17,97.83
104.72,97.83 103.31,99.70 103.31,102.64 Z
M 126.09,91.11
C 126.09,90.38 126.58,90.00 127.19,90.00
127.86,90.00 128.30,90.38 128.30,91.11
128.30,91.11 128.30,108.11 128.30,108.11
128.30,108.72 127.83,109.00 127.16,109.00
126.47,109.00 126.09,108.66 126.09,107.91
126.09,107.91 126.09,107.41 126.09,107.41
126.09,107.14 126.14,106.64 126.17,106.34
126.17,106.34 126.23,106.38 126.23,106.38
125.42,107.97 124.02,109.00 121.78,109.00
118.28,109.00 116.03,106.33 116.03,102.44
116.03,98.64 118.25,96.00 121.81,96.00
124.05,96.00 125.42,97.06 126.27,98.67
126.27,98.67 126.19,98.67 126.19,98.67
126.14,98.19 126.09,97.52 126.09,96.98
126.09,96.98 126.09,91.11 126.09,91.11 Z
M 118.31,102.45
C 118.31,105.30 119.67,107.14 122.09,107.14
124.61,107.14 126.14,105.16 126.14,102.64
126.14,99.91 124.73,97.88 122.09,97.88
119.67,97.88 118.31,99.77 118.31,102.45 Z
M 143.42,102.47
C 143.42,106.23 141.08,109.00 137.34,109.00
133.38,109.00 131.03,106.31 131.03,102.47
131.03,98.72 133.41,96.00 137.28,96.00
141.12,96.00 143.42,98.70 143.42,102.47 Z
M 133.31,102.53
C 133.31,105.28 134.72,107.16 137.19,107.16
139.81,107.16 141.14,105.33 141.14,102.44
141.14,99.72 139.81,97.86 137.36,97.86
134.77,97.86 133.31,99.66 133.31,102.53 Z
M 155.00,96.88
C 155.00,96.38 155.38,96.00 156.06,96.00
156.72,96.00 157.12,96.31 157.12,97.14
157.12,97.14 157.12,107.69 157.12,107.69
157.12,113.56 154.19,114.81 151.03,114.81
148.44,114.81 146.75,113.94 145.84,112.75
145.72,112.56 145.64,112.38 145.64,112.17
145.64,111.83 145.81,111.48 146.11,111.25
146.30,111.09 146.58,110.98 146.83,110.98
147.09,110.98 147.36,111.09 147.55,111.33
148.44,112.45 149.47,113.00 150.92,113.00
153.92,113.00 154.97,111.31 155.00,107.56
155.00,107.30 155.05,106.77 155.09,106.45
155.09,106.45 155.16,106.48 155.16,106.48
154.36,107.95 152.97,109.00 150.86,109.00
147.45,109.00 145.03,106.42 145.03,102.64
145.03,98.73 147.20,96.00 150.91,96.00
153.09,96.00 154.50,97.20 155.17,98.69
155.17,98.69 155.09,98.69 155.09,98.69
155.06,98.44 155.00,98.02 155.00,97.77
155.00,97.77 155.00,96.88 155.00,96.88 Z
M 147.31,102.64
C 147.31,105.52 148.91,107.25 151.14,107.25
153.59,107.25 155.05,105.28 155.05,102.47
155.05,99.78 153.61,97.83 151.17,97.83
148.72,97.83 147.31,99.70 147.31,102.64 Z
M 161.23,91.11
C 161.23,90.44 161.80,90.00 162.59,90.00
163.36,90.00 163.92,90.44 163.91,91.11
163.91,91.11 163.56,103.41 163.56,103.41
163.55,103.97 163.05,104.23 162.56,104.23
162.06,104.23 161.58,103.97 161.58,103.41
161.58,103.41 161.23,91.11 161.23,91.11 Z
M 164.27,107.36
C 164.27,108.36 163.45,109.00 162.56,109.00
161.62,109.00 160.88,108.36 160.88,107.36
160.88,106.34 161.62,105.70 162.56,105.70
163.45,105.70 164.27,106.34 164.27,107.36 Z"/>
</vector>
This will generate a list-layer drawable like the following:
Most image tools (gimp, photoshop, illustrator, etc.) give you the ability to export the path that can be used for the vector graphic.
Checkout the gist at github!