2012-10-16 46 views
1

我正在使用列表视图,其中我有用于下载的按钮。ListView问题中的按钮动画

我想旋转按钮点击事件,直到一些后台下载过程工作。

旋转工作正常,但一个周期完成后有些延迟。

主要问题是,当按钮是动画和用户滚动列表不同行中的一些其他按钮也开始动画。

我有一个阵列布尔类型的保持按钮状态isDownloading []。所以我得到那个位置的按钮,启动动画,但它产生的问题

代码来获得按钮来回动画:

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); 
     } 

代码动画按钮:

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); 
     } 

回答

3

所有的按钮开始动画,因为他们有相同的ID,当他们获得焦点时,他们开始动画。所以,你必须做的是分配不同的ID或标签。

在该id和标记的基础上,使该按钮旋转。

尝试按钮验证码点击

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); 
    } 
} 
} 

这里是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>