animate single layer-list item in android - android

I have a "layer-list" (rotated_image.xml) which is a "imageview" background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:gravity="center"
android:src="#drawable/shadow" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/bg" />
</item>
<item>
<rotate
android:duration="5000"
android:fromDegrees="0"
android:interpolator="#drawable/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" >
<bitmap
android:gravity="center"
android:src="#drawable/button" />
</rotate>
</item>
1) I need to start and stop third image rotation on "imageview" click. I do not know how to get reference to my third item in layer-list and how to start animation?
2) I also need to put opacity to my second item in list, is it possible?
3) Can I combine one more animation here (fade in-fade out), I need it for my first item in list?
If my question is to complicated, can someone give me good reference or advice?
Thanks,
Milos

Related

How do you animate the background of a TextView that has been defined in xml?

For example I have the following as the background of a TextView:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:id="#+id/pulsator">
<stroke android:width="4dp" android:color="#77FF00" />
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
/>
<size
android:height="8dp"
android:width="8dp"/>
</shape>
In the main activity I have:
txtMessageData = (TextView) findViewById(R.id.txtMessageData);
The background is set in xml as:
<TextView
.
.
android:background="#drawable/pulsator />
Animating the TextView itself is easy but I actually only want to animate the background. Essentially I'm aiming to have rings pulsating concentrically away from the TextView too show that it is actively updating.
I would think maybe a property animation could be used but I have not found a way to do it.
I have the following animation set defined in xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1500"
android:fromAlpha="1.0"
android:interpolator="#android:anim/linear_interpolator"
android:toAlpha="0.0"
/>
<scale
android:duration="1500"
android:fromXScale="1.0"
android:toXScale="2.5"
android:fromYScale="1.0"
android:toYScale="2.5"
/>
</set>
Can this animation data be applied to the background of my TextView?
I think you can use a LinearLayout and put into it your text view then animate this linearLayout... try it.

How do I make a shape scale up and stay that size in android?

I have some knowledge of drawables and animations in android. I created a drawable rectangle along with an animation scale. Here is my scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="2.0" />
</set>
Here is my rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#5a5a5a88"
android:endColor="#14141488"
android:angle="270" android:centerX="0.25"/>
</shape>
</item>
</layer-list>
When I launch the app and click on the rectangle, it just grows and then goes back to it's regular scale. I want it to grow and stay that scale. How do i do that?
You have to use
android:fillAfter="true"
to make your view stable at the end of the animation. See here.

Animation defined in XML doesn't start

I found the pngs for the standard progress spinner in Android. However, when I try to animate them in a similar way to how android does it with the following code nothing happens. They just appear to be a static icons.
The button
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#anim/anim"
android:drawableRight="#anim/anim" >
</Button>
The animation
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="#drawable/spinner_76_outer_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="1080" />
</item>
<item>
<rotate
android:drawable="#drawable/spinner_76_inner_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="720"
android:toDegrees="0" />
</item>
</layer-list>
Is it even possible to start animations automatically that is defined in XML only or do they have to be started with java?
You can use animated-rotate but that will only work on API 11+. If you use rotate, then you'll have to start them in Java, and animate them by animating the level (drawable.setLevel()) between 0 and 10000 (sounds weird? It is, but it's how it's done).

rotate and repeat image using xml

I want to set background image with rotation and repeating the background image.
For repeating
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/border"
android:tileMode="repeat" />
For rotation
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="#drawable/border">
Now I want to combine this code in one drawable and want to use as background
I'm trying like this but not working
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat" >
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="#drawable/border">
</rotate>
</bitmap>
I would suggest you to use a custom view. I also tried that for my animated background. Just use thread, handler and canvas.scale() method for rotating the view background. And one thing i wanna ask. Whats the need of setting as background? You can set the rotating drawable as a background view and do the other things on the view above with transparent background.
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="-45"
android:pivotX="50%"
android:pivotY="50%">
<bitmap
android:alpha=".5"
android:src="#drawable/image"
android:tileMode="repeat" />
</rotate>

How to decrease the speed of custom ProgressBar

I have implemented a custom progress bar and I am displaying it while getting data from an URL, it is working fine as progress bar is spinning, but my problem is that I am not able to find a proper way to decrease its revolving speed, currently it is spinning very fast.
please help me with your suggestions and reply.
xml layout for progress bar:-
<ProgressBar
android:id="#+id/showProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="#drawable/my_progress_indeterminate" />
Also here's "my_progress_indeterminate" which is used to rotate my custom progressbar :-
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/image_loading"
android:pivotX="50%"
android:pivotY="50%" />
please share some example if possible to decrease the speed of rotation.
Any suggestions will be appreciated.
add in code to progress.xml
android:fromDegrees="0"
android:toDegrees="1080" <!-- Add in code to progress custumize xml to speed up -->
Try this. It will help you.
Create progressbar_custom.xml
/res/drawable/progressbar_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadius="18dp"
android:shape="ring"
android:thickness="5dp"
android:useLevel="false" >
<size
android:height="48dp"
android:width="48dp" />
<gradient
android:centerColor="#802A67AD"
android:centerY="0.5"
android:endColor="#ff2A67AD"
android:startColor="#002A67AD"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
Add Progressbar in XML files
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="#drawable/progressbar_custom" />
Use "rotate" instead of "animated-rotate" and changing android:toDegrees or android:toDegress should make it go slower.
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/image_loading"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />

Categories

Resources