Android ProgressBar setProgressDrawable stretches the vector drawable instead of repeating - android

I have the ff. vector drawable that I want to repeat based on the progress
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="6dp"
android:viewportHeight="6"
android:viewportWidth="10"
android:width="10dp">
<clip-path android:pathData="M 0 6 L 2.3 0 L 5 0 L 2.7 6 Z" />
<path
android:fillColor="#E60000"
android:pathData="M -5 -5 H 10 V 11 H -5 V -5 Z" />
<clip-path android:pathData="M 4.9 6 L 7.2 0 L 9.9 0 L 7.6 6 Z" />
<path
android:fillColor="#E60000"
android:pathData="M -0.1 -5 H 14.9 V 11 H -0.1 V -5 Z" />
</vector>
However setProgressDrawable stretches the entire drawable.
progressBar.setProgressDrawable(ContextCompat.getDrawable(context, getVectorDrawableRes()));
How do I repeat the above vector drawable instead of stretching it?

Related

how to create Animated layout while data loading data?

How to create this layout while fetching data ?
You could have used the Facebook shimmer library for this which is very straightforward.
Facebook Shimmer Library
But for more fine-tuned control you might have to write an animated vector like this.
Source for code Snippet
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
<vector
android:name="vector"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<clip-path
android:name="heart"
android:pathData="M 7 3 C 5.465 3 3.922 3.5 2.75 4.7 C 0.407 7.1 0.471 10.8 2.75 13.2 L 12 23 L 21.25 13.2 C 23.529 10.8 23.593 7.1 21.25 4.7 C 18.907 2.4 15.093 2.4 12.75 4.7 L 12 5.5 L 11.25 4.7 C 10.078 3.5 8.536 3 7 3 Z"/>
<path
android:name="path"
android:pathData="M 7 3 C 5.465 3 3.922 3.5 2.75 4.7 C 0.407 7.1 0.471 10.8 2.75 13.2 L 12 23 L 21.25 13.2 C 23.529 10.8 23.593 7.1 21.25 4.7 C 18.907 2.4 15.093 2.4 12.75 4.7 L 12 5.5 L 11.25 4.7 C 10.078 3.5 8.536 3 7 3 Z"
android:fillColor="#ff0000"/>
<group android:name="group">
<path
android:name="shimmer"
android:pathData="M 4 0 L 24 19 L 22 22 L 0 3 Z">
<aapt:attr name="android:fillColor">
<gradient
android:endColor="#FFC500"
android:endX="24"
android:endY="24"
android:startColor="#ED613A"
android:startX="0"
android:startY="0"
android:type="linear" />
</aapt:attr>
</path>
</group>
</vector>
</aapt:attr>
<target android:name="group">
<aapt:attr name="android:animation">
<set>
<objectAnimator
android:propertyName="translateX"
android:duration="700"
android:valueFrom="10"
android:valueTo="-10"
android:valueType="floatType"
android:interpolator="#android:anim/linear_interpolator"/>
<objectAnimator
android:propertyName="translateY"
android:duration="700"
android:valueFrom="-10"
android:valueTo="10"
android:valueType="floatType"
android:interpolator="#android:anim/linear_interpolator"/>
</set>
</aapt:attr>
</target>
</animated-vector>

Convert SVG into android animated vector drawables

How can i convert this SVG file into a Animated vector drawable for android to show on the activity image.
SVG file
<svg width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg" stroke="#E31F64">
<g fill="none" fill-rule="evenodd">
<g transform="translate(1 1)" stroke-width="2">
<circle stroke-opacity=".5" cx="18" cy="18" r="18"/>
<path d="M36 18c0-9.94-8.06-18-18-18">
<animateTransform
attributeName="transform"
type="rotate"
from="0 18 18"
to="360 18 18"
dur="1s"
repeatCount="indefinite"/>
</path>
</g>
</g>
</svg>
I used some online tools to convert it but they simply creates a vector not the animated vector
Here is the vector
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="38dp"
android:height="38dp"
android:viewportWidth="38"
android:viewportHeight="38">
<group
android:translateX="1"
android:translateY="1">
<path
android:fillType="evenOdd"
android:strokeAlpha=".5"
android:strokeWidth="2"
android:pathData="M 18 0 C 27.941125497 0 36 8.05887450305 36 18 C 36 27.941125497 27.941125497 36 18 36 C 8.05887450305 36 0 27.941125497 0 18 C 0 8.05887450305 8.05887450305 0 18 0 Z" />
<path
android:fillType="evenOdd"
android:strokeWidth="2"
android:pathData="M36 18c0-9.94-8.06-18-18-18" />
</group>
</vector>
How can i create animated out of this to show spinning oval.
Thanks
Use ShapeShifter tool to create Animated Vector Drawable. It is a very basic tool yet fulfills the need.

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>

AnimatedVectorDrawable empty path to the drawable path

What I want
I want to be able to draw a Vector Drawable from no path -> target. So for example, draw from nothing to a check mark SVG.
What I've tried
Here is my Vector drawable:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="128dp"
android:width="128dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:name="done"
android:pathData="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"
android:fillColor="#fff"
android:strokeWidth="1"
android:strokeLineCap="round"
android:strokeColor="#fff"/>
That is just a check mark SVG.
Here is my animation (Which I know is wrong.. :( ). The path data for animating from one vector drawable to another must have the same number of path directions:
<objectAnimator
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="0"
android:valueTo="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"
android:valueType="pathType"
xmlns:android="http://schemas.android.com/apk/res/android" />
And the animated vector (tying together the vector + animation)
<?xml version="1.0" encoding="utf-8"?>
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/ic_done">
<target
android:animation="#animator/ic_disappear"
android:name="opacity"/>
This is wrong as I noted above. How then, do I create an object animator for a SVG to animate from no path to the path I want. This animation is similar to someone drawing the drawable.
As you say, the two paths descriptions have to have the same number of commands and coordinates. Your path is a filled shape, and you can only animate once linearly. So you can't do a two-step down-and-then-up check motion. At least not with a single path morph.
A first simple approach would just be to start from the path start point. That happens to be at the crook of the check mark. So it doesn't look to bad.
<svg width="200" height="200" viewBox="0 0 24 24">
<path d="M0 0">
<animate attributeName="d" attributeType="XML"
from="M9 16.2 L 9 16.2 l 0 0 L 9 16.2 9 16.2 l 0 0 L 9 16.2z"
to="M9 16.2 L 4.8 12 l -1.4 1.4 L 9 19 21 7 l -1.4-1.4 L 9 16.2z"
dur="1s" fill="freeze" />
</path>
</svg>
Or you could start it at the centre point of the corner:
<svg width="200" height="200" viewBox="0 0 24 24">
<path d="M0 0">
<animate attributeName="d" attributeType="XML"
from="M9 17.6 L 9 17.6 l 0 0 L 9 17.6 9 17.6 l 0 0 L 9 17.6z"
to="M9 16.2 L 4.8 12 l -1.4 1.4 L 9 19 21 7 l -1.4-1.4 L 9 16.2z"
dur="1s" fill="freeze" />
</path>
</svg>
Or maybe start with the corner diamond and grow the two "arms" from there.
<svg width="200" height="200" viewBox="0 0 24 24">
<path d="M0 0">
<animate attributeName="d" attributeType="XML"
from="M9 16.2 L 9 16.2 l -1.4 1.4 L 9 19 l 1.4 -1.4 l -1.4-1.4 L 9 16.2z"
to= "M9 16.2 L 4.8 12 l -1.4 1.4 L 9 19 L 21 7 l -1.4-1.4 L 9 16.2z"
dur="1s" fill="freeze" />
</path>
</svg>
You may animate trimPathEnd property from 0 to 1 in duration of your choice instead of animating pathData. So change the propertyName to trimPathEnd and changing the AnimatedVectorDrawable file also suitably. The result is as if the tick is being written now.
Further, if you want a demo read the tutorial "An Introduction to Icon Animation Techniques". There are some interactive demo items in the body of the article.
You may also read about AnimatedVectorDrawable class in android developer site. Though trimPathEnd propertyName is not in the list, it works.

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