java动画代码_安卓动画基础02_Java代码实现

上一节我们介绍的补间动画是以xml的形式实现的,现在我们用Java代码的方式实现。

渐变对应的类 (AlphaAnimation)

//构造方法

public AlphaAnimation(float fromAlpha, float toAlpha) {

mFromAlpha = fromAlpha;

mToAlpha = toAlpha;

}

在点击事件之后,我们不再调用引入xml动画方式来加载动画,而是通过下来的Java代码来实现渐变动画。

//实例化渐变动画

AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);

//变化时间

alphaAnimation.setDuration(3000);

//保持动画之后的状态

alphaAnimation.setFillAfter(true);

//设置动画的重复模式:反转REVERSE和重新开始RESTART

alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);

//设置动画播放次数

alphaAnimation.setRepeatCount(1);

//设置监听

alphaAnimation.setAnimationListener(new Animation.AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

});

//清除动画

viewDemo.clearAnimation();

//给View设置动画

viewDemo.setAnimation(alphaAnimation);

//开始动画,不调用好像也可以开始

alphaAnimation.start();

最后的效果:渐变从设置的0.0f到1.0f,持续3秒,动画执行一次,保持最好状态

d6ff4ee99f6c

渐变动画

平移对应的类 (TranslateAnimation)

我们先来看看几个构造方法

public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {

mFromXValue = fromXDelta;

mToXValue = toXDelta;

mFromYValue = fromYDelta;

mToYValue = toYDelta;

mFromXType = ABSOLUTE;

mToXType = ABSOLUTE;

mFromYType = ABSOLUTE;

mToYType = ABSOLUTE;

}

public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,

int fromYType, float fromYValue, int toYType, float toYValue) {

mFromXValue = fromXValue;

mToXValue = toXValue;

mFromYValue = fromYValue;

mToYValue = toYValue;

mFromXType = fromXType;

mToXType = toXType;

mFromYType = fromYType;

mToYType = toYType;

}

fromXType:动画开始前的X坐标类型。取值范围为ABSOLUTE(绝对位置)、RELATIVE_TO_SELF(以自身宽或高为参考)、RELATIVE_TO_PARENT(以父控件宽或高为参考)。

fromXValue:动画开始前的X坐标值。当对应的Type为ABSOLUTE时,表示绝对位置;否则表示相对位置,1.0表示100%。

toXType:动画结束后的X坐标类型。

toXValue:动画结束后的X坐标值。

fromYType:动画开始前的Y坐标类型。

fromYValue:动画开始前的Y坐标值。

toYType:动画结束后的Y坐标类型。

toYValue:动画结束后的Y坐标值。

如果使用第一个构造方法,默认的坐标类型是ABSOLUTE(绝对位置),看下来的Java代码和运行效果

//实例化平移动画

TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 200.0f, 0.0f, 200.0f);

//变化时间

translateAnimation.setDuration(3000);

//保持动画之后的状态

translateAnimation.setFillAfter(true);

//设置动画的重复模式:反转REVERSE和重新开始RESTART

translateAnimation.setRepeatMode(AlphaAnimation.RESTART);

//设置动画播放次数

translateAnimation.setRepeatCount(0);

//设置监听

translateAnimation.setAnimationListener(new Animation.AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

});

//清除动画

viewDemo.clearAnimation();

//给View设置动画

viewDemo.setAnimation(translateAnimation);

translateAnimation.start();

d6ff4ee99f6c

平移动画1

下面我们设置以父元素为50%也就是0.5的情况,横向偏移,只需要调用另外一个构造方法即可,设置偏移类型为相对父元素的 Animation.RELATIVE_TO_PARENT

TranslateAnimation translateAnimation = new TranslateAnimation(

Animation.RELATIVE_TO_PARENT, 0.0f,

Animation.RELATIVE_TO_PARENT, 0.5f,

Animation.RELATIVE_TO_PARENT, 0.0f,

Animation.RELATIVE_TO_PARENT, 0.0f

);

d6ff4ee99f6c

平移动画2

缩放对应的类 (ScaleAnimation)

和上面的动画类,相似,也有几个构造方法,直接看下面代码,默认相对于左上角xy的初始化位置0坐标的位置进行缩放。

//实例化缩放动画

ScaleAnimation scaleAnimation = new ScaleAnimation(

0.0f,

1.0f,

0.0f,

1.0f

);

//变化时间

scaleAnimation.setDuration(3000);

//保持动画之后的状态

scaleAnimation.setFillAfter(true);

//设置动画的重复模式:反转REVERSE和重新开始RESTART

scaleAnimation.setRepeatMode(AlphaAnimation.RESTART);

//设置动画播放次数

scaleAnimation.setRepeatCount(0);

//清除动画

viewDemo.clearAnimation();

//给View设置动画

viewDemo.setAnimation(scaleAnimation);

scaleAnimation.start();

d6ff4ee99f6c

缩放动画

如果想相对于自身的或者父布局为基准,可以设置 pivotYType 默认是 ABSOLUTE,对应的构造方法如下

public ScaleAnimation(float fromX, float toX, float fromY, float toY,

int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {

mResources = null;

mFromX = fromX;

mToX = toX;

mFromY = fromY;

mToY = toY;

mPivotXValue = pivotXValue;

mPivotXType = pivotXType;

mPivotYValue = pivotYValue;

mPivotYType = pivotYType;

initializePivotPoint();

}

我们设置一下相对于自己中心点来进行一个缩放效果显示

ScaleAnimation scaleAnimation = new ScaleAnimation(

0.0f,

1.0f,

0.0f,

1.0f,

Animation.RELATIVE_TO_SELF, //相对于自己

0.5f,

Animation.RELATIVE_TO_SELF,//相对于自己

0.5f

);

旋转对应的类 (ScaleAnimation)

/**

* Constructor to use when building a RotateAnimation from code

*

* @param fromDegrees Rotation offset to apply at the start of the

* animation.

*

* @param toDegrees Rotation offset to apply at the end of the animation.

*

* @param pivotXType Specifies how pivotXValue should be interpreted. One of

* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or

* Animation.RELATIVE_TO_PARENT.

* @param pivotXValue The X coordinate of the point about which the object

* is being rotated, specified as an absolute number where 0 is the

* left edge. This value can either be an absolute number if

* pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%)

* otherwise.

* @param pivotYType Specifies how pivotYValue should be interpreted. One of

* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or

* Animation.RELATIVE_TO_PARENT.

* @param pivotYValue The Y coordinate of the point about which the object

* is being rotated, specified as an absolute number where 0 is the

* top edge. This value can either be an absolute number if

* pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%)

* otherwise.

*/

public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,

int pivotYType, float pivotYValue) {

mFromDegrees = fromDegrees;

mToDegrees = toDegrees;

mPivotXValue = pivotXValue;

mPivotXType = pivotXType;

mPivotYValue = pivotYValue;

mPivotYType = pivotYType;

initializePivotPoint();

}

我们可以看之前XML设置的属性来对应上面的参数进行设置,一个相对于自己中心点旋转180度的效果。

android:fromDegrees 旋转的起始点(旋转开始的角度)

android:toDegrees 旋转的结束点(旋转最终角度)

andoird:pivotX 旋转点的X值(距离左侧的偏移量)

android:pivotY旋转点的Y值(距离顶部的偏移量)

//对应的初始化构造方法

RotateAnimation rotateAnimation = new RotateAnimation(

0.0f,

180.0f,

Animation.RELATIVE_TO_SELF,

0.5f,

Animation.RELATIVE_TO_SELF,

0.5f

);

//变化时间

rotateAnimation.setDuration(3000);

//保持动画之后的状态

rotateAnimation.setFillAfter(true);

//设置动画的重复模式:反转REVERSE和重新开始RESTART

rotateAnimation.setRepeatMode(AlphaAnimation.RESTART);

//设置动画播放次数

rotateAnimation.setRepeatCount(0);

//清除动画

viewDemo.clearAnimation();

//给View设置动画

viewDemo.setAnimation(rotateAnimation);

rotateAnimation.start();

d6ff4ee99f6c

旋转动画

多个动画的组合运用(AnimationSet)

我们新建两个动画,一个是旋转动画,一个是缩放动画

/*

* 创建一个AnimationSet,它能够同时执行多个动画效果

* 构造方法的入参如果是“true”,则代表使用默认的interpolator,

* 如果是“false”则代表使用自定义interpolator

*/

AnimationSet animationSet = new AnimationSet(true);

RotateAnimation rotateAnimation = new RotateAnimation(

0.0f,

180.0f,

Animation.RELATIVE_TO_SELF,

0.5f,

Animation.RELATIVE_TO_SELF,

0.5f

);

ScaleAnimation scaleAnimation = new ScaleAnimation(

0.0f,

1.0f,

0.0f,

1.0f,

Animation.RELATIVE_TO_SELF,

0.5f,

Animation.RELATIVE_TO_SELF,

0.5f

);

然后添加到AnimationSet

animationSet.addAnimation(rotateAnimation);

animationSet.addAnimation(scaleAnimation);

animationSet.setFillAfter(true);

animationSet.setDuration(3000);

animationSet.setRepeatMode(AlphaAnimation.RESTART);

animationSet.setRepeatCount(0);

viewDemo.startAnimation(animationSet);

d6ff4ee99f6c

组合动画

我们看到,几个动画的类应用基本都是一样的。所以不多举例了,我们应用这些基本动画就可以设置到我们需要的动画啦。这部分就先写到这吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值