
原本想通过framelayout实现一个悬浮在其他控件上的按钮,但是觉得很麻烦,需要各个界面都要动态填充.于是想到了悬浮窗,就自定一个ImageVIEw用于显示全局按钮.
一、首先因为悬浮窗式的所以要添加权限,对于SDK>=23的需要动态获取权限,我这边用的是22的
<uses-permission androID:name="androID.permission.SYstem_ALERT_WINDOW" /> <uses-permission androID:name="androID.permission.WRITE_SETTINGS"/>
二、通过application获取到全局性的WindowManager的params数据
private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams(); public WindowManager.LayoutParams getMywmParams(){ return wmParams; }三、自定义ImageVIEw,并实现点击具有状态选择.其中写了一个回调接口用于对点击事件的处理
public class CustomeMovebutton extends ImageVIEw { private final int statusHeight; int sW; int sH; private float mtouchstartX; private float mtouchstartY; private float x; private float y; private boolean isMove=false; private Context context; private WindowManager wm = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); private WindowManager.LayoutParams wmParams = ((MyApplication) getContext().getApplicationContext()).getMywmParams(); private float mLastX; private float mLastY; private float mStartX; private float mStartY; private long mDownTime; private long mUpTime; private OnSpeakListener Listener; public CustomeMovebutton(Context context) { this(context,null); this.context = context; } public CustomeMovebutton(Context context,AttributeSet attrs) { this(context,attrs,-1); } public CustomeMovebutton(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); sW = wm.getDefaultdisplay().getWIDth(); sH = wm.getDefaultdisplay().getHeight(); statusHeight = getStatusHeight(context); } /** * 状态栏的高度 * */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class clazz = Class.forname("com.androID.internal.R$dimen"); //使用反射获取实例 Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getFIEld("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printstacktrace(); } return statusHeight; } @OverrIDe public boolean ontouchEvent(MotionEvent event) { //获取相对屏幕的坐标,即以屏幕左上角为原点 x = event.getRawX(); y = event.getRawY() - statusHeight; //statusHeight是系统状态栏的高度 switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //按下 setimageResource(R.drawable.btn_voice_pressed); mtouchstartX = event.getX(); mtouchstartY = event.getY(); mStartX = event.getRawX(); mStartY = event.getRawY(); mDownTime = System.currentTimeMillis(); isMove = false; break; case MotionEvent.ACTION_MOVE: //手指移动 updateVIEwposition(); isMove = true; break; case MotionEvent.ACTION_UP: //手抬起 setimageResource(R.drawable.btn_voice_rest); mLastX = event.getRawX(); mLastY = event.getRawY(); mUpTime = System.currentTimeMillis(); //按下到抬起的时间大于500毫秒,并且抬手到抬手绝对值大于20像素处理点击事件 if(mUpTime - mDownTime < 500){ if(Math.abs(mStartX- mLastX )< 20.0 && Math.abs(mStartY - mLastY) < 20.0){ if (Listener!=null){ Listener.onSpeakListener(); } } } break; } return true; } private voID updateVIEwposition() { wmParams.x = (int) (x - mtouchstartX); wmParams.y = (int) (y- mtouchstartY); wm.updateVIEwLayout(this,wmParams); //刷新显示 } /** * 设置点击回调接口 */ public interface OnSpeakListener{ voID onSpeakListener(); } public voID setonSpeakListener(OnSpeakListener Listener){ this.Listener=Listener; }}四、Activity中使用,其中有设置图片的参数和位置参数
public class MainActivity extends AppCompatActivity{ private WindowManager wm; private WindowManager.LayoutParams wmParams; private CustomeMovebutton CustomeMovebutton; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); displayMetrics dm = getResources().getdisplayMetrics(); int wIDthPixels = dm.wIDthPixels; int heightPixels = dm.heightPixels; wmParams = ((MyApplication) getApplication()).getMywmParams(); wmParams.type = WindowManager.LayoutParams.TYPE_SYstem_ALERT; 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 = wIDthPixels-150; //设置位置像素 wmParams.y = heightPixels-110; wmParams.wIDth=200; //设置图片大小 wmParams.height=200; CustomeMovebutton = new CustomeMovebutton(getApplicationContext()); CustomeMovebutton.setimageResource(R.drawable.btn_voice_rest); wm.addVIEw(CustomeMovebutton,wmParams); CustomeMovebutton.setonSpeakListener(new CustomeMovebutton.OnSpeakListener() { @OverrIDe public voID onSpeakListener() { Toast.makeText(MainActivity.this,"点击事件",Toast.LENGTH_SHORT).show(); } }); } @OverrIDe protected voID onDestroy() { super.onDestroy(); if(CustomeMovebutton != null){ wm.removeVIEw(CustomeMovebutton); } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:Android自定义可拖拽的悬浮按钮DragFloatingActionButtonAndroid 中FloatingActionButton(悬浮按钮)实例详解Android中FloatingActionButton实现悬浮按钮实例Android实现系统级悬浮按钮Android悬浮按钮点击返回顶部FloatingActionButtonAndroid开发模仿qq视频通话悬浮按钮(实例代码)Android利用WindowManager生成悬浮按钮及悬浮菜单Android开发中在TableView上添加悬浮按钮的方法Android开发悬浮按钮 Floating ActionButton的实现方法Android利用悬浮按钮实现翻页效果 总结以上是内存溢出为你收集整理的Android自定义APP全局悬浮按钮全部内容,希望文章能够帮你解决Android自定义APP全局悬浮按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)