安卓中的动画

补间动画和帧动画

MainActivity:

public class MainActivity extends AppCompatActivity {

    private ImageView mTestImg;
    private ImageView mTestImg2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTestImg = (ImageView) findViewById(R.id.testImg);
        mTestImg2 = (ImageView) findViewById(R.id.testImg2);
    }
    /**
     * 安卓中的基础动画
     * 补间动画Tween
     * 帧动画Frame
     * 布局动画Layout
     * 属性动画Property
     */

    /**
     * 补间动画
     * Alpha 渐变透明度动画
     * Scale 渐变尺寸缩放动画
     * Translate 位置移动动画
     * Rotate 旋转动画
     *
     * Duration 动画持续时间
     * fillAfter 设置为true 动画转换在动画结束后被应用
     * fillBefore 设置为true 动画转换在动画开始前被应用
     * interpolator 动画插入器(加速、减速插入器)
     * repeatCount 动画重复次数
     * repateMode 顺序重复、倒序重复
     * startOffset 动画之间的时间间隔
     */

    /**
     * 实现方式
     * 1.配置文件 /res/anim
     *   alpha
     *   scale
     *   translate
     *   rotate
     * 2.java代码实现
     *   AlphaAnimation
     *   ScaleAnimation
     *   TranslateAnimation
     *   RotateAnimation
     */

    /**
     * 1.java代码创建
     */
    public void testJava(View view) {
        // 创建alpha动画 (from,to)
        AlphaAnimation alpha = new AlphaAnimation(0.1f, 1.0f);
        // 设置动画时间为5秒
        alpha.setDuration(5000);
        // 开始播放动画
        view.startAnimation(alpha);
    }

    /**
     * 2.加载配置文件
     */
    public void testXML(View view) {
        // 从xml中加载动画资源
        Animation scale = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim);
        // 开始播放动画
        view.startAnimation(scale);

    }

    /**
     * 透明度动画
     * fromAlpha  起始透明度
     * toAlpha    结束透明度
     * 0~1 透明度范围
     * duration 时间间隔(ms)
     */
    public void alphaClick(View view) {
        // 从xml中加载透明度动画资源
        Animation alpha = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_anim);
        // 开始播放动画
        mTestImg.startAnimation(alpha);
    }

    /**
     * 缩放动画
     * fromXScale 起始X坐标伸缩尺寸
     * toXScale 结束X坐标伸缩尺寸
     * fromYScale 起始Y坐标伸缩尺寸
     * toYScale 结束Y坐标伸缩尺寸
     * pivotX 伸缩动画相对于X坐标的开始位置
     * pivotY 伸缩动画相对于Y坐标的开始位置伸缩动画
     * duration 时间间隔(ms)
     * fillAfter 设置为true固定结束后的位置
     * interpolator 插速器 如: @android:anim/accelerate_interpolator
     */
    public void scaleClick(View view) {
        // 从xml中加载缩放动画资源
        Animation scale = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim);
        // 开始播放动画
        mTestImg.startAnimation(scale);

    }

    /**
     * 位移动画
     * fromXDelta   相对于view的起始X坐标
     * fromYDelta   相对于view的起始Y坐标
     * toXDelta     相对于view的结束X坐标
     * toYDelta     相对于view的结束Y坐标
     */
    public void translateClick(View view){
        // 从xml中加载位移动画资源
        Animation translate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_anim);
        // 开始播放动画
        mTestImg.startAnimation(translate);
    }


    /**
     * 旋转动画
     * fromDegrees
     * toDegrees
     * pivotX 伸缩动画相对于X坐标的开始位置
     * pivotY 伸缩动画相对于Y坐标的开始位置旋转动画
     */
    public void rotateClick(View view){
        // 从xml中加载旋转动画资源
        Animation rotate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_anim);
        // 开始播放动画
        mTestImg.startAnimation(rotate);
    }

    /**
     * 组合动画XML实现-先旋转后位移
     */
    public void rtClick(View view){

        Animation rt = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_n_translate);
        // 开始播放动画
        mTestImg.startAnimation(rt);
    }

    /**
     * 组合动画-循环闪烁的代码实现
     * setRepeatCount 设置循环次数
     * setRepateMode 设置循环模式 RESTART/REVERSE  正序或倒序
     */
    public void kirakiraClick(View view){

        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setRepeatCount(6);
        // 开始播放动画
        mTestImg.startAnimation(alphaAnimation);
    }

    /**
     * 帧动画
     *  默认一直循环
     */
    public void frameClick(View view){
        mTestImg2.setImageResource(R.drawable.frame_list);

        AnimationDrawable animationDrawable = (AnimationDrawable) mTestImg2.getDrawable();
        // 设置重复显示一次
        animationDrawable.setOneShot(true);

        animationDrawable.start();
    }


    /**
     * Activity的切换动画
     */
    public void atClick(View view){

        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
        // 设置第二个activity的进入动画和第一个activity的退出动画
        overridePendingTransition(R.anim.activity_in_anim,android.R.anim.fade_out);
    }


}

SecondActivity:

public class SecondActivity extends AppCompatActivity {

    private LinearLayout ll;
    private LayoutAnimationController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ll = (LinearLayout) findViewById(R.id.ll);
//        // 创建布局动画控制器
//        controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
//        // 设置布局动画的顺序
//        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
//        // 设置布局动画
//        ll.setLayoutAnimation(controller);
//        // 开始播放动画
//        ll.startLayoutAnimation();
    }
    public void add(View view){
        ImageView img = new ImageView(this);
        img.setImageResource(R.mipmap.ic_launcher);

        // 为view设置动画
        img.setAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        ll.addView(img);
    }
}

activity_in_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--设置activity的进入动画-->
    <translate
        android:fromXDelta="0"
        android:fromYDelta="-100%"
        android:toXDelta="0"
        android:toYDelta="0"

        android:duration="1000"
        />
</set>

alpha_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--透明度动画-->
<alpha
    android:fromAlpha="0.1"
    android:toAlpha="1.0"
    android:duration="3000"
    />
</set>

rotate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--旋转动画-->
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
</set>

rotate_n_translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--组合动画旋转后位移-->
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:startOffset="2000"
        android:toXDelta="500%"
        android:toYDelta="0" />
    <!--startOffset表示延迟开始-->
</set>

scale_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--缩放动画-->
    <scale
        android:duration="3000"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"

        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"

        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

translate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--位移动画-->
    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="500%"

        android:toYDelta="0" />
</set>

这里写图片描述
这里写图片描述
这里写图片描述

属性动画

/**
     * 属性动画
     * translationX
     * translationY
     * rotation
     * rotationX
     * rotationY
     * scaleX
     * scaleY
     * X
     * Y
     * alpha
     */
    public void translateClick(View view) {
        // translationX表示偏移量
        ObjectAnimator.ofFloat(mTestImg, "translationX", 0f, 200f)
                .setDuration(2000)
                .start();
//        // x表示绝对值
//        ObjectAnimator.ofFloat(mTestImg, "X", 0, 200)
//                .setDuration(2000)
//                .start();
//        //旋转
//        ObjectAnimator.ofFloat(mTestImg, "rotation", 0, 360)
//                .setDuration(2000)
//                .start();
//        //透明度
//        ObjectAnimator.ofFloat(mTestImg, "alpha", 1, 0)
//                .setDuration(2000)
//                .start();
    }

    /**
     * 组合属性动画1
     */
    public void zuheClick(View view) {
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0, 200);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("rotation", 0, 360);
        ObjectAnimator.ofPropertyValuesHolder(mTestImg, p1, p2)
                .setDuration(2000)
                .start();
    }

    /**
     * 组合属性动画2
     */
    public void zuheClick2(View view) {
        ObjectAnimator a1 = ObjectAnimator.ofFloat(mTestImg, "translationX", 0, 200)
                .setDuration(2000);
        //旋转
        ObjectAnimator a2 = ObjectAnimator.ofFloat(mTestImg, "rotation", 0, 360)
                .setDuration(2000);
        AnimatorSet animatorSet = new AnimatorSet();
        // 按先后顺序播放
        animatorSet.playSequentially(a1, a2);
        // 同时播放
        //animatorSet.playTogether(a1,a2);
        // 设置播放的顺序
        //animatorSet.play(a2).before(a1);
        animatorSet.setDuration(1000);
        animatorSet.start();

    }

    /**
     * 动画监听事件
     */
    public void jianting(View view) {
        a1 = ObjectAnimator.ofFloat(mTestImg, "translationX", 0, 200)
                .setDuration(2000);

//        a1.addListener(new Animator.AnimatorListener() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//                Log.e("onAnimationStart","动画开始");
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                Log.e("onAnimationEnd","动画结束");
//            }
//
//            @Override
//            public void onAnimationCancel(Animator animation) {
//                Log.e("onAnimationCancel","动画取消");
//            }
//
//            @Override
//            public void onAnimationRepeat(Animator animation) {
//                Log.e("onAnimationRepeat","动画重复");
//            }
//        });

        // 使用适配器监听器可以选择要重写的方法
        a1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Log.e("适配器","动画结束");
            }
        });

        a1.start();

    }

    /**
     * 取消动画
     */
    public void cancelClick(View view){
        a1.cancel();
    }

    /**
     * 数值发生器
     */
    public void shuzhiClick(View view){
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                mTv.setText(value+"");
            }
        });
        valueAnimator.setDuration(5000).start();
    }

这里写图片描述

Activity跳转动画

//设置Activity的跳转动画  第一个参数:指定进入的动画 第二个参数:指定退出的动画
overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值