Android ImageView Animation

前端 未结 9 1010
忘掉有多难
忘掉有多难 2020-12-04 07:32

I\'ve created a layout with an image view and a web view. The web view is set to have a default visibility of gone. When the activity fires up it displays the image view fir

相关标签:
9条回答
  • 2020-12-04 07:59

    Don't hard code image bounds. Just use:

    RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
    
    0 讨论(0)
  • 2020-12-04 08:02
    imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
        imgDics.setOnClickListener(onPlayer2Click);
        anim = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                                0.5f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(4000);
    
        // Start animating the image
        imgDics.startAnimation(anim);
    
    0 讨论(0)
  • 2020-12-04 08:07

    Inside the element put:

    android:repeatCount="infinite"
    
    0 讨论(0)
  • 2020-12-04 08:11

    Use a RotateAnimation, setting the pivot point to the centre of your image.

    RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(700);
    
    // Start animating the image
    final ImageView splash = (ImageView) findViewById(R.id.splash);
    splash.startAnimation(anim);
    
    // Later.. stop the animation
    splash.setAnimation(null);
    
    0 讨论(0)
  • 2020-12-04 08:12

    Verified Code:

    imageView.setImageResource(R.drawable.ic_arrow_up);
    
    boolean up = true;
    
    if (!up) { 
        up = true; 
        imageView.startAnimation(animate(up)); 
    } else { 
        up = false; 
        imageView.startAnimation(animate(up)); 
    }
    
    private Animation animate(boolean up) {
        Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
        anim.setInterpolator(new LinearInterpolator()); // for smooth animation
        return anim;
    }
    

    drawable/ic_arrow_up.xml

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24.0"
            android:viewportHeight="24.0">
        <path
            android:fillColor="#3d3d3d"
            android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
    </vector>
    

    anim/rotate_up.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true"
        android:fillEnabled="true">
        <rotate
            android:duration="200"
            android:fromDegrees="-180"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="0" />
    </set>
    

    anim/rotate_down.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true"
        android:fillEnabled="true">
        <rotate
            android:duration="200"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="180" />
    </set>
    
    0 讨论(0)
  • 2020-12-04 08:22

    How to rotate an image around its center:

    ImageView view = ... //Initialize ImageView via FindViewById or programatically
    
    RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    
    //Setup anim with desired properties
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely
    anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds
    
    //Start animation
    view.startAnimation(anim); 
    //Later on, use view.setAnimation(null) to stop it.
    

    This will cause the image to rotate around its center (0.5 or 50% of its width/height). I am posting this for future readers who get here from Google, as I have, and who wish to rotate the image around its center without defining said center in absolute pixels.

    0 讨论(0)
提交回复
热议问题