android 动画效果

最近项目需要用到帧动画,简单总结下:
效果图:
这里写图片描述
使用方法1:
在android/drawable中创建animlist

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"  >
    <item android:drawable="@drawable/image1"
        android:duration="400"/>
    <item android:drawable="@drawable/image2"
        android:duration="400"/>
    <item android:drawable="@drawable/image2"
        android:duration="400"/>
    <item android:drawable="@drawable/image3"
        android:duration="400"/>
    <item android:drawable="@drawable/image4"
        android:duration="400"/>
    <item android:drawable="@drawable/image5"
        android:duration="400"/>
</animation-list>

布局中给个image控件就不写了
主要是想在activity启动时动画就开始,那么就需要在activity中重写onWindowFocusChanged方法

   @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //给图片添加动画背景
        imageView.setImageResource(R.drawable.animlist);
        //获取动画可以方便控制停止
        frameAnim = (AnimationDrawable) imageView.getDrawable();
    }

使用方法2:
在activity中动态添加图片

 private void initFrameAnim() {
        frameAnim = new AnimationDrawable();
        // 为AnimationDrawable添加动画帧
        frameAnim.addFrame(getResources().getDrawable(R.drawable.image1), 100);
        frameAnim.addFrame(getResources().getDrawable(R.drawable.image2), 100);
        frameAnim.addFrame(getResources().getDrawable(R.drawable.image3), 100);
        frameAnim.addFrame(getResources().getDrawable(R.drawable.image4), 100);
        frameAnim.addFrame(getResources().getDrawable(R.drawable.image5), 100);
        // 设置ImageView的背景为AnimationDrawable
        imageView.setBackgroundDrawable(frameAnim);
    }

控制动画停止或开始

 /**
     * 开始播放
     */
    protected void start() {
        if (frameAnim != null && !frameAnim.isRunning()) {
            frameAnim.start();
        }
    }

    /**
     * 停止播放
     */
    protected void stop() {
        if (frameAnim != null && frameAnim.isRunning()) {
            frameAnim.stop();
        }
    }

扩展:
如果想要帧动画和补间动画一起用,须在android/anim文件夹下添加补间动画。
例:
位移动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromXDelta="100"
        android:fromYDelta="100"
        android:toXDelta="200"
        android:toYDelta="0"
        />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromXDelta="200"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0"
        />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100"
        android:toYDelta="100"
        />
</set>

透明度动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <alpha
        android:fromAlpha="0.2"
        android:toAlpha="1.0"
        android:duration="200"
        />
</set>

然后使用AnimationUtils类

 final Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animtion);
        final Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animtion1);
        final Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animtion2);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                imageView.startAnimation(animation1);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                imageView.startAnimation(animation2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animation2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation anim) {
                imageView.startAnimation(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        imageView.startAnimation(animation);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值