一、创建悬浮图标类
package com.test.androidtest; import android.content.Context; import android.graphics.PixelFormat; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; /** * 悬浮图标 */ public class GameFloatView extends ImageView { private static GameFloatView instance; private WindowManager wm; private WindowManager.LayoutParams wmParams; private float mTouchStartX; private float mTouchStartY; private float mRawX; private float mRawY; public GameFloatView(Context context) { super(context); createFloat(context); } private void createFloat(Context context) { wm = (WindowManager)(context.getSystemService(Context.WINDOW_SERVICE)); wmParams = new WindowManager.LayoutParams(); //wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;//设置窗口类型 wmParams.format = PixelFormat.RGBA_8888;//设置背景透明 wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; wmParams.gravity = Gravity.LEFT|Gravity.TOP; //设置屏幕坐标系的原点在左上角 //设置坐标(屏幕左上角) wmParams.x = 0; wmParams.y = 0; //设置宽高 wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; wm.addView(this, wmParams);//显示悬浮图标 this.setImageResource(R.drawable.game_float_view_bg); //处理拖动 this.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { //获取屏蔽坐标(以屏幕左上角为原点) mRawX = event.getRawX(); mRawY = event.getRawY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: //获取相对view的坐标(以此view左上角为原点) mTouchStartX = event.getX(); mTouchStartY = event.getY(); break; case MotionEvent.ACTION_MOVE: updatePosition(); break; case MotionEvent.ACTION_UP: updatePosition(); mTouchStartX=mTouchStartY=0; break; } return false; } }); //处理Click this.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Log.i("test", "点击悬浮图标 onClick"); } }); } void updatePosition() { //在全屏模式(Fullscreen)下以下计算方式是对的。如果有状态栏,需要减去状态栏高度。 wmParams.x=(int)(mRawX-mTouchStartX); wmParams.y=(int)(mRawY-mTouchStartY); wm.updateViewLayout(this, wmParams); } public static void show(Context context) { if(instance == null) instance = new GameFloatView(context); instance.setVisibility(VISIBLE); } public static void hide() { if(instance != null) instance.setVisibility(GONE); } public void onDestroy() { wm.removeView(this); } public static void destroy() { if(instance != null){ instance.onDestroy(); instance = null; } } }
二、创建活动
package com.test.androidtest; import android.app.Activity; import android.os.Bundle; public class GameFloatActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); } @Override protected void onResume(){ super.onResume(); GameFloatView.show(this); } @Override protected void onPause(){ super.onPause(); GameFloatView.hide(); } @Override protected void onDestroy(){ super.onDestroy(); GameFloatView.destroy(); } }
运行测试