AdvertisementFragment.java
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.polysoft.nafmii.fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.polysoft.nafmii.R;
import com.polysoft.nafmii.activity.HomeActivity;
import com.polysoft.nafmii.utils.IntentUtils;
public class AdvertisementFragment extends Fragment implements View.OnClickListener {
private ImageView mImgView;
private TextView mCountDown;
private AdvertisementFragment() {
}
public static Fragment getInstance(String data) {
Fragment fragment = new AdvertisementFragment();
Bundle bundle = new Bundle();
bundle.putString("data", data);
fragment.setArguments(bundle);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_advertisement, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.advertisement_pass).setOnClickListener(this);
this.mImgView = view.findViewById(R.id.advertisement_imgview);
this.mCountDown = view.findViewById(R.id.advertisement_countdown);
this.loadAdvertisementImage();
}
@Override
public void onClick(View v) {
this.gotoHome();
}
private void loadAdvertisementImage() {
String json = getArguments().getString("data");
JSONObject item = JSON.parseObject(json);
String id = item.getString("id");
String description = item.getString("description");
String picture = item.getString("picture");
String url = item.getString("url");
Glide.with(getContext()).load(picture).into(new DrawableImageViewTarget(this.mImgView){
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
super.onResourceReady(resource, transition);
startCountDown();
}
});
}
private void startCountDown() {
long unit = 1000, maxSecond = 5;
mCountDown.setText(String.format("%ds", maxSecond));
new CountDownTimer(maxSecond * unit, unit) {
@Override
public void onTick(long millisUntilFinished) {
long second = millisUntilFinished / unit;
Log.d("CountDownTimer", "onTick: " + second);
mCountDown.setText(String.format("%ds", second));
}
@Override
public void onFinish() {
gotoHome();
}
}.start();
}
private synchronized void gotoHome() {
if (null != getActivity() && !getActivity().isFinishing()) {
IntentUtils.startActivity(getActivity(), HomeActivity.class);
}
}
}