FloatLogoMenu.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright (c) 2016, Shanghai YUEWEN Information Technology Co., Ltd.
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. *
  6. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. * 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.
  8. * 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.
  9. *
  10. * 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.
  11. *
  12. */
  13. package com.yyrh.ui.floatmenu;
  14. import android.animation.Animator;
  15. import android.animation.ValueAnimator;
  16. import android.app.Activity;
  17. import android.content.Context;
  18. import android.content.DialogInterface;
  19. import android.content.SharedPreferences;
  20. import android.graphics.Bitmap;
  21. import android.graphics.Color;
  22. import android.graphics.PixelFormat;
  23. import android.graphics.drawable.Drawable;
  24. import android.os.Build;
  25. import android.os.CountDownTimer;
  26. import android.os.Handler;
  27. import android.os.Looper;
  28. import android.text.TextUtils;
  29. import android.util.DisplayMetrics;
  30. import android.util.TypedValue;
  31. import android.view.Gravity;
  32. import android.view.LayoutInflater;
  33. import android.view.MotionEvent;
  34. import android.view.View;
  35. import android.view.View.OnClickListener;
  36. import android.view.View.OnTouchListener;
  37. import android.view.ViewGroup;
  38. import android.view.WindowManager;
  39. import android.view.animation.Interpolator;
  40. import android.view.animation.LinearInterpolator;
  41. import android.widget.LinearLayout;
  42. import android.widget.TextView;
  43. import com.yyrh.ui.dialog.CustomExitGameDialog;
  44. import com.yyrh.utils.ResourceUtil;
  45. import com.yyrh.utils.TosUtil;
  46. import com.yyrh.utils.YYLog;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. /**
  50. * Created by wengyiming on 2017/7/20.
  51. */
  52. public class FloatLogoMenu {
  53. /**
  54. * 记录 logo 停放的位置,以备下次恢复
  55. */
  56. private static final String LOCATION_X = "hintLocation";
  57. private static final String LOCATION_Y = "locationY";
  58. /**
  59. * 悬浮球 坐落 左 右 标记
  60. */
  61. public static final int LEFT = 0;
  62. public static final int RIGHT = 1;
  63. /**
  64. * 记录系统状态栏的高度
  65. */
  66. private int mStatusBarHeight;
  67. /**
  68. * 记录当前手指位置在屏幕上的横坐标值
  69. */
  70. private float mXInScreen;
  71. /**
  72. * 记录当前手指位置在屏幕上的纵坐标值
  73. */
  74. private float mYInScreen;
  75. /**
  76. * 记录手指按下时在屏幕上的横坐标的值
  77. */
  78. private float mXDownInScreen;
  79. /**
  80. * 记录手指按下时在屏幕上的纵坐标的值
  81. */
  82. private float mYDownInScreen;
  83. /**
  84. * 记录手指按下时在小悬浮窗的View上的横坐标的值
  85. */
  86. private float mXInView;
  87. /**
  88. * 记录手指按下时在小悬浮窗的View上的纵坐标的值
  89. */
  90. private float mYinview;
  91. /**
  92. * 记录屏幕的宽度
  93. */
  94. private int mScreenWidth,screenHeigth;
  95. /**
  96. * 来自 activity 的 wManager
  97. */
  98. private WindowManager wManager;
  99. /**
  100. * 为 wManager 设置 LayoutParams
  101. */
  102. private WindowManager.LayoutParams wmParams,wmParams2;
  103. /**
  104. * 带透明度动画、旋转、放大的悬浮球
  105. */
  106. private DotImageView mFloatLogo;
  107. /**
  108. * 用于 定时 隐藏 logo的定时器
  109. */
  110. private CountDownTimer mHideTimer;
  111. /**
  112. * float menu的高度
  113. */
  114. private Handler mHandler = new Handler(Looper.getMainLooper());
  115. /**
  116. * 悬浮窗左右移动到默认位置 动画的 加速器
  117. */
  118. private Interpolator mLinearInterpolator = new LinearInterpolator();
  119. /**
  120. * 标记是否拖动中
  121. */
  122. private boolean isDraging = false;
  123. /**
  124. * 用于恢复悬浮球的location的属性动画值
  125. */
  126. private int mResetLocationValue;
  127. private LinearLayout ll_hide;
  128. /**
  129. * 手指离开屏幕后 用于恢复 悬浮球的 logo 的左右位置
  130. */
  131. private Runnable updatePositionRunnable = new Runnable() {
  132. @Override
  133. public void run() {
  134. isDraging = true;
  135. checkPosition();
  136. }
  137. };
  138. /**
  139. * 这个事件用于处理移动、自定义点击或者其它事情,return true可以保证onclick事件失效
  140. */
  141. private OnTouchListener touchListener = new OnTouchListener() {
  142. @Override
  143. public boolean onTouch(View v, MotionEvent event) {
  144. switch (event.getAction()) {
  145. case MotionEvent.ACTION_DOWN:
  146. floatEventDown(event);
  147. break;
  148. case MotionEvent.ACTION_MOVE:
  149. floatEventMove(event);
  150. break;
  151. case MotionEvent.ACTION_UP:
  152. floatEventUp();
  153. case MotionEvent.ACTION_CANCEL:
  154. floatEventCancel();
  155. break;
  156. }
  157. return true;
  158. }
  159. };
  160. private View createHideView(Context context){
  161. hideView = LayoutInflater.from(context).inflate(ResourceUtil.getLayoutId(context,
  162. "qj_hide_float_view"), null);
  163. ll_hide = hideView.findViewById(ResourceUtil.getId(context,
  164. "ll_hide"));
  165. return hideView;
  166. }
  167. /**
  168. * R.drawable.game_logo
  169. *
  170. * @param floatItems
  171. */
  172. private Bitmap mLogoRes;
  173. /**
  174. * 用于显示在 mActivity 上的 mActivity
  175. */
  176. private Context mActivity;
  177. /**
  178. * 停靠默认位置
  179. */
  180. private int mDefaultLocation = RIGHT;
  181. /**
  182. * 悬浮窗 坐落 位置
  183. */
  184. private int mHintLocation = mDefaultLocation;
  185. private ValueAnimator valueAnimator;
  186. private View hideView;
  187. private OnFloatClickListener mListener;
  188. private FloatLogoMenu(Builder builder) {
  189. mLogoRes = builder.mLogoRes;
  190. mActivity = builder.mActivity;
  191. mDefaultLocation = builder.mDefaultLocation;
  192. mListener = builder.listener;
  193. // if (mActivity == null || mActivity.isFinishing() || mActivity.getWindowManager() == null) {
  194. // throw new IllegalArgumentException("Activity = null, or Activity is isFinishing ,or this Activity`s token is bad");
  195. // }
  196. if (mLogoRes == null) {
  197. throw new IllegalArgumentException("No logo found,you can setLogo/showWithLogo to set a FloatLogo ");
  198. }
  199. initFloatWindow();
  200. initTimer();
  201. initFloat();
  202. }
  203. /**
  204. * 初始化悬浮球 window
  205. */
  206. private void initFloatWindow() {
  207. wmParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT,
  208. WindowManager.LayoutParams.WRAP_CONTENT,
  209. WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
  210. WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
  211. | WindowManager.LayoutParams.FLAG_FULLSCREEN,
  212. PixelFormat.TRANSLUCENT);
  213. if (mActivity instanceof Activity) {
  214. Activity activity = (Activity) mActivity;
  215. wManager = activity.getWindowManager();
  216. //类似dialog,寄托在activity的windows上,activity关闭时需要关闭当前float
  217. wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
  218. } else {
  219. wManager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
  220. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  221. if (Build.VERSION.SDK_INT > 23) {
  222. //在android7.1以上系统需要使用TYPE_PHONE类型 配合运行时权限
  223. wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
  224. } else {
  225. wmParams.type =WindowManager.LayoutParams.TYPE_TOAST;
  226. }
  227. } else {
  228. wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
  229. }
  230. }
  231. DisplayMetrics dm = new DisplayMetrics();
  232. wManager.getDefaultDisplay().getMetrics(dm);
  233. mScreenWidth = dm.widthPixels;
  234. screenHeigth = dm.heightPixels;
  235. //判断状态栏是否显示 如果不显示则statusBarHeight为0
  236. mStatusBarHeight = dp2Px(0, mActivity);
  237. wmParams.format = PixelFormat.RGBA_8888;
  238. wmParams.gravity = Gravity.LEFT | Gravity.TOP;
  239. wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
  240. | WindowManager.LayoutParams.FLAG_FULLSCREEN;
  241. mHintLocation = getSetting(LOCATION_X, mDefaultLocation);
  242. int defaultY = ((screenHeigth - mStatusBarHeight) / 2) / 3;
  243. int y = getSetting(LOCATION_Y, defaultY);
  244. if (mHintLocation == LEFT) {
  245. wmParams.x = 0;
  246. } else {
  247. wmParams.x = mScreenWidth;
  248. }
  249. if (y != 0 && y != defaultY) {
  250. wmParams.y = y;
  251. } else {
  252. wmParams.y = defaultY;
  253. }
  254. wmParams.alpha = 1;
  255. wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
  256. wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
  257. wmParams2 = new WindowManager.LayoutParams();
  258. wmParams2.format = PixelFormat.RGBA_8888;
  259. wmParams2.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  260. wmParams2.width = WindowManager.LayoutParams.WRAP_CONTENT;
  261. wmParams2.height = WindowManager.LayoutParams.WRAP_CONTENT;
  262. wmParams2.y=-screenHeigth / 4;
  263. wmParams2.gravity = Gravity.CENTER_HORIZONTAL;
  264. wmParams2.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
  265. }
  266. /**
  267. * 初始化悬浮球
  268. */
  269. private void initFloat() {
  270. mFloatLogo = new DotImageView(mActivity, mLogoRes);
  271. mFloatLogo.setLayoutParams(new WindowManager.LayoutParams(dp2Px(50, mActivity), dp2Px(50, mActivity)));
  272. mFloatLogo.setDrawDarkBg(true);
  273. floatBtnEvent();
  274. try {
  275. wManager.addView(createHideView(mActivity), wmParams2);
  276. hideView.setVisibility(View.INVISIBLE);
  277. wManager.addView(mFloatLogo, wmParams);
  278. }
  279. catch (Exception e) {
  280. e.printStackTrace();
  281. }
  282. mHideTimer.start();
  283. }
  284. /**
  285. * 初始化 隐藏悬浮球的定时器
  286. */
  287. private void initTimer() {
  288. mHideTimer = new CountDownTimer(2000, 10) { //悬浮窗超过5秒没有操作的话会自动隐藏
  289. @Override
  290. public void onTick(long millisUntilFinished) {
  291. }
  292. @Override
  293. public void onFinish() {
  294. if (!isDraging) {
  295. if (mHintLocation == LEFT) {
  296. mFloatLogo.setStatus(DotImageView.HIDE_LEFT);
  297. mFloatLogo.setDrawDarkBg(true);
  298. } else {
  299. mFloatLogo.setStatus(DotImageView.HIDE_RIGHT);
  300. mFloatLogo.setDrawDarkBg(true);
  301. }
  302. }
  303. }
  304. };
  305. }
  306. /**
  307. * 悬浮窗的点击事件和touch事件的切换
  308. */
  309. private void floatBtnEvent() {
  310. mFloatLogo.setOnTouchListener(touchListener);//恢复touch事件
  311. }
  312. /**
  313. * 悬浮窗touch事件的 down 事件
  314. */
  315. private void floatEventDown(MotionEvent event) {
  316. isDraging = false;
  317. mHideTimer.cancel();
  318. if (mFloatLogo.getStatus() != DotImageView.NORMAL) {
  319. mFloatLogo.setStatus(DotImageView.NORMAL);
  320. }
  321. if (!mFloatLogo.mDrawDarkBg) {
  322. mFloatLogo.setDrawDarkBg(true);
  323. }
  324. if (mFloatLogo.getStatus() != DotImageView.NORMAL) {
  325. mFloatLogo.setStatus(DotImageView.NORMAL);
  326. }
  327. mXInView = event.getX();
  328. mYinview = event.getY();
  329. mXDownInScreen = event.getRawX();
  330. mYDownInScreen = event.getRawY();
  331. mXInScreen = event.getRawX();
  332. mYInScreen = event.getRawY();
  333. }
  334. /**
  335. * 悬浮窗touch事件的 move 事件
  336. */
  337. private void floatEventMove(MotionEvent event) {
  338. mXInScreen = event.getRawX();
  339. mYInScreen = event.getRawY();
  340. //连续移动的距离超过3则更新一次视图位置
  341. if (Math.abs(mXInScreen - mXDownInScreen) > mFloatLogo.getWidth() / 4 || Math.abs(mYInScreen - mYDownInScreen) > mFloatLogo.getWidth() / 4) {
  342. wmParams.x = (int) (mXInScreen - mXInView);
  343. wmParams.y = (int) (mYInScreen - mYinview) - mFloatLogo.getHeight() / 2;
  344. updateViewPosition(); // 手指移动的时候更新小悬浮窗的位置
  345. double a = mScreenWidth / 2;
  346. float offset = (float) ((a - (Math.abs(wmParams.x - a))) / a);
  347. mFloatLogo.setDraging(isDraging, offset, false);
  348. } else {
  349. isDraging = false;
  350. mFloatLogo.setDraging(false, 0, true);
  351. }
  352. hideView.setVisibility(View.VISIBLE);
  353. float x = wmParams.x;
  354. float y = wmParams.y;
  355. float XMinPoint = mScreenWidth / 2 - hideView.getWidth();
  356. float XMaxPoint = mScreenWidth / 2 + hideView.getWidth();
  357. float YMinPoint = screenHeigth / 4 - hideView.getHeight() - mStatusBarHeight;
  358. float YMaxPoint = screenHeigth / 4 - mStatusBarHeight;
  359. if (x >= XMinPoint && x <= XMaxPoint && y >=YMinPoint && y <= YMaxPoint){
  360. ll_hide.setBackgroundResource(ResourceUtil.getDrawableId(mActivity, "qj_hide_float_view_bg"));
  361. }else {
  362. ll_hide.setBackgroundResource(ResourceUtil.getDrawableId(mActivity, "qj_text_black"));
  363. }
  364. }
  365. private void floatEventUp() {
  366. float x = wmParams.x;
  367. float y = wmParams.y;
  368. float RawX = mScreenWidth / 2 - hideView.getWidth();
  369. float RawX1 = mScreenWidth / 2 + hideView.getWidth();
  370. float RawY = screenHeigth / 4 - hideView.getHeight() - mStatusBarHeight;
  371. float RawY1 = screenHeigth / 4 - mStatusBarHeight;
  372. hideView.setVisibility(View.INVISIBLE);
  373. if (x >= RawX && x <= RawX1 && y >=RawY && y <= RawY1){
  374. CustomExitGameDialog.Builder builder = new CustomExitGameDialog.Builder(mActivity);
  375. builder.Create().show();
  376. builder.setPositiveButton(new DialogInterface.OnClickListener() {
  377. @Override
  378. public void onClick(DialogInterface dialog, int which) {
  379. destoryFloat();
  380. dialog.dismiss();
  381. }
  382. });
  383. builder.setnegativeButton(new DialogInterface.OnClickListener() {
  384. @Override
  385. public void onClick(DialogInterface dialog, int which) {
  386. dialog.dismiss();
  387. }
  388. });
  389. builder.setTitle(mActivity.getString(ResourceUtil.getStringId(mActivity, "qj_close_float_view_tip")));
  390. }
  391. }
  392. /**
  393. * 悬浮窗touch事件的 up 事件
  394. */
  395. private void floatEventCancel() {
  396. if (mXInScreen < mScreenWidth / 2) { //在左边
  397. mHintLocation = LEFT;
  398. } else { //在右边
  399. mHintLocation = RIGHT;
  400. }
  401. if (valueAnimator == null) {
  402. valueAnimator = ValueAnimator.ofInt(64);
  403. valueAnimator.setInterpolator(mLinearInterpolator);
  404. valueAnimator.setDuration(1000);
  405. valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  406. @Override
  407. public void onAnimationUpdate(ValueAnimator animation) {
  408. mResetLocationValue = (int) animation.getAnimatedValue();
  409. mHandler.post(updatePositionRunnable);
  410. }
  411. });
  412. valueAnimator.addListener(new Animator.AnimatorListener() {
  413. @Override
  414. public void onAnimationStart(Animator animation) {
  415. }
  416. @Override
  417. public void onAnimationEnd(Animator animation) {
  418. if (Math.abs(wmParams.x) < 0) {
  419. wmParams.x = 0;
  420. } else if (Math.abs(wmParams.x) > mScreenWidth) {
  421. wmParams.x = mScreenWidth;
  422. }
  423. updateViewPosition();
  424. isDraging = false;
  425. mFloatLogo.setDraging(false, 0, true);
  426. mHideTimer.start();
  427. }
  428. @Override
  429. public void onAnimationCancel(Animator animation) {
  430. if (Math.abs(wmParams.x) < 0) {
  431. wmParams.x = 0;
  432. } else if (Math.abs(wmParams.x) > mScreenWidth) {
  433. wmParams.x = mScreenWidth;
  434. }
  435. updateViewPosition();
  436. isDraging = false;
  437. mFloatLogo.setDraging(false, 0, true);
  438. mHideTimer.start();
  439. }
  440. @Override
  441. public void onAnimationRepeat(Animator animation) {
  442. }
  443. });
  444. }
  445. if (!valueAnimator.isRunning()) {
  446. valueAnimator.start();
  447. }
  448. // //这里需要判断如果如果手指所在位置和logo所在位置在一个宽度内则不移动,
  449. if (Math.abs(mXInScreen - mXDownInScreen) > mFloatLogo.getWidth() / 5 || Math.abs(mYInScreen - mYDownInScreen) > mFloatLogo.getHeight() / 5) {
  450. isDraging = false;
  451. }else {
  452. if (isDraging)
  453. return;
  454. if (mListener != null) mListener.onClick();
  455. }
  456. }
  457. /**
  458. * 用于检查并更新悬浮球的位置
  459. */
  460. private void checkPosition() {
  461. if (wmParams.x > 0 && wmParams.x < mScreenWidth) {
  462. if (mHintLocation == LEFT) {
  463. wmParams.x = wmParams.x - mResetLocationValue;
  464. } else {
  465. wmParams.x = wmParams.x + mResetLocationValue;
  466. }
  467. updateViewPosition();
  468. double a = mScreenWidth / 2;
  469. float offset = (float) ((a - (Math.abs(wmParams.x - a))) / a);
  470. mFloatLogo.setDraging(isDraging, offset, true);
  471. return;
  472. }
  473. if (Math.abs(wmParams.x) < 0) {
  474. wmParams.x = 0;
  475. } else if (Math.abs(wmParams.x) > mScreenWidth) {
  476. wmParams.x = mScreenWidth;
  477. }
  478. if (valueAnimator.isRunning()) {
  479. valueAnimator.cancel();
  480. }
  481. updateViewPosition();
  482. isDraging = false;
  483. }
  484. /**
  485. * 更新悬浮窗在屏幕中的位置。
  486. */
  487. private void updateViewPosition() {
  488. isDraging = true;
  489. try {
  490. wManager.updateViewLayout(mFloatLogo, wmParams);
  491. } catch (Exception e) {
  492. e.printStackTrace();
  493. }
  494. }
  495. public void show() {
  496. try {
  497. if (wManager != null && wmParams != null && mFloatLogo != null) {
  498. wManager.addView(createHideView(mActivity), wmParams2);
  499. wManager.addView(mFloatLogo, wmParams);
  500. }
  501. if (mHideTimer != null) {
  502. mHideTimer.start();
  503. } else {
  504. initTimer();
  505. mHideTimer.start();
  506. }
  507. } catch (Exception e) {
  508. e.printStackTrace();
  509. }
  510. }
  511. /**
  512. * 移除所有悬浮窗 释放资源
  513. */
  514. public void destoryFloat() {
  515. //记录上次的位置logo的停放位置,以备下次恢复
  516. saveSetting(LOCATION_X, mHintLocation);
  517. saveSetting(LOCATION_Y, wmParams.y);
  518. mFloatLogo.clearAnimation();
  519. try {
  520. mHideTimer.cancel();
  521. wManager.removeViewImmediate(mFloatLogo);
  522. wManager.removeViewImmediate(hideView);
  523. isDraging = false;
  524. } catch (Exception e) {
  525. e.printStackTrace();
  526. }
  527. }
  528. /**
  529. * 用于保存悬浮球的位置记录
  530. *
  531. * @param key String
  532. * @param defaultValue int
  533. * @return int
  534. */
  535. private int getSetting(String key, int defaultValue) {
  536. try {
  537. SharedPreferences sharedata = mActivity.getSharedPreferences("floatLogo", 0);
  538. return sharedata.getInt(key, defaultValue);
  539. } catch (Exception e) {
  540. e.printStackTrace();
  541. }
  542. return defaultValue;
  543. }
  544. /**
  545. * 用于保存悬浮球的位置记录
  546. *
  547. * @param key String
  548. * @param value int
  549. */
  550. public void saveSetting(String key, int value) {
  551. try {
  552. SharedPreferences.Editor sharedata = mActivity.getSharedPreferences("floatLogo", 0).edit();
  553. sharedata.putInt(key, value);
  554. sharedata.apply();
  555. } catch (Exception e) {
  556. e.printStackTrace();
  557. }
  558. }
  559. public static int dp2Px(float dp, Context mContext) {
  560. return (int) TypedValue.applyDimension(
  561. TypedValue.COMPLEX_UNIT_DIP,
  562. dp,
  563. mContext.getResources().getDisplayMetrics());
  564. }
  565. public static final class Builder {
  566. private Bitmap mLogoRes;
  567. private int mDefaultLocation;
  568. private Context mActivity;
  569. private OnFloatClickListener listener;
  570. public Builder setBgDrawable(Drawable drawable) {
  571. return this;
  572. }
  573. public Builder() {
  574. }
  575. public Builder logo(Bitmap val) {
  576. mLogoRes = val;
  577. return this;
  578. }
  579. public Builder withActivity(Activity val) {
  580. mActivity = val;
  581. return this;
  582. }
  583. public Builder defaultLocation(int val) {
  584. mDefaultLocation = val;
  585. return this;
  586. }
  587. public Builder setOnFloatClickListener(OnFloatClickListener onClickFloatListener) {
  588. this.listener = onClickFloatListener;
  589. return this;
  590. }
  591. public FloatLogoMenu show() {
  592. return new FloatLogoMenu(this);
  593. }
  594. }
  595. public interface OnFloatClickListener {
  596. void onClick();
  597. }
  598. }