Android开发模仿qq视频通话悬浮按钮(实例代码)

模仿qq视频通话的悬浮按钮的实例代码,如下所示;

public class FloatingWindowService extends Service{ private static final String TAG="OnTouchListener"; private static View mView = null; private static WindowManager mWindowManager = null; private static Context mContext = null; public static Boolean isShown = false; public WindowManager.LayoutParams params = null; private int pixel; private int TheOffset; @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { pixel = intent.getIntExtra("pixel",1); showPopupWindow(this); return super.onStartCommand(intent, flags, startId); } /** * 显示弹出框 * * @param context * */ private void showPopupWindow(final Context context) { if (isShown) { return; } isShown = true; // 获取应用的Context mContext = context.getApplicationContext(); // 获取WindowManager mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); params = new WindowManager.LayoutParams(); mView = setUpView(context); // 类型 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; int flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.flags = flags; params.format = PixelFormat.TRANSLUCENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.gravity = Gravity.CENTER; mWindowManager.addView(mView, params); } /** * 隐藏弹出框 */ private static void hidePopupWindow() { if (isShown && null != mView) { mWindowManager.removeView(mView); isShown = false; } } private int x=0; private int y=0; private int startX=0; private int startY=0; private View setUpView(final Context context) { View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null); TextView tv= (TextView) view.findViewById(R.id.title); int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); tv.measure(w, h); TheOffset=(pixel-tv.getMeasuredWidth())/2-50; tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent =new Intent(context,MainActivity.class); context.startActivity(intent); // Toast.makeText(context,"点击事件",Toast.LENGTH_LONG).show(); } }); tv.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_MOVE: int newX= (int) (event.getRawX()-x); int newY= (int) (event.getRawY()-y); params.x=newX+startX; params.y=newY+startY; mWindowManager.updateViewLayout(mView,params); break; case MotionEvent.ACTION_DOWN: x= (int) event.getRawX(); y= (int) event.getRawY(); break; case MotionEvent.ACTION_UP: if(params.x>=0){ params.x=TheOffset; mWindowManager.updateViewLayout(mView,params); } if(params.x<=-0){ params.x=-TheOffset; mWindowManager.updateViewLayout(mView,params); } Log.i(TAG,params.x+""); Log.i(TAG,params.y+""); //判断 从按住到抬起时候的移动距离, 如果如果移动距离大于20 那么就拦截事件,否则就不拦截事件,主要是处理点击事件的冲突 if(Math.abs(startX-params.x)>20 ||Math.abs(startY-params.y)>20 ){ //记录上一次的偏移量 startX=params.x; startY=params.y; return true; }else { startX=params.x; startY=params.y; return false; } } return false; } }); return view; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); if (mView != null) { isShown=false; mWindowManager.removeView(mView); } } }

Main

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.open).setOnClickListener(this); findViewById(R.id.close).setOnClickListener(this); }

-点击开启 关闭悬浮按钮

@Override public void onClick(View v) { switch (v.getId()){ case R.id.open: //判断是否拥有悬浮权限 //op 的值是 0 ~ 47,其中0代表粗略定位权限,1代表精确定位权限,24代表悬浮窗权限。(具体可以看看Android源码在android.app下就有个AppOpsManager类) if(utils.checkOp(this,24)==0) { Intent intent=new Intent(MainActivity.this, FloatingWindowService.class); intent.putExtra("pixel",utils.pixel(this)[0]); startService(intent); }else { //引导用户进入悬浮权限设置界面 Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 200); } break; case R.id.close: stopService(new Intent(MainActivity.this,FloatingWindowService.class)); break; } }

判断权限 -获取屏幕的宽高

public class utils { public static int checkOp(Context context, int op){ final int version = Build.VERSION.SDK_INT; if (version >= 19){ Object object = context.getSystemService("appops"); Class c = object.getClass(); try { Class[] cArg = new Class[3]; cArg[0] = int.class; cArg[1] = int.class; cArg[2] = String.class; Method lMethod = c.getDeclaredMethod("checkOp", cArg); return (Integer) lMethod.invoke(object, op, Binder.getCallingUid(), context.getPackageName()); } catch(NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } return -1; } /** * 获取屏幕的宽高 * @param context * @return */ public static int[] pixel(Activity context){ DisplayMetrics dm = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(dm); return new int[]{dm.widthPixels,dm.heightPixels}; } }

--popupwindow填充布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/popup_window" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" > <TextView android:background="@mipmap/ic_launcher" android:id="@+id/title" android:layout_width="50dp" android:layout_height="50dp"/> </LinearLayout> </LinearLayout>

以上所述是小编给大家介绍的Android开发模仿qq视频通话悬浮按钮(实例代码),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

时间: 2024-08-21 14:51:51

Android开发模仿qq视频通话悬浮按钮(实例代码)的相关文章

Android 中FloatingActionButton(悬浮按钮)实例详解

Android 中FloatingActionButton(悬浮按钮)实例详解 一.介绍 这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性 二.使用准备, 在as 的 build.grade文件中写上 compile 'com.android.support:design:22.2.0' 三.使用说明 <android.support.design.widget.FloatingActionButton android:id="@+id/floa

Android开发高仿课程表的布局实例详解_Android

先说下这个demo,这是一个模仿课程表的布局文件,虽然我是个菜鸟,但我还是想留给学习的人一些例子,先看下效果   然后再来看一下我们学校的app 布局分析 先上一张划分好了的布局图 首先整个页面放在一个LinearLayout布局下面,分为上面和下面两个部分,下面一个是显示课程表的详细信息 1:这个没什么好讲的,就是直接一个LinearLayout布局,然后将控件一个TextView用来显示年份,一个View用来当作竖线,一个Spinner用来显示选择周数 2:这个是显示星期几的部件,是我自定义

Android开发仿QQ空间根据位置弹出PopupWindow显示更多操作效果_Android

我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图: 这个要实现这个效果可以分几步进行 1.第一步自定义PopupWindow,实现如图的样式,这个继承PopupWindow自定义布局很容易实现 2.得到点击按钮的位置,根据位置是否在屏幕的中间的上方还是下方,将PopupWindow显示在控件的上方或者下方 3.适配问题,因为PopupWindow上面的操作列表

Android开发仿QQ空间根据位置弹出PopupWindow显示更多操作效果

我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图: 这个要实现这个效果可以分几步进行 1.第一步自定义PopupWindow,实现如图的样式,这个继承PopupWindow自定义布局很容易实现 2.得到点击按钮的位置,根据位置是否在屏幕的中间的上方还是下方,将PopupWindow显示在控件的上方或者下方 3.适配问题,因为PopupWindow上面的操作列表

Android开发高仿课程表的布局实例详解

先说下这个demo,这是一个模仿课程表的布局文件,虽然我是个菜鸟,但我还是想留给学习的人一些例子,先看下效果 然后再来看一下我们学校的app 布局分析 先上一张划分好了的布局图 首先整个页面放在一个LinearLayout布局下面,分为上面和下面两个部分,下面一个是显示课程表的详细信息 1:这个没什么好讲的,就是直接一个LinearLayout布局,然后将控件一个TextView用来显示年份,一个View用来当作竖线,一个Spinner用来显示选择周数 2:这个是显示星期几的部件,是我自定义的V

Android中实现图文并茂的按钮实例代码

效果图如下所示: 代码: <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:gravity="center" android:background="#8

Android高仿QQ6.0侧滑删除实例代码_Android

推荐阅读: 先给大家分享一下,侧滑删除,布局也就是前面一个item,然后有两个隐藏的按钮(TextView也可以),然后我们可以向左侧滑动,然后显示出来,然后对delete(删除键)实现监听,就可以了哈.好了那就来看看代码怎么实现的吧. 首先和之前一样 自定义View,初始化ViewDragHelper: package com.example.removesidepull; import android.content.Context; import android.support.v4.wi

Android高仿QQ6.0侧滑删除实例代码

推荐阅读: 先给大家分享一下,侧滑删除,布局也就是前面一个item,然后有两个隐藏的按钮(TextView也可以),然后我们可以向左侧滑动,然后显示出来,然后对delete(删除键)实现监听,就可以了哈.好了那就来看看代码怎么实现的吧. 首先和之前一样 自定义View,初始化ViewDragHelper: package com.example.removesidepull; import android.content.Context; import android.support.v4.wi

网页-Android开发之中,是否可以运行js代码。

问题描述 Android开发之中,是否可以运行js代码. Android开发过程之中,我需要异步登陆其他网站并解析其网页.在进行模拟登陆过程之中需要对输入的用户名和密码进行加密.现在已经有了网站加密的js文件,请问除了用webview方法之外还有没有方法可以运行js代码的. 解决方案 用javax.script参考:http://stackoverflow.com/questions/1454425/reference-javax-script-scriptengine-in-android-o