仿ios touch按钮创建可随意拖动的悬浮按钮
2017-10-12 16:35
591 查看
WindowManager mWindowManager; WindowManager.LayoutParams wmParams; LinearLayout mFloatLayout; ImageView mFloatView; private void createFloatView() { //获取LayoutParams对象 wmParams = new WindowManager.LayoutParams(); //获取的是LocalWindowManager对象 mWindowManager = this.getWindowManager(); Log.i(TAG, "mWindowManager1--->" + this.getWindowManager()); //mWindowManager = getWindow().getWindowManager(); Log.i(TAG, "mWindowManager2--->" + getWindow().getWindowManager()); //获取的是CompatModeWrapper对象 //mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE); Log.i(TAG, "mWindowManager3--->" + mWindowManager); wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; wmParams.format = PixelFormat.RGBA_8888; wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; wmParams.gravity = Gravity.LEFT | Gravity.TOP; wmParams.x = 0; wmParams.y = 100; wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; LayoutInflater inflater = this.getLayoutInflater();//LayoutInflater.from(getApplication()); mFloatLayout = (LinearLayout) inflater.inflate(R.layout.home_easy_touch_view, null); mWindowManager.addView(mFloatLayout, wmParams); //setContentView(R.layout.main); mFloatView = (ImageView) mFloatLayout.findViewById(R.id.easy_touch_view_imageview); Log.i(TAG, "mFloatView" + mFloatView); Log.i(TAG, "mFloatView--parent-->" + mFloatView.getParent()); Log.i(TAG, "mFloatView--parent--parent-->" + mFloatView.getParent().getParent()); //绑定触摸移动监听 mFloatView.setOnTouchListener(mTouchListener); } private View.OnTouchListener mTouchListener = new View.OnTouchListener() { float lastX, lastY; int paramX, paramY; private int mOldOffsetX; private int mOldOffsetY; private int mTag = 0; public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction(); float x = event.getRawX(); float y = event.getRawY(); if (mTag == 0) { mOldOffsetX = wmParams.x; // ƫ���� mOldOffsetY = wmParams.y; // ƫ���� } switch (action) { case MotionEvent.ACTION_MOVE: wmParams.x = (int) event.getRawX() - mFloatLayout.getWidth() / 2; //25为状态栏高度 wmParams.y = (int) event.getRawY() - mFloatLayout.getHeight() / 2 - 40; mWindowManager.updateViewLayout(mFloatLayout, wmParams); mTag = 1; break; case MotionEvent.ACTION_UP: motionActionUpEvent(x, y); break; default: break; } return true; } private void motionActionUpEvent(float x, float y) { int newOffsetX = wmParams.x; int newOffsetY = wmParams.y; if (Math.abs(mOldOffsetX - newOffsetX) > 50 || Math.abs(mOldOffsetY - newOffsetY) > 50) { mTag = 0; } else { try { Intent intentUhome = new Intent(); PackageManager packageManager = getPackageManager(); intentUhome = packageManager.getLaunchIntentForPackage(SpeechUtil.AVS_PROCESS_NAME); intentUhome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intentUhome); } catch (Exception ex) { ex.toString(); } } } }; home_easy_touch_view.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical" >
<ImageView
android:id="@+id/easy_touch_view_imageview"
android:layout_width="200px"
android:layout_height="250px"
android:background="@drawable/icon_avs" />
</LinearLayout>
往android6.0的系统上运行时,会崩溃,抛出permission denied for this window type异常。但在AndroidMenafest.xml中已经添加了SYSTEM_ALERT_WINDOW的权限,
解决方法:
将wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;改成
wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;即可。
相关文章推荐
- iOS自定义悬浮按钮,Objective-C可拖动的浮动按钮,iOS浮动图标,类似AssistiveTouch的浮动漂浮按钮
- IOS图片和按钮在屏幕中随意拖动
- iOS开发-UIWindow的用法-创建悬浮按钮
- 【iOS开发】UIWindow创建悬浮按钮
- 使用WindowManager创建可拖动的悬浮按钮
- 您的位置:首页 » IOS » iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
- 【iOS_GitHub】悬浮球/悬浮按钮/辅助按钮(类似于iOS系统自带的AssistiveTouch/京东/聚划算/等的辅助按钮)
- iOS开发-UIWindow的用法-创建悬浮按钮
- 用WindowManager实现一个类似ios悬浮可拖动的虚拟导航按钮
- iOS---悬浮按钮的创建和使用
- iOS 悬浮可拖动可点击按钮
- iOS悬浮、可拖动、自动吸附屏幕边缘的按钮制作
- 安卓实现按钮可随意拖动(同时解决click和touch事件的冲突)
- ios通过代码创建按钮
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
- C#创建可拖动按钮
- iOS之实现按钮拖动/tableViewcell多选,拖动/collectionviewcell多选,拖动(交换位置)
- android 拖动相似Iphone AssistiveTouch的快捷键按钮 - 代码共享
- 启动第三方应用时弹出仿touch悬浮按钮以退出第三方应用,返回launcher界面
- iOS代码实现:创建按钮,绑定按钮事件,读取控件值