2016-05-05 5 views
0

I habe den folgenden Code, der kontinuierlich ein Bild zu drehen versucht:Drehen eines Bildes kontinuierlich

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

Das Problem mit dem oben:

Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_animation); image.startAnimation(animation);

Die rotate_animation.xml Datei wie folgt ist Code ist, dass es das Bild dreht und pausiert, bevor es erneut dreht. Nach was ich suche, ist glatte ununterbrochene Rotation, die das nur stoppen wird, wenn ich es ausdrücklich benötige, um zu stoppen.

+0

statt '' Animation Verwendung '' zeichnbar – pskink

Antwort

1

Wechseln Sie zu Property-Animatoren, was jetzt der empfohlene Ansatz für Animationen ist. Versuchen Sie, diese -

ImageView imageview = (ImageView)findViewById(R.id.yourimage); 

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview , 
        "rotation", 0f, 360f); 
imageViewObjectAnimator.setRepeatCount(ObjectAnimator.INFINITE); 
imageViewObjectAnimator.setRepeatMode(ObjectAnimator.RESTART); 
imageViewObjectAnimator.setInterpolator(new AccelerateInterpolator()); 
imageViewObjectAnimator.start(); 
0

ich erfolgreich animiert drehen in einer Aktivität verwendet haben, sehen meine rotate.xml

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:drawable="@drawable/ic_rounded_loading" 
android:duration="1000" 
android:interpolator="@android:anim/linear_interpolator" 
android:pivotX="50%" 
android:pivotY="50%" 
android:repeatCount="infinite" /> 

und in activity_main.xml Datei,

<ImageView 
    android:id="@+id/ivLoading" 
    android:layout_width="200px" 
    android:layout_height="200px" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/rotate" 
    android:layout_centerVertical="true" /> 

Aber nicht an einem Fragment arbeiten. Also habe ich es in einem Fragment von unten Code, der kontinuierlich ein Bild dreht,

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    // Accessing the layout components to be displayed. 
    rootView = inflater.inflate(R.layout.fragment_loading, container, false); 
    ivLoading= (ImageView) rootView.findViewById(R.id.ivLoading); 

    RotateAnimation rotateAnimation = new RotateAnimation(
      0,//float: Rotation offset to apply at the start of the animation. 
      360,//float: Rotation offset to apply at the end of the animation. 
      Animation.RELATIVE_TO_SELF,//int: Specifies how pivotXValue should be interpreted 
      0.5f,//float: The X coordinate of the point about which the object is being rotated 
      Animation.RELATIVE_TO_SELF,//int: Specifies how pivotYValue should be interpreted 
      0.5f//float: The Y coordinate of the point about which the object is being rotated 
    ); 
    rotateAnimation.setDuration(1500);//How long this animation should last. 
    rotateAnimation.setRepeatCount(Animation.INFINITE);//Sets how many times the animation should be repeated. 
    rotateAnimation.setInterpolator(new LinearInterpolator());//Sets the acceleration curve for this animation. 
    ivLoading.startAnimation(rotateAnimation); 

    return rootView; 
} 

und in fragment_loading.xml Datei,

 <ImageView 
    android:id="@+id/ivLoading" 
    android:layout_width="200px" 
    android:layout_height="200px" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/ic_rounded_loading" 
    android:layout_centerVertical="true" />