android项目【新闻】项目实战(一):开发启动图并实现动画效果

来源:互联网 发布:php中while的用法 编辑:程序博客网 时间:2024/05/21 06:42

一:创建android项目,结构如图

这里写图片描述

二:添加启动图的SplashActivity和布局文件

   布局文件代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/splash_root"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/splash_bg_newyear"    tools:context="com.jingtong.hefeinews.SplashActivity">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@drawable/splash_sheep_newyear"/></RelativeLayout>
   SplashActivity代码如下:
public class SplashActivity extends AppCompatActivity {    public static final String START_MAIN = "start_main";    private RelativeLayout splash_root;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_splash);        splash_root = (RelativeLayout) findViewById(R.id.splash_root);        //渐变动画        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);        //设置持续播放时间        alphaAnimation.setDuration(500);        alphaAnimation.setFillAfter(true);        //缩放动画        ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);        scaleAnimation.setDuration(500);        scaleAnimation.setFillAfter(true);        //旋转动画        RotateAnimation rotateAnimation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);        rotateAnimation.setDuration(500);        rotateAnimation.setFillAfter(true);        AnimationSet set = new AnimationSet(false);        set.addAnimation(alphaAnimation);        set.addAnimation(scaleAnimation);        set.addAnimation(rotateAnimation);        set.setDuration(2000);        splash_root.startAnimation(set);        set.setAnimationListener(new MyAnimationListenner());    }    //动画监听    class MyAnimationListenner implements Animation.AnimationListener{        @Override        public void onAnimationStart(Animation animation) {        }        @Override        public void onAnimationEnd(Animation animation) {            boolean isStartMain = CacheUtils.getBoolean(SplashActivity.this,START_MAIN);            if (isStartMain){                //如果进入过主页面,直接进入主页面            }else {                //如果没有进入过主页面,进入向导界面                Intent intent = new Intent(SplashActivity.this,GuideActivity.class);                startActivity(intent);            }            finish();            Toast.makeText(SplashActivity.this, "播放完成", Toast.LENGTH_SHORT).show();        }        @Override        public void onAnimationRepeat(Animation animation) {        }    }}

动态效果图:这里写图片描述

阅读全文
0 0