MyApp.java
6.35 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.cnlive.strike.xiaojiaspeaker;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cnlive.libs.base.application.AppConfig;
import com.cnlive.strike.speaker.BuildConfig;
import com.cnlive.strike.xiaojiaspeaker.util.utilcode.util.ScreenUtils;
import static com.cnlive.strike.xiaojiaspeaker.util.utilcode.util.ConvertUtils.dp2px;
public class MyApp extends Application implements Application.ActivityLifecycleCallbacks {
/*当前对象的静态实例*/
private static MyApp instance;
/*当前显示的Activity*/
private Activity activity;
@Override
public void onCreate() {
super.onCreate();
MyApp.instance = this;
this.registerActivityLifecycleCallbacks(this);
initARouter();
String userId = "";
AppConfig.init(this, BuildConfig.APP_ID, BuildConfig.APP_KEY,
BuildConfig.APP_SCERET, BuildConfig.SPECIAL_GROUP_MEMBER_LIMIT,
BuildConfig.VERSION_AS_DEBUG, "", "",
"10514333", "",
"", "", "",
"", "", "",
BuildConfig.BAIDU_MAP_APP_KEY, "");
}
/**
* 初始化阿里的路由
*/
private void initARouter() {
if (BuildConfig.DEBUG) { // 这两行必须写在init之前,否则这些配置在init过程中将无效
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
ARouter.printStackTrace(); // 打印日志的时候打印线程堆栈
}
ARouter.init(this); // 尽可能早,推荐在Application中初始化
}
/**
* 获取Application对象
*
* @return 返回一个App对象实例
* @see MyApp
*/
public static MyApp instance() {
return MyApp.instance;
}
/**
* 显示View
*
* @param view 需要显示到Activity的视图
*/
public void showView(View view) {
/*Activity不为空并且没有被释放掉*/
if (this.activity != null && !this.activity.isFinishing()) {
/*获取Activity顶层视图,并添加自定义View*/
((ViewGroup) this.activity.getWindow().getDecorView()).addView(view);
}
}
/**
* 隐藏View
*
* @param view 需要从Activity中移除的视图
*/
public void hideView(View view) {
/*Activity不为空并且没有被释放掉*/
if (this.activity != null && !this.activity.isFinishing() && null != view) {
/*获取Activity顶层视图*/
ViewGroup root = ((ViewGroup) this.activity.getWindow().getDecorView());
if (null == root) {
return;
}
removeView(view, root);
}
}
private void removeView(View view, ViewGroup root) {
ObjectAnimator bottomToTop = ObjectAnimator.ofFloat(view, "translationY", ScreenUtils.getScreenHeight() - dp2px(80), ScreenUtils.getScreenHeight()).setDuration(500);
/*初始化动画组合器*/
AnimatorSet animator = new AnimatorSet();
animator.play(bottomToTop);
/*添加动画结束回调*/
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
/*如果Activity中存在View对象则删除*/
if (root.indexOfChild(view) != -1) {
/*从顶层视图中删除*/
root.removeView(view);
}
}
});
/*启动动画*/
animator.start();
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
/*获取当前显示的Activity*/
this.activity = activity;
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
public void createAndStart() {
/*创建提示消息View*/
View view = LayoutInflater.from(this).inflate(R.layout.view_top_msg, null);
if (null == view) {
return;
}
LinearLayout ll_ignore = view.findViewById(R.id.ll_ignore);
LinearLayout ll_lookUp = view.findViewById(R.id.ll_lookUp);
ll_ignore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != view) {
instance().hideView(view);
}
}
});
/*创建属性动画,从下到上*/
ObjectAnimator bottomToTop = ObjectAnimator.ofFloat(view, "translationY", ScreenUtils.getScreenHeight(), ScreenUtils.getScreenHeight() - dp2px(80)).setDuration(500);
/*初始化动画组合器*/
AnimatorSet animator = new AnimatorSet();
/*组合动画*/
animator.play(bottomToTop);
/*添加动画结束回调*/
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/*删除View*/
instance().hideView(view);
}
}, 5000);
}
});
/*添加View到当前显示的Activity*/
instance().showView(view);
/*启动动画*/
animator.start();
}
}