舒心、快乐,比什么都重要

Android实现帧动画,以及出场时的动画

最近有个小需求,在数据上传的时候加一个上传的动画,然后就寻思着自己写一个帧动画

上传开始的时候调用动画,上传结束通知容器将其删除(这个方法应该不会太耗内存),然后吐槽下gif图片还是我自己一帧一帧从ps上取出来然后另存为png格式的,哎,奖励自己一下竟然这么全能

写帧动画之前需要写一个帧list,命名为upload_anim.xml

<?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/s1"
        android:duration="50" />
    <item
        android:drawable="@drawable/s2"
        android:duration="50" />
    <item
        android:drawable="@drawable/s3"
        android:duration="50" />
    <item
        android:drawable="@drawable/s4"
        android:duration="50" />
    <item
        android:drawable="@drawable/s5"
        android:duration="50" />
    <item
        android:drawable="@drawable/s6"
        android:duration="50" />
    <item
        android:drawable="@drawable/s7"
        android:duration="50" />

    <item
        android:drawable="@drawable/s8"
        android:duration="50" />
    <item
        android:drawable="@drawable/s9"
        android:duration="50" />
    <item
        android:drawable="@drawable/s10"
        android:duration="50" />
    <item
        android:drawable="@drawable/s11"
        android:duration="50" />
    <item
        android:drawable="@drawable/s12"
        android:duration="50" />
</animation-list>

比如这个有12帧的动画资源文件,我们来看看他怎么使用,这里要讲下ImageVIew是可以播放如以上代码所示的帧list的

使用时我只需将以上的帧list当做Drawbale放在ImageView的资源来用即可,然后我们在获取到这个资源使其播放起来!就可以实现了,然后再加上ImageVIew的入场动画!完美

               ImageView imageView = new ImageView(context);

                animationIn = AnimationUtils.loadAnimation(context,R.anim.in);

                imageView.setAnimation(animationIn);

                imageView.setImageResource(R.drawable.upload_anim);

                final AnimationDrawable animationDrawable = (AnimationDrawable)  imageView.getDrawable();
                animationDrawable.start();
                     ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        50,50);

                rlContentMain.addView(imageView,params);    

注:上面有未声明的变量animationIn是Animation类型;rlContentMain是RelativeLayout类型为了放置动画的一个layout(我将这个动画放置在relativeLayout里然后加上动画,就有了点击然后动画的效果(*^__^*) ,设置params是为了控制动画资源的大小)

关键代码分析:使动画资源播放起来的关键代码就是标红的代码了,先定义一个AnimationDrawable然后其实例从imageVIew中get到,之后直接start()就可以播放起来

然后我还有R.anim.in这个是一个imageView进入时的动画,代码如下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="-100%p"
        android:duration="600"
        />
</set>

实现的是简单的位移动画,fromYdelta是  源码解释:Change in Y coordinate to apply at the start of the animation即为 应用动画开头Y 坐标变化;

这样就完成了

当然还有其他的动画比如渐变,加速等等

 

posted @ 2016-11-24 16:57  Arcturis  阅读(1638)  评论(0编辑  收藏  举报