Rotate animation android

后端 未结 4 2098
轻奢々
轻奢々 2021-02-07 01:25

I\'m trying to do a rotating image animation. I need to rotate an icon around itself just like they do in a progressbar, but what I\'m getting is an image rotat

相关标签:
4条回答
  • 2021-02-07 01:33

    Well I got where I was wrong. I gave padding to my image which caused it to move aside a little every time it rotated. Anyhow I removed the padding and now it's working just fine.

    0 讨论(0)
  • 2021-02-07 01:36

    This is the source code for the layout main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <Button
            android:id="@+id/testButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="animationText" 
            android:onClick="AnimClick"/>
    
        <ImageView
            android:id="@+id/testImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:contentDescription="image_desc"
            android:scaleType="fitCenter"
            android:src="@drawable/cat2" />
    
    </RelativeLayout>
    

    To implement the rotation animation, we can define the animation by XML or Java code. If we want to write the animation in the xml, we need to create an animation xml file under /res/anim folder. Here, we create a xml file named rotate_around_center_point.xml

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

    and this is my Activity:

    public class MainActivity extends Activity implements OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button btn = (Button) findViewById(R.id.testButton);
            btn.setOnClickListener(this);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);
    
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
            animationTarget.startAnimation(animation);
    
        }
    
    
    }
    
    0 讨论(0)
  • 2021-02-07 01:44

    You could try with the following code, rather than doing it in XML:

    RotateAnimation rotate = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    
    rotate.setDuration(4000);
    rotate.setRepeatCount(Animation.INFINITE);
    yourView.setAnimation(rotate);
    
    0 讨论(0)
  • 2021-02-07 01:57

    Add the following xml in the animation folder

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false" 
        android:duration="4000" 
        android:fromdegrees="0" 
        android:pivotx="50%" 
        android:pivoty="50%"
        android:todegrees="360" 
        android:toyscale="0.0">
     </set>
    

    Hope this will help you..

    for more information http://www.blazin.in/2013/09/simple-rotate-animation-in-android.html

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