一般APP的Welcome动画、动画循环(补间动画)

image

作用:一般进入APP都会播放一个小动画,可以在播放动画的同时处理数据的加载
步骤:
1、设置好动画布局,给布局一个ID,方便给此布局设置动画
2、主程序实现动画和数据加载
1)设置动画效果:选择动画类型、设置动画时间,重复次数····
2)为要播放动画的布局或控件绑定动画
3)设置动画监听
代码如下:

1、布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.leixiansheng.test.MainActivity"
    android:background="@drawable/logo2">

    <TextView
        android:id="@+id/tv"
        android:textSize="20dp"
        android:textColor="#ffffffff"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="welcome to" />

    <ProgressBar
        android:id="@+id/pb"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

2、功能实现

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        /**
         * TranslateAnimation、AlphaAnimation、ScaleAnimation......so on
         */
        //创建渐变动画
        AlphaAnimation animation = new AlphaAnimation(0.1f, 1f);
        //设置动画时间
        animation.setDuration(5000);
        //动画重复次数  1次(共播放2)
        animation.setRepeatCount(1);
        //为界面 添加动画效果
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
        rl.setAnimation(animation);

        //设置动画监听
        animation.setAnimationListener(new Animation.AnimationListener() {
            //动画开始回调
            @Override
            public void onAnimationStart(Animation animation) {
                //可以在此处加载数据
                Log.i("MMM", "onAnimationStart");
            }

            //动画结束回调
            @Override
            public void onAnimationEnd(Animation animation) {
                TextView textView = (TextView) findViewById(R.id.tv);
                ProgressBar pb = (ProgressBar) findViewById(R.id.pb);
                textView.setText("动画播放完毕");
                pb.setVisibility(View.GONE);
                Log.i("MMM", "onAnimationEnd");
            }
            //动画重复回调
            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("MMM", "onAnimationRepeat");
            }
        });
    }
}

拓展:动画的循环播放

1、可以用 animation.setRepeatCount(); 实现循环(此方法要循环完后才会执行 onAnimationEnd 监听回调)
2、通过动画监听实现循环
代码如下:

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener{
    ImageView imageView;
    LinearLayout li;
    AlphaAnimation animation;

    boolean isFinsh = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.image_view);
        li = (LinearLayout) findViewById(R.id.activity_main);

        animation = new AlphaAnimation(0f, 1f);
        animation.setDuration(3000);
        imageView.setAnimation(animation);
        animation.setAnimationListener(this);
    }


    @Override
    public void onAnimationStart(Animation animation) {

    }

    /**
     * 根据第一张动画是否播放完毕来实现两个动画产生循环交替
     * @param animation
     */
    @Override
    public void onAnimationEnd(Animation animation) {
        if (isFinsh) {
            animation = new AlphaAnimation(1f, 0f);
            animation.setDuration(5000);
            imageView.setAnimation(animation);
            animation.setAnimationListener(this);
            isFinsh = !isFinsh;
        } else {
            animation = new AlphaAnimation(0f, 1f);
            animation.setDuration(5000);
            imageView.setAnimation(animation);
            animation.setAnimationListener(this);
            isFinsh = !isFinsh;
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}

赠人玫瑰,手有余香。您的支持是我创作最大的动力!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容