android SDK提供给我们四种动画,ScaleAnimaction(缩放动画),AlphaAnimaction(透明度渐变动画) ,RotateAnimaction(旋转动画),TranslateAnimaction(位移动画),不多说了,注释代码里都有!
package com.example.tp_zhao.animation;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
/**
* Created by TP_zhao on 2017/7/31.
*/
public class AnimationUtils {
static AnimationUtils animationUtils;
private ScaleAnimation animation;
public static AnimationUtils getInstant(){
if(animationUtils == null){
animationUtils = new AnimationUtils();
}
return animationUtils;
}
public void scaleAnimaction(ImageView image,float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue){
/**
* 缩放动画
* public ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
* float fromX 动画开始时 X坐标上的伸缩尺寸
* float toX 动画结束时 X坐标上的伸缩尺寸
* float fromY 动画起始时Y坐标上的伸缩尺寸
* float toY 动画结束时Y坐标上的伸缩尺寸
* int pivotXType 动画在X轴相对于物件位置类型
* float pivotXValue 动画相对于物件的X坐标的开始位置
* int pivotYType 动画在Y轴相对于物件位置类型
* float pivotYValue 动画相对于物件的Y坐标的开始位置
*
*/
/***
* 本着优化内存的原则,尽量少的创建对象,本来想判断一下对象不为空的时候重新设置动画开始结束时候xy轴的值,得没有重新设置的方法,所以只有咋用完后释放掉了
*/
/* if(animation == null) {
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
}else{
}*/
animation = new ScaleAnimation(fromX, toX, fromY, toY,
Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF, pivotYValue);
animation.setDuration(2000);//设置动画持续时间
image.startAnimation(animation);
/**
* 结束的时候调用,可能用不上,但是还是要强调一下这个
* 告诉垃圾回收器,有空的对象了,过来回收吧
* 这里之所以不用System.gc();调用回收,是因为在源码中调用了System.gc()会经过一系列的判断,最后调用的也是Runtime.getRuntime().gc()
*
* animation.cancel();
* Runtime.getRuntime().gc();
*/
}
/**
*
* @param image 图片view引用啊
* @param fromAlpha 动画开始透明度以1完全不透明,0是....
* @param toAlpha 结束时候的透明度
* @param carryCount 动画执行几次
* @param endType 动画结束后是否停留在执行状态
* @param sleepTime 睡多久开始执行
*/
public void alphaclick(ImageView image,float fromAlpha, float toAlpha,int carryCount,boolean endType,int sleepTime){
/** 设置透明度渐变动画 */
final AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);
animation.setDuration(2000);//设置动画持续时间
/** 常用的方法 */
if (carryCount != 0) {
//设置重复次数
animation.setRepeatCount(carryCount);
}
//动画执行完后是否停留在执行完的状态
animation.setFillAfter(endType);
//执行前的等待时间
animation.setStartOffset(sleepTime);
image.startAnimation(animation);
/** 开始动画 */
}
/**
*旋转动画
* @param image ImageView哇
* @param fromDegrees 旋转的其实比角度
* @param toDegrees 旋转结束的角度
* @param pivotXType X轴的伸缩模式,提供有不同的样式选择
* @param pivotXValue X坐标的拉伸值
* @param pivotYType Y轴的伸缩模式,提供有不同的样式选择
* @param pivotYValue Y坐标的拉伸值
*/
public void rotateAnimaction(ImageView image,float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue){
final RotateAnimation animation =new RotateAnimation(fromDegrees,toDegrees,pivotXType,
pivotXValue,pivotYType,pivotYValue);
animation.setDuration(3000);//设置动画持续时间
image.startAnimation(animation);
}
/**
* 位移动画
* @param image
* @param fromXDelta X坐标开始位置
* @param toXDelta X坐标结束位置
* @param fromYDelta Y坐标开始位置
* @param toYDelta Y坐标结束位置
* @param runTime 运行时间
* @param runCount 运行次数
*/
public void translateAnimaction(ImageView image,float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,int runTime,int runCount){
final TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta,fromYDelta, toYDelta);
animation.setDuration(runTime);//设置动画持续时间
animation.setRepeatCount(runCount);//设置重复次数
animation.setRepeatMode(Animation.REVERSE);//设置反方向执行
image.setAnimation(animation);
}
}