帧动画代码实现

package com.example.day06_code;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView);
}

// 透明
public void alpha(View v) {

    AlphaAnimation animation = new AlphaAnimation(0f, 1.5f);
    // 持续时间
    animation.setDuration(2000);
    // 设置重复次数
    animation.setRepeatCount(2);
    // 重复的模式
    animation.setRepeatMode(Animation.REVERSE);

    imageView.startAnimation(animation);

}

// 缩放
public void scale(View v) {
    // ScaleAnimation animation=new ScaleAnimation(fromX, toX, fromY, toY,
    // pivotXType, pivotXValue, pivotYType, pivotYValue)
    ScaleAnimation animation = new ScaleAnimation(

    1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_PARENT, 0.5f,
            Animation.RELATIVE_TO_PARENT, 0.5f);
    // 持续时间
    animation.setDuration(2000);
    // 设置重复次数
    animation.setRepeatCount(2);
    // 重复的模式
    animation.setRepeatMode(Animation.REVERSE);
    // 运行完的保持状态
    animation.setFillAfter(true);

    imageView.startAnimation(animation);
}

// 位移
public void translate(View v) {

    // TranslateAnimation animation=new TranslateAnimation(

    // fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue,
    // toYType, toYValue)

    // 前四个参数都是设置x轴,后四个参数都是设置y轴
    // 参数一:x轴起始点的类型(相对于自己,相对于父亲,绝对点)参数二:x轴起始点(当相对于自己和相对于父亲,都是倍数关系,当相对类型是绝对点时,代表的是像素);
    // 参数三:x轴终点的类型,参数四:x轴终点的位置
    TranslateAnimation animation = new TranslateAnimation(

    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,
            Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);

    // 持续时间
    animation.setDuration(2000);
    // 设置重复次数
    animation.setRepeatCount(2);
    // 重复的模式
    animation.setRepeatMode(Animation.REVERSE);
    // 运行完的保持状态
    animation.setFillAfter(true);

    imageView.startAnimation(animation);

}

// 旋转
public void rotate(View v) {

    // 参数一:起始角度
    // 参数二:结束角度
    // 参数三:x轴原点的相对类型
    // 参数四:x轴原点的位置
    // 参数五:y轴原点的相对类型
    // 参数六:y轴原点的位置
    // 使用相对位置时,位置都倍数关系
    RotateAnimation animation = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

    // 持续时间
    animation.setDuration(2000);
    // 设置重复次数
    animation.setRepeatCount(2);
    // 重复的模式
    animation.setRepeatMode(Animation.REVERSE);
    // 运行完的保持状态
    animation.setFillAfter(true);

    imageView.startAnimation(animation);
}

// 集合
public void set(View v) {
    AnimationSet animationSet = new AnimationSet(false);

    // /渐变
    AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.5f);
    // 持续时间
    alphaAnimation.setDuration(2000);
    // 设置重复次数
    alphaAnimation.setRepeatCount(2);
    // 重复的模式
    alphaAnimation.setRepeatMode(Animation.REVERSE);
    // /放缩
    ScaleAnimation scaleAnimation = new ScaleAnimation(

    1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_PARENT, 0.5f,
            Animation.RELATIVE_TO_PARENT, 0.5f);
    // 持续时间
    scaleAnimation.setDuration(2000);
    // 设置重复次数
    scaleAnimation.setRepeatCount(2);
    // 重复的模式
    scaleAnimation.setRepeatMode(Animation.REVERSE);
    // 运行完的保持状态
    scaleAnimation.setFillAfter(true);

    // //位移/

    TranslateAnimation translateAnimation = new TranslateAnimation(

    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,
            Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);

    // 持续时间
    translateAnimation.setDuration(2000);
    // 设置重复次数
    translateAnimation.setRepeatCount(2);
    // 重复的模式
    translateAnimation.setRepeatMode(Animation.REVERSE);
    // 运行完的保持状态
    translateAnimation.setFillAfter(true);

    // ///旋转//

    RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

    // 持续时间
    rotateAnimation.setDuration(2000);
    // 设置重复次数
    rotateAnimation.setRepeatCount(2);
    // 重复的模式
    rotateAnimation.setRepeatMode(Animation.REVERSE);
    // 运行完的保持状态
    rotateAnimation.setFillAfter(true);
    //添加动画到动画集合
    animationSet.addAnimation(alphaAnimation);
    animationSet.addAnimation(scaleAnimation);
    animationSet.addAnimation(translateAnimation);
    animationSet.addAnimation(rotateAnimation);



    imageView.startAnimation(animationSet);
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值