Reputation: 279
I am using a list view in which i have button for downloading.
I Want to rotate button on click event until some background downloading process working.
The rotation works fine but some delay when one cycle completes.
The main problem is that when button is animating and user scroll the list some other buttons in different rows also starts animation.
I have an array of boolean type to keep button state isDownloading[]. so i get the button on that position and start animation but it creating problem
Code to get button fro animation :
else if (isDownloading[position] == true)
{
holder.downloadListBtn.setBackgroundResource(R.drawable.downloading);
LinearLayout layout = (LinearLayout) holder.downloadListBtn.getParent();
Button button = (Button) layout.getChildAt(0);
ButtonAnimate(button);
}
Code to animate button :
public void ButtonAnimate(Button b)
{
RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(4500);
animation.setRepeatCount(100);
b.startAnimation(animation);
}
Upvotes: 1
Views: 919
Reputation: 1045
All of button starts animating because they are having same id, when they got focus they starts animating. so, what you have to do is assign different ids or tags.
On the basis of that id and tag, make that button rotate.
try this code on button click
Rotater.runRotatorAnimation(this, v.getId());
public class Rotater {
public static void runRotatorAnimation(Activity act, int viewId) {
// load animation XML resource under res/anim
Animation animation = AnimationUtils.loadAnimation(act, R.anim.rotate);
if (animation == null) {
return; // here, we don't care
}
// reset initialization state
animation.reset();
// find View by its id attribute in the XML
View v = act.findViewById(viewId);
// cancel any pending animation and start this one
if (v != null) {
v.clearAnimation();
v.startAnimation(animation);
}
}
}
here is rotate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360" >
</rotate>
Upvotes: 3