如何在中心点旋转android中的imageview

时间:2014-02-18 12:37:35

标签: android animation imageview

我想在中心点上旋转一个持续时间(例如:5秒)的图像视图。 可能吗 ?怎么样?

像这张照片一样:

enter image description here

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

使用此animation.xml:

 <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="5000" />