天天看点

android帧动画,移动位置,缩放,改变透明度等动画讲解

1.苦逼的需求又来了,需要实现一些动画效果,第一个想到的是播放gif图片,但是这样会占包的资源,并且清晰度不高,于是想着程序实现,自己用帧动画+缩放+移动+透明度 实现了一些想要的效果,这里跟大家分享一下

2.效果图如下:

android帧动画,移动位置,缩放,改变透明度等动画讲解

3.帧动画实现代码

    1).首先获取每帧显示的imageview控件,然后把所有帧放到animationdrawable对象里面去,开启动画,通过handle延时2秒关闭动画

imageview ivframe = (imageview) findviewbyid(r.id.iv_frame);  

// 通过逐帧动画的资源文件获得animationdrawable示例  

final animationdrawable frameanim = (animationdrawable) getresources().getdrawable(r.drawable.thank_you_boss_anim);  

ivframe.setbackgrounddrawable(frameanim);// 把animationdrawable设置为imageview的背景  

frameanim.start();  

new handler().postdelayed(new runnable() {  

    @override  

    public void run(){  

        if (frameanim != null && frameanim.isrunning()) {//2秒之后结束动画  

            frameanim.stop();  

        }  

    }  

},2000);  

  2).thank_you_boss_anim.xml  所有帧的布局文件   设置每一帧的图片,每帧时间

<?xml version="1.0" encoding="utf-8"?>  

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  

    android:oneshot="false" >  

    <!-- 定义一个动画帧,drawable为img0,持续时间300毫秒 -->  

    <item  android:drawable="@drawable/icon_think_boss_one"   android:duration="100"/>  

    <item android:drawable="@drawable/icon_think_boss_two"  android:duration="100"/>  

    <item android:drawable="@drawable/icon_think_boss_three"  android:duration="100"/>  

    <item  android:drawable="@drawable/icon_think_boss_four"  android:duration="300"/>  

</animation-list>  

4.帧动画加上移动  放大  透明度效果   

   开启一个dialog,背景设置透明,得到帧动画跟文字图片显示imageview,然后开启两个动画。

/** 

 * 谢谢土豪打赏 

 */  

private void thanktuhaoreward(){  

    view view=layoutinflater.from(this).inflate(r.layout.activity_thank_tuhao_reward,null);  

    final dialog tyrantdialog = new dialog(this,r.style.selectordialog);  

    tyrantdialog.findviewbyid(r.id.iv_frame);  

    tyrantdialog.setcontentview(view);   

    tyrantdialog.setcanceledontouchoutside(true);  

    imageview ivframe=(imageview) tyrantdialog.findviewbyid(r.id.iv_frame);  

    // 通过逐帧动画的资源文件获得animationdrawable示例  

    final animationdrawable frameanim=(animationdrawable) getresources().getdrawable(r.drawable.thank_you_tyrant_anim);  

    ivframe.setbackgrounddrawable(frameanim);//把animationdrawable设置为imageview的背景  

    frameanim.start();  

    imageview playthinkonetext=(imageview) tyrantdialog.findviewbyid(r.id.play_think_one_text);  

    animation anim = animationutils.loadanimation(this,r.anim.translate_thank_you_tyrant);  

    playthinkonetext.startanimation(anim);  

    anim.setfillafter(true);  

    tyrantdialog.show();  

    view.setonclicklistener(new onclicklistener() {  

        @override  

        public void onclick(view arg0) {  

            stop(frameanim);  

            tyrantdialog.cancel();  

    });  

    anim.setanimationlistener(new animationlistener() {  

        public void onanimationstart(animation animation) {}  

        public void onanimationrepeat(animation animation) {}  

        public void onanimationend(animation animation){  

}  

 * 停止播放 

protected void stop(animationdrawable frameanim) {  

    if (frameanim != null && frameanim.isrunning()) {  

        frameanim.stop();  

帧动画的布局文件我就不贴出来了,贴一下移动,缩放,改变透明度的布局文件    translate_thank_you_tyrant.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >  

    <translate  

        android:duration="1000"  

        android:fromxdelta="30"  

        android:fromydelta="30"  

        android:toxdelta="30"  

        android:toydelta="-120" />  

    <alpha  

        android:duration="2000"  

        android:fromalpha="0.4"  

        android:toalpha="1.0" />  

    <scale  

        android:fillafter="false"  

        android:fromxscale="0.4"  

        android:fromyscale="0.4"  

        android:interpolator="@android:anim/accelerate_decelerate_interpolator"  

        android:pivotx="50%"  

        android:pivoty="50%"  

        android:toxscale="1.0"  

        android:toyscale="1.0" />  

    <!--  

     translate 位置转移动画效果  

        整型值:  

        fromxdelta 属性为动画起始时 x坐标上的位置      

        toxdelta   属性为动画结束时 x坐标上的位置  

        fromydelta 属性为动画起始时 y坐标上的位置  

        toydelta   属性为动画结束时 y坐标上的位置  

        注意:没有指定fromxtype toxtype fromytype toytype 时候,默认是以自己为相对参照物   

        长整型值:  

        duration  属性为动画持续时间  

        说明:      时间以毫秒为单位  

    -->  

</set>  

<a target="_blank" href="http://download.csdn.net/detail/lowprofile_coding/9023781">点击下载源码</a>

继续阅读