PageLoadingView.java
3.36 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.wd.common.progress;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import com.airbnb.lottie.LottieAnimationView;
import com.wd.kittools.density.DensityUtils;
import com.wd.foundation.wdkit.constant.Constants;
import com.wd.foundation.wdkit.utils.AnimUtil;
import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkit.utils.UiUtils;
/**
* 第一次 视频详情 请求接口 loading 效果
* @author xujiawei
*/
public class PageLoadingView extends LinearLayout {
/**
* 上下文
*/
private final Context context;
/**
* 延迟0.5秒展示loading,如果0.5秒内结束了,则不展示loading
* */
private boolean needShow;
private Handler handler;
/**
* 动画父布局
*/
private LinearLayout llAnimationView;
/**
* 动画布局
*/
private LottieAnimationView animationView;
/**
* 动画文件
*/
private String interactCode;
public PageLoadingView(Context context) {
this(context, null);
}
public PageLoadingView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PageLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init() {
View view = LayoutInflater.from(context).inflate(R.layout.small_white_black_loading_view, this, true);
llAnimationView = view.findViewById(R.id.llAnimationView);
animationView = view.findViewById(R.id.animation_view);
handler = new Handler(Looper.getMainLooper());
}
/**
* 设置动画文件
* @param interactCode 动画文件
*/
public void setInteractCode(String interactCode) {
this.interactCode = interactCode;
// 如果时人民日报扫光动画,宽高设置为167*60
if ("video_loading.json".equalsIgnoreCase(interactCode)) {
// 动画父布局
UiUtils.setWH(llAnimationView, DensityUtils.INSTANCE.dip2px(context, 167), DensityUtils.INSTANCE.dip2px(context, 100));
// 动画布局
UiUtils.setWH(animationView, DensityUtils.INSTANCE.dip2px(context, 167), DensityUtils.INSTANCE.dip2px(context, 60));
}
}
/**
* loading
*/
public void showLoading() {
//需要展示loading
needShow = true;
//延时展示loading
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(needShow){
needShow = false;
if (TextUtils.isEmpty(interactCode)) {
AnimUtil.showLocalLottieEffects(animationView,"refreshing_common_loading.json", true);
}else {
AnimUtil.showLocalLottieEffects(animationView, interactCode, true);
}
}
}
}, Constants.MIN_DELAY_TIME_SHOWLOADING);
}
public void stopLoading() {
// 已经结束,不需要再展示loading
needShow = false;
animationView.pauseAnimation();
}
}