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

    xiaoxiao2021-04-17  45

    转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/70170771

    作用:一般进入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、功能实现

    package com.example.leixiansheng.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; 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、通过动画监听实现循环

    package com.example.leixiansheng.test; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; 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) { } }

    转载请注明原文地址: https://ju.6miu.com/read-673657.html

    最新回复(0)