如何阻止无限动画android

时间:2014-02-19 07:34:19

标签: android xml animation

我创建了一个用于旋转imageview的类。它的工作正常,但是当我使用stopAnimation()函数时,它不起作用! 每次调用stopAnimation()函数时,如何停止动画? 我的最低目标是11。 我的班级:

public class RotateImageView extends ImageView {
private Animation mRotation;
public boolean isAnimating = false;

public RotateImageView(Context context) {
super(context);
Init(null);
}

public RotateImageView(Context context, AttributeSet attrs) {
super(context, attrs);
Init(attrs);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public RotateImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Init(attrs);
}

private void Init(AttributeSet attrs) {
startAnimation();
}

public void startAnimation() {
if (mRotation == null) {
    mRotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
    mRotation.setRepeatCount(Animation.INFINITE);
}
this.startAnimation(mRotation);
        isAnimating  = true;
}

public void stopAnimation() {
if (mRotation != null)
    mRotation = null ;

     this.clearAnimation();
        isAnimating  = false;
}

@Override
public void setVisibility(int visibility) {
if (visibility == GONE || visibility == INVISIBLE) {
    this.clearAnimation();
} else if (visibility == VISIBLE) {
    this.startAnimation(mRotation);
}
super.setVisibility(visibility);
 }
}

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button)findViewById(R.id.btn);
    final RotateImageView image = new RotateImageView(getApplicationContext());
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

             image.stopAnimation();
        }
    });



}

mylayout.xml:

<Button 
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    />
<com.example.ex48.RotateImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_centerInParent="true"  
    />

0 个答案:

没有答案