android下Rotate旋转动画实现效果

旋转动画在我开发以来并未过多用到,但这个效果确实给人以很炫的感觉,旋转嘛,一直不停的转,比起其它动画效果可能就美观一些。另外在这次的动画中也写到了几个动画的综合使用。接下来让我们从代码中学习吧。

首先在src文件中定义一个anim文件夹,在anim文件夹下再定义一个动画的xml文件。如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="2000"
        android:fromAlpha="1"
        android:toAlpha="0.5" />

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

    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0"
        android:toYScale="0" />

    <translate
        android:duration="2000"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="45%p"
        android:toYDelta="-45%p" />

</set>
在这里我们最外层包围了一个set,所谓set就是无序集合的意思,也就是说四种动画可以同时执行。我们在set里面分别对每个动画加以设置,这样就可以实现我们想要的动画联合效果。


接下来再说说avtivity文件里的代码,有注释,不懂的可以多看看。如下:

public class RotateAnimationDemoActivity extends Activity implements OnClickListener {
    private ImageView mImageView;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.btn_rotate).setOnClickListener(this);
        mImageView = (ImageView) findViewById(R.id.imageview);
    }

	public void onClick(View v) {
		//startRotateAnimationJavaCode();
		startRotateAnimationXml();
	}

	private void startRotateAnimationXml() {
		//四种动画叠加效果
		AnimationSet setAnim = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
		mImageView.startAnimation(setAnim);
	}

	private void startRotateAnimationJavaCode() {
		//旋转动画  参数分别是  起始的角度  转多少度  相对于自身的多少旋转 
		RotateAnimation retateAnim = new RotateAnimation(
				0.0f, 720f, 
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f);
		retateAnim.setDuration(1000);
		mImageView.startAnimation(retateAnim);
	}
}
这段代码里面有两个方法//startRotateAnimationJavaCode();
startRotateAnimationXml();  有兴趣的朋友可以通过反注释来看到单纯的旋转效果。这里我就不细讲了。想要见证奇迹,那就运行去看吧。


以上有什么不准的地方或者写的不到位的地方还望广大博友多多提起,本人以后会多加注意。以下附图。                         完毕!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章