forked from crosg/FloatMenuSample
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MainActivity.java
306 lines (249 loc) · 12.4 KB
/
MainActivity.java
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (c) 2016, Shanghai YUEWEN Information Technology Co., Ltd.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Shanghai YUEWEN Information Technology Co., Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY SHANGHAI YUEWEN INFORMATION TECHNOLOGY CO., LTD. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package com.yw.game.floatmenu.demo;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.yw.game.floatmenu.FloatItem;
import com.yw.game.floatmenu.FloatLogoMenu;
import com.yw.game.floatmenu.FloatMenuView;
import com.yw.game.floatmenu.customfloat.FloatManager;
import java.util.ArrayList;
public class MainActivity extends Activity {
FloatLogoMenu mFloatMenu;
FloatLogoMenu mFloatMenu1;
ArrayList<FloatItem> itemList = new ArrayList<>();
private Activity mActivity;
String HOME = "首页";
String FEEDBACK = "客服";
String MESSAGE = "消息";
String[] MENU_ITEMS = {HOME, FEEDBACK, MESSAGE};
private int[] menuIcons = new int[]{R.drawable.yw_menu_account, R.drawable.yw_menu_fb, R.drawable.yw_menu_msg};
FloatManager floatManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
for (int i = 0; i < menuIcons.length; i++) {
itemList.add(new FloatItem(MENU_ITEMS[i], 0x99000000, 0x99000000, BitmapFactory.decodeResource(this.getResources(), menuIcons[i]), String.valueOf(i + 1)));
}
}
@Override
protected void onResume() {
super.onResume();
if (mFloatMenu == null) {
mFloatMenu = new FloatLogoMenu.Builder()
.withActivity(mActivity)
// .withContext(mActivity.getApplication())//这个在7.0(包括7.0)以上以及大部分7.0以下的国产手机上需要用户授权,需要搭配<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
.logo(BitmapFactory.decodeResource(getResources(), R.drawable.yw_game_logo))
.drawCicleMenuBg(true)
.backMenuColor(0xffe4e3e1)
.setBgDrawable(this.getResources().getDrawable(R.drawable.yw_game_float_menu_bg))
//这个背景色需要和logo的背景色一致
.setFloatItems(itemList)
.defaultLocation(FloatLogoMenu.RIGHT)
.drawRedPointNum(false)
.showWithListener(new FloatMenuView.OnMenuClickListener() {
@Override
public void onItemClick(int position, String title) {
Toast.makeText(MainActivity.this, "position " + position + " title:" + title + " is clicked.", Toast.LENGTH_SHORT).show();
}
@Override
public void dismiss() {
}
});
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
refreshDot();
}
}, 5000);
//同时只能new一个
}
if (floatManager != null) return;
floatManager = new FloatManager(this, new FloatManager.GetViewCallback() {
@Override
public View getLeftView(View.OnTouchListener touchListener) {
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
TextView textView = new TextView(mActivity);
textView.setText("左边");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mActivity, "左边的菜单被点击了", Toast.LENGTH_SHORT).show();
}
});
ImageView imageView = new ImageView(mActivity);
imageView.setLayoutParams(new LinearLayout.LayoutParams(dip2px(50), dip2px(50)));
imageView.setImageResource(R.drawable.yw_game_logo);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setOnTouchListener(touchListener);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(dip2px(50), dip2px(50)));
linearLayout.setBackgroundResource(R.drawable.yw_game_float_menu_bg);
linearLayout.addView(imageView);
linearLayout.addView(textView);
return linearLayout;
}
@Override
public View getRightView(View.OnTouchListener touchListener) {
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
TextView textView = new TextView(mActivity);
textView.setText("右边");
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mActivity, "右边的菜单被点击了", Toast.LENGTH_SHORT).show();
}
});
ImageView imageView = new ImageView(mActivity);
imageView.setLayoutParams(new LinearLayout.LayoutParams(dip2px(50), dip2px(50)));
imageView.setImageResource(R.drawable.yw_game_logo);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setOnTouchListener(touchListener);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(dip2px(50), dip2px(50)));
linearLayout.setBackgroundResource(R.drawable.yw_game_float_menu_bg);
linearLayout.addView(textView);
linearLayout.addView(imageView);
return linearLayout;
}
@Override
public View getLogoView() {
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(mActivity);
imageView.setLayoutParams(new LinearLayout.LayoutParams(dip2px(50), dip2px(50)));
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.yw_game_logo);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(dip2px(50), dip2px(50)));
linearLayout.setBackgroundResource(R.drawable.yw_game_float_menu_bg);
linearLayout.addView(imageView);
return linearLayout;
}
@Override
public void resetLogoViewSize(int hintLocation, View logoView) {
logoView.clearAnimation();
logoView.setTranslationX(0);
logoView.setScaleX(1);
logoView.setScaleY(1);
}
@Override
public void dragingLogoViewOffset(final View smallView, boolean isDraging, boolean isResetPosition, float offset) {
if (isDraging && offset > 0) {
smallView.setBackgroundDrawable(null);
smallView.setScaleX(1 + offset);
smallView.setScaleY(1 + offset);
} else {
smallView.setBackgroundResource(R.drawable.yw_game_float_menu_bg);
smallView.setTranslationX(0);
smallView.setScaleX(1);
smallView.setScaleY(1);
}
if (isResetPosition) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
smallView.setRotation(offset * 360);
}
} else {
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360 * 4);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int v = (int) animation.getAnimatedValue();
smallView.setRotation(v);
}
});
valueAnimator.setDuration(800);
valueAnimator.start();
}
}
@Override
public void shrinkLeftLogoView(View smallView) {
smallView.setTranslationX(-smallView.getWidth() / 3);
}
@Override
public void shrinkRightLogoView(View smallView) {
smallView.setTranslationX(smallView.getWidth() / 3);
}
@Override
public void leftViewOpened(View leftView) {
Toast.makeText(mActivity, "左边的菜单被打开了", Toast.LENGTH_SHORT).show();
}
@Override
public void rightViewOpened(View rightView) {
Toast.makeText(mActivity, "右边的菜单被打开了", Toast.LENGTH_SHORT).show();
}
@Override
public void leftOrRightViewClosed(View smallView) {
Toast.makeText(mActivity, "菜单被关闭了", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestoryed() {
}
});
floatManager.show();
}
private int dip2px(float dipValue) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
public void refreshDot() {
for (FloatItem menuItem : itemList) {
if (TextUtils.equals(menuItem.getTitle(), "我的")) {
menuItem.dotNum = String.valueOf(8);
}
}
mFloatMenu.setFloatItemList(itemList);
}
@Override
protected void onPause() {
super.onPause();
hideFloat();
}
@Override
protected void onDestroy() {
destroyFloat();
super.onDestroy();
if (floatManager != null) floatManager.destoryFloat();
}
public void hideFloat() {
if (mFloatMenu != null) {
mFloatMenu.hide();
}
}
public void destroyFloat() {
if (mFloatMenu != null) {
mFloatMenu.destoryFloat();
}
mFloatMenu = null;
mActivity = null;
}
}