2014-02-18 3 views

ответ

3
public class RotateImageView extends ImageView { 

private Animation mRotation; 
    public bool 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) 
     clearAnimation(); 
      isAnimating = false; 
} 

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

}

rotate.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="5000" 
    android:fromDegrees="0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:startOffset="0" 
    android:toDegrees="360" /> 

EDITED

RotateImageView image = new RotateImageView(Context); 
    image.addOnClickListener(new View.OnClickListener(){ 
    if(image.isAnimating) 
     image.stopAnimating(); 
    else 
     image.startAnimating(); 
} 
+0

это нормально, но когда я добавляю com.example.ex48.RotateImageView в моем макете, ImageView по умолчанию вращается! Я хотел обработать этот образ Когда пользователь нажимает на него. – user3103823

+0

Вы можете добавить флаг в RotateImageView и добавить слушателя onClick. Таким образом, вы можете проверить флаг и запустить анимацию \ stop. PS: см. Править –

+0

Функция stopAnimation() не работает. !!! – user3103823

0

Используйте этот animation.xml:

<rotate 
      android:fromDegrees="0" 
      android:toDegrees="360" 
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:duration="5000" /> 
Смежные вопросы