美文网首页
Android闪屏页(具有倒计时功能)

Android闪屏页(具有倒计时功能)

作者: mcp1993 | 来源:发表于2017-04-25 12:51 被阅读343次

现在APP的闪屏页基本上都是一张图片,一个倒计时的TextView或者Buttton。
效果图:


Screenshot_20170425-103329.png

具体实现:
1.SplashActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv_picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>
    <TextView
        android:id="@+id/tv_jump"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="30dp"
        android:layout_marginTop="40dp"
        android:background="@drawable/shape_jump"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="16sp"
        android:visibility="gone"/>
    <ImageView
        android:id="@+id/iv_default"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

2.SplashActivity,看代码:

public class SplashActivity extends AppCompatActivity{
    @BindView(R.id.iv_picture)
    ImageView iv_picture;
    @BindView(R.id.iv_default)
    ImageView iv_default;
    @BindView(R.id.tv_jump)
    TextView tv_jump;
    private String imgUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493095641000&di=a7a9b52b6d531a8a0a4f25fff85b2471&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01e41b5788a3fd0000018c1bb8d896.png";
    private static SplashActivity splashActivity;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        splashActivity = this;
//        hideStatusBar();
        ButterKnife.bind(this);
        iv_default.setBackgroundResource(R.mipmap.p_login_bg);
        Glide.with(SplashActivity.this).load(imgUrl)
                .centerCrop()
                .placeholder(R.mipmap.p_login_bg)
                .error(R.mipmap.p_login_bg)
                .into(iv_picture);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                iv_default.setVisibility(View.GONE);
                tv_jump.setVisibility(View.VISIBLE);
                tv_jump.setText("跳转 3");
                Thread t = new Thread(new JumpThread(tv_jump,3,splashActivity));
                t.start();
            }
        }, 2000);
    }
}

3.最重要的JumpThread,实现倒计时功能,跳转界面,代码:

public class JumpThread implements Runnable {
    private TextView textView;
    private int numberTime ;
    private Activity activity;

    public JumpThread(TextView textView, int numberTime, Activity activity) {
        this.textView = textView;
        this.numberTime = numberTime;
        this.activity = activity;
    }

    Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
      //String.valueOf(numberTime) 跳转后面空格显示,直接写numberTime空格不显示。
            textView.setText("跳转   "+String.valueOf(numberTime));
            if (numberTime==0) {
                jumpActivity();
            }
        }
    };
    private void jumpActivity(){
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        //activity切换动画
        activity.overridePendingTransition(R.anim.screen_zoom_in, R.anim.screen_zoom_out);

        activity.finish();
    }
    @Override
    public void run() {
        try {
            while (numberTime>0) {
                Thread.sleep(1000);
                numberTime--;
                handler.sendMessage(new Message());
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

项目代码:
an example

相关文章

网友评论

      本文标题:Android闪屏页(具有倒计时功能)

      本文链接:https://www.haomeiwen.com/subject/jfomzttx.html