Android旋转动画

第一种:
代码继承Animation实现
package com.iaiai.an;

import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

/**
 * 
 * <br/>
 * Title: WindowAnimation.java<br/>
 * E-Mail: [email protected]<br/>
 * QQ: 176291935<br/>
 * Http: iaiai.iteye.com<br/>
 * Create time: 2013-8-27 上午10:22:22<br/>
 * <br/>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class WindowAnimation extends Animation {

	private int halfWidth;
	private int halfHeight;
	private int duration;

	public WindowAnimation(int duration) {
		this.duration = duration;
	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		super.applyTransformation(interpolatedTime, t);
		Matrix matrix = t.getMatrix();
//		matrix.preScale(interpolatedTime, interpolatedTime);
		matrix.preRotate(interpolatedTime * 360); // 进行旋转
		matrix.preTranslate(-halfWidth, -halfHeight); // 改变动画的起始位置,把扩散点和起始点移到中间
		matrix.postTranslate(halfWidth, halfHeight);
	}

	@Override
	public void initialize(int width, int height, int parentWidth, int parentHeight) {
		super.initialize(width, height, parentWidth, parentHeight);
		this.setDuration(duration);
		this.setFillAfter(true);
		this.halfHeight = height / 2;
		this.halfWidth = width / 2;
		this.setInterpolator(new LinearInterpolator());
	}

}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Animation animation = new WindowAnimation(1000);
		animation.setRepeatCount(-1);	//执行多少次
		
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				findViewById(R.id.imageView1).setAnimation(animation);
				animation.start();
			}
		});
	}



第二种:
在xml中配置
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:shareInterpolator="true"
        android:toDegrees="+360" />
    <!--
rotate 旋转动画效果    
repeatCount 旋转次数
===========================
interpolator 指定一个动画的插入器,用来控制动画的速度变化   
AccelerateInterpolator:动画开始时比较慢,然后逐渐加速。
DecelerateInterpolator:动画开始时比较快,然后逐渐减速。
AccelerateDecelerateInterpolator:动画开始时和结束时比较慢,中间过程加速。
LinearInterpolator:动画匀速进行。
CycleInterpolator:动画循环播放指定次数,速率沿着正弦曲线改变。)
=============================
fromDegrees 属性为动画起始时物件的角度     
toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针    
duration 属性为动画持续时间,以毫秒为单位
pivotX/pivotY 属性为动画旋转中心位置
shareInterpolator 


    -->

</set>


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Animation animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.xz);
		
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				findViewById(R.id.imageView1).setAnimation(animation);
				animation.start();
			}
		});
	}

猜你喜欢

转载自iaiai.iteye.com/blog/1931437