1. importandroid.app.Activity;
  2. importandroid.content.Context;
  3. importandroid.content.SharedPreferences;
  4. importandroid.content.SharedPreferences.Editor;
  5. importandroid.graphics.PixelFormat;
  6. importandroid.os.Bundle;
  7. importandroid.util.Log;
  8. importandroid.view.Gravity;
  9. importandroid.view.LayoutInflater;
  10. importandroid.view.MotionEvent;
  11. importandroid.view.View;
  12. importandroid.view.View.OnTouchListener;
  13. importandroid.view.WindowManager;
  14. importandroid.view.WindowManager.LayoutParams;
  15. importandroid.widget.TextView;
  16. publicclassFrameActivityextendsActivity{
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. privatestaticfinalStringTAG="FloatFrameActivity";
  19. privatestaticfinalbooleanDEBUG=true;
  20. privateViewmViews;
  21. privateTextViewmView;
  22. //窗口管理器
  23. privateWindowManagermWindowManager;
  24. //布局参数
  25. privateWindowManager.LayoutParamsmLayoutParams;
  26. //上次使用时悬浮框在屏幕中的坐标
  27. privateintlastX=0;
  28. privateintlastY=0;
  29. //屏幕大小
  30. privatestaticintmWidth;
  31. privatestaticintmHidth;
  32. @Override
  33. protectedvoidonPause(){
  34. super.onPause();
  35. //保存最后一次使用的位置
  36. SharedPreferenceslocation=getSharedPreferences("Location",Context.MODE_PRIVATE);
  37. Editoredit=location.edit();
  38. edit.putInt("lastX",lastX);
  39. edit.putInt("lastY",lastY);
  40. edit.commit();
  41. if(DEBUG)Log.i(TAG,"--->onPaust");
  42. }
  43. @Override
  44. protectedvoidonStart(){
  45. super.onStart();
  46. //取出上次使用的位置
  47. SharedPreferenceslocation=this.getSharedPreferences("Location",Context.MODE_PRIVATE);
  48. lastX=location.getInt("lastX",(mWidth-mViews.getWidth())/2);
  49. lastY=location.getInt("lastY",(mHidth-mViews.getHeight())/2);
  50. updateFloatFrame();
  51. if(DEBUG)Log.i(TAG,"--->onStart");
  52. }
  53. @Override
  54. protectedvoidonDestroy(){
  55. super.onDestroy();
  56. removeFloatFrame();
  57. if(DEBUG)Log.i(TAG,"--->onDestroy");
  58. }
  59. @Override
  60. publicvoidonCreate(BundlesavedInstanceState){
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.main);
  63. //得到屏幕的大小
  64. mWidth=getWindowManager().getDefaultDisplay().getWidth();
  65. mHidth=getWindowManager().getDefaultDisplay().getHeight();
  66. //显示悬浮窗
  67. init();
  68. if(DEBUG)Log.i(TAG,"--->onCreate");
  69. }
  70. //删除悬浮框
  71. privatevoidremoveFloatFrame(){
  72. mWindowManager.removeView(mViews);
  73. if(DEBUG)Log.i(TAG,"--->FloatFrameremoved");
  74. }
  75. //更新悬浮框
  76. privatevoidupdateFloatFrame(){
  77. mLayoutParams.x=lastX;
  78. mLayoutParams.y=lastY;
  79. mWindowManager.updateViewLayout(mViews,mLayoutParams);
  80. if(DEBUG)Log.i(TAG,"--->FloatFrameupdated");
  81. }
  82. privatevoidinit(){
  83. /*必须添加权限:<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  84. */
  85. //获得窗口管理器
  86. mWindowManager=(WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
  87. mLayoutParams=newLayoutParams();
  88. //悬浮框布局
  89. mLayoutParams.width=LayoutParams.WRAP_CONTENT;
  90. mLayoutParams.height=LayoutParams.WRAP_CONTENT;
  91. mLayoutParams.gravity=Gravity.LEFT|Gravity.TOP;
  92. //悬浮框在屏幕中的初始位置
  93. mLayoutParams.x=lastX;
  94. mLayoutParams.y=lastY;
  95. mLayoutParams.type=LayoutParams.TYPE_PHONE;
  96. //设置半透明
  97. mLayoutParams.format=PixelFormat.TRANSLUCENT;
  98. mLayoutParams.flags=LayoutParams.FLAG_NOT_FOCUSABLE|LayoutParams.FLAG_NOT_TOUCH_MODAL;
  99. //获得悬浮窗布局视图
  100. mViews=LayoutInflater.from(this).inflate(R.layout.frame,null);
  101. //从悬浮窗布局视图中获取显示电话号码的TextView
  102. mView=(TextView)mViews.findViewById(R.id.text_view);
  103. //将悬浮框添加到窗口管理器中
  104. mWindowManager.addView(mViews,mLayoutParams);
  105. //给整个悬浮框添加OnTouch事件
  106. mView.setOnTouchListener(newOnTouchListener(){
  107. floatx=0,y=0;
  108. publicbooleanonTouch(Viewv,MotionEventevent){
  109. switch(event.getAction()){
  110. caseMotionEvent.ACTION_DOWN:
  111. x=event.getX();
  112. y=event.getY();
  113. break;
  114. caseMotionEvent.ACTION_MOVE:
  115. updateViewPosition(v,event);
  116. break;
  117. caseMotionEvent.ACTION_UP:
  118. break;
  119. }
  120. returntrue;
  121. }
  122. //更新悬浮框
  123. privatevoidupdateViewPosition(Viewv,MotionEventevent){
  124. lastX=(int)(event.getRawX()-x);
  125. //38为状态栏的宽度
  126. lastY=(int)(event.getRawY()-y)-38;
  127. //窗口管理器更新悬浮框
  128. updateFloatFrame();
  129. }
  130. });
  131. }
  132. }

Java代码

            
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TextViewxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/text_view"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:gravity="center"
  7. android:background="@android:color/transparent"
  8. android:layout_gravity="center"
  9. android:text="悬浮框">
  10. </TextView>

更多相关文章

  1. Android(安卓)技术博客(3):android 悬浮窗菜单,可用于显示在 launche
  2. Android(安卓)响应键盘移动图标
  3. android manifest相关属性
  4. android点亮(唤醒)屏幕
  5. android
  6. Android(安卓)利用adb命令 使App自动点击屏幕指定位置
  7. Android(安卓)-- DisplayMetrics
  8. android定制对话框
  9. android 获取屏幕高度和宽度

随机推荐

  1. 最新android sdk版本号和sdk的对应关系
  2. Android 动画方案
  3. Android Log详解!
  4. [Android] ubuntu 下不识别 Android 设备
  5. [置顶] Android IPC 通讯机制源码分析【
  6. android去掉button默认的点击阴影
  7. View视图——TextView、EditText、Button
  8. 【转】Android之自定义设备管理
  9. android进程间共享简单数据
  10. 自己封装的Android sqlite-helper.jar包