悬浮图标

作者:追风剑情 发布于:2016-5-17 11:47 分类:Android

一、创建悬浮图标类

  1. package com.test.androidtest;
  2.  
  3. import android.content.Context;
  4. import android.graphics.PixelFormat;
  5. import android.util.Log;
  6. import android.view.Gravity;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.ImageView;
  11.  
  12. /**
  13. * 悬浮图标
  14. */
  15. public class GameFloatView extends ImageView {
  16.  
  17. private static GameFloatView instance;
  18. private WindowManager wm;
  19. private WindowManager.LayoutParams wmParams;
  20. private float mTouchStartX;
  21. private float mTouchStartY;
  22. private float mRawX;
  23. private float mRawY;
  24. public GameFloatView(Context context) {
  25. super(context);
  26. createFloat(context);
  27. }
  28. private void createFloat(Context context)
  29. {
  30. wm = (WindowManager)(context.getSystemService(Context.WINDOW_SERVICE));
  31. wmParams = new WindowManager.LayoutParams();
  32. //wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;//设置窗口类型
  33. wmParams.format = PixelFormat.RGBA_8888;//设置背景透明
  34. wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  35. wmParams.gravity = Gravity.LEFT|Gravity.TOP; //设置屏幕坐标系的原点在左上角
  36. //设置坐标(屏幕左上角)
  37. wmParams.x = 0;
  38. wmParams.y = 0;
  39. //设置宽高
  40. wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
  41. wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
  42. wm.addView(this, wmParams);//显示悬浮图标
  43. this.setImageResource(R.drawable.game_float_view_bg);
  44. //处理拖动
  45. this.setOnTouchListener(new OnTouchListener(){
  46. @Override
  47. public boolean onTouch(View v, MotionEvent event) {
  48. //获取屏蔽坐标(以屏幕左上角为原点)
  49. mRawX = event.getRawX();
  50. mRawY = event.getRawY();
  51. switch(event.getAction()){
  52. case MotionEvent.ACTION_DOWN:
  53. //获取相对view的坐标(以此view左上角为原点)
  54. mTouchStartX = event.getX();
  55. mTouchStartY = event.getY();
  56. break;
  57. case MotionEvent.ACTION_MOVE:
  58. updatePosition();
  59. break;
  60. case MotionEvent.ACTION_UP:
  61. updatePosition();
  62. mTouchStartX=mTouchStartY=0;
  63. break;
  64. }
  65. return false;
  66. }
  67. });
  68. //处理Click
  69. this.setOnClickListener(new OnClickListener(){
  70. @Override
  71. public void onClick(View v) {
  72. Log.i("test", "点击悬浮图标 onClick");
  73. }
  74. });
  75. }
  76.  
  77. void updatePosition()
  78. {
  79. //在全屏模式(Fullscreen)下以下计算方式是对的。如果有状态栏,需要减去状态栏高度。
  80. wmParams.x=(int)(mRawX-mTouchStartX);
  81. wmParams.y=(int)(mRawY-mTouchStartY);
  82. wm.updateViewLayout(this, wmParams);
  83. }
  84. public static void show(Context context)
  85. {
  86. if(instance == null)
  87. instance = new GameFloatView(context);
  88. instance.setVisibility(VISIBLE);
  89. }
  90. public static void hide()
  91. {
  92. if(instance != null)
  93. instance.setVisibility(GONE);
  94. }
  95. public void onDestroy()
  96. {
  97. wm.removeView(this);
  98. }
  99. public static void destroy()
  100. {
  101. if(instance != null){
  102. instance.onDestroy();
  103. instance = null;
  104. }
  105. }
  106. }

 

二、创建活动

  1. package com.test.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class GameFloatActivity extends Activity{
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.fragment_main);
  11. }
  12. @Override
  13. protected void onResume(){
  14. super.onResume();
  15. GameFloatView.show(this);
  16. }
  17. @Override
  18. protected void onPause(){
  19. super.onPause();
  20. GameFloatView.hide();
  21. }
  22. @Override
  23. protected void onDestroy(){
  24. super.onDestroy();
  25. GameFloatView.destroy();
  26. }
  27. }

 

运行测试

222222.png

1111.png

 

 

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号