用过iphone的朋友都知道,iPhone有个圆球辅助工具,它漂浮在你的手机屏幕(在任何APP之上),你可以将它移动到任何地方,它叫做AssistiveTouch,本篇模拟该软件实现一个小案例,主要是实现它的界面,首先来看看实现的效果吧:

拖动小圆球:

点击弹出pop窗口:

为了让辅助工具一直悬浮在窗口之上,这里使用的机制是通过在程序初始化是,启动一个service,在service的onCreate() 函数中使用LayoutInflater来加载一个view,而这个view就是辅助球的布局文件:floatball.xml,然后对它进行onclick事件的监听,setOnClickListener监听到辅助球点击事件之后,就创建一个PopupWindow,弹出如上的菜单界面,大体的实现就是这样。

其实,实现窗口悬浮于最前面的一个重要属性是:WindowManager.LayoutParams.TYPE_PHONE

我们只要将WindowManager.LayoutParams的type属性设置为 WindowManager.LayoutParams.TYPE_PHONE就可以实现悬浮最前面。

工程目录结构:

部分代码解析:

MyApplication.java:

package com.tyd.floatball.util;
import android.app.Application;
import android.view.WindowManager;
public class MyApplication extends Application {
    private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
    public WindowManager.LayoutParams getMywmParams() {
        return wmParams;
    }
}  

MainActivity.java:

package com.tyd.floatball.ui;  

import com.tyd.floatball.R;
import com.tyd.floatball.R.layout;
import com.tyd.floatball.service.TopFloatService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;  

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent service = new Intent();
        service.setClass(this, TopFloatService.class);
        //启动服务
        startService(service);
    }
}  

TopFloatService.java:

package com.tyd.floatball.service;  

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.tyd.floatball.R;
import com.tyd.floatball.util.MyApplication;  

public class TopFloatService extends Service implements OnClickListener,OnKeyListener{
    WindowManager wm = null;
    WindowManager.LayoutParams ballWmParams = null;
    private View ballView;
    private View menuView;
    private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    private RelativeLayout menuLayout;
    private Button floatImage;
    private PopupWindow pop;
    private RelativeLayout menuTop;
    private boolean ismoving = false;  

    @Override
    public void onCreate() {
        super.onCreate();
        //加载辅助球布局
        ballView = LayoutInflater.from(this).inflate(R.layout.floatball, null);
        floatImage = (Button)ballView.findViewById(R.id.float_image);
        setUpFloatMenuView();
        createView();
    }  

    /**
     * 窗口菜单初始化
     */
    private void setUpFloatMenuView(){
        menuView = LayoutInflater.from(this).inflate(R.layout.floatmenu, null);
        menuLayout = (RelativeLayout)menuView.findViewById(R.id.menu);
        menuTop = (RelativeLayout)menuView.findViewById(R.id.lay_main);
        menuLayout.setOnClickListener(this);
        menuLayout.setOnKeyListener(this);
        menuTop.setOnClickListener(this);
    }  

    /**
     * 通过MyApplication创建view,并初始化显示参数
     */
    private void createView() {
        wm = (WindowManager) getApplicationContext().getSystemService("window");
        ballWmParams =  ((MyApplication) getApplication()).getMywmParams();
        ballWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        ballWmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        ballWmParams.gravity = Gravity.LEFT | Gravity.TOP;
        ballWmParams.x = 0;
        ballWmParams.y = 0;
        ballWmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        ballWmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        ballWmParams.format = PixelFormat.RGBA_8888;
        //添加显示层
        wm.addView(ballView, ballWmParams);
        //注册触碰事件监听器
        floatImage.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                x = event.getRawX();
                y = event.getRawY();
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    ismoving = false;
                    mTouchStartX = (int)event.getX();
                    mTouchStartY = (int)event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    ismoving = true;
                    updateViewPosition();
                    break;
                case MotionEvent.ACTION_UP:
                    mTouchStartX = mTouchStartY = 0;
                    break;
                }
                //如果拖动则返回false,否则返回true
                if(ismoving == false){
                    return false;
                }else{
                    return true;
                }
            }  

        });
        //注册点击事件监听器
        floatImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DisplayMetrics dm = getResources().getDisplayMetrics();
                pop = new PopupWindow(menuView, dm.widthPixels, dm.heightPixels);
                pop.showAtLocation(ballView, Gravity.CENTER, 0, 0);
                pop.update();
            }
        });
    }  

    /**
     * 更新view的显示位置
     */
    private void updateViewPosition() {
        ballWmParams.x = (int) (x - mTouchStartX);
        ballWmParams.y = (int) (y - mTouchStartY);
        wm.updateViewLayout(ballView, ballWmParams);
    }  

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }  

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.lay_main:
            Toast.makeText(getApplicationContext(), "111", 1000).show();
            break;  

        default:
            if(pop!=null && pop.isShowing()){
                pop.dismiss();
            }
            break;
        }  

    }  

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        Toast.makeText(getApplicationContext(), "keyCode:"+keyCode, 1000).show();
        switch (keyCode) {
        case KeyEvent.KEYCODE_HOME:
            pop.dismiss();
            break;
        default:
            break;
        }
        return true;
    }  

} 

辅助球的布局文件 floatball.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical">  

    <Button
        android:id="@+id/float_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/selector_btn_assistive"
        />  

</FrameLayout>  

窗口菜单的布局文件floatmenu.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/transparent" >  

    <LinearLayout
        android:layout_width="@dimen/size_dialog"
        android:layout_height="@dimen/size_dialog"
        android:layout_centerInParent="true"
        android:background="@drawable/shape_background_assistivetouch"
        android:orientation="vertical" >  

        <RelativeLayout
            android:id="@+id/lay_main"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="4.0px"
            android:visibility="visible" >  

            <TextView
                android:id="@+id/btn_apps"
                style="@style/Icon"
                android:layout_centerInParent="true"
                android:drawableTop="@drawable/selector_ic_apps"
                android:text="@string/apps" />  

            <TextView
                android:id="@+id/btn_home_screen"
                style="@style/Icon"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:drawableTop="@drawable/selector_ic_home"
                android:text="@string/home_screen" />  

            <TextView
                android:id="@+id/btn_setting"
                style="@style/Icon"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:drawableTop="@drawable/selector_ic_phone"
                android:text="@string/setting" />  

            <TextView
                android:id="@+id/btn_lock_screen"
                style="@style/Icon"
                android:layout_centerHorizontal="true"
                android:drawableTop="@drawable/selector_ic_power_down"
                android:text="@string/lock_screen" />  

            <TextView
                android:id="@+id/btn_favor"
                style="@style/Icon"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:drawableTop="@drawable/selector_ic_star"
                android:text="@string/favor" />
        </RelativeLayout>
    </LinearLayout>  

</RelativeLayout> 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tyd.floatball"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="14" />  

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:name=".util.MyApplication">
        <activity
            android:label="@string/app_name"
            android:name=".ui.MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />  

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
                android:name=".service.TopFloatService"
                android:enabled="true"
                android:exported="true"
            />
    </application>  

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
</manifest>

该实例我已经将源码整理打包,进行了上传,下面是资源的下载地址:

http://download.csdn.net/detail/wulianghuan/5364129

仿iphone快速导航悬浮球的更多相关文章

  1. android悬浮球实现各种功能、快速开发框架、单词、笔记本、应用市场应用等源码

    Android精选源码 悬浮球,实现一键静音,一键锁频,一键截屏等功能 一个Android快速开发框架,MVP架构 Android QQ小红点的实现源码 android一款单词应用完整app源码 an ...

  2. html5悬浮球效果

    自己想做一个自己的网站,觉得自适应的效果会好一点,但是放到手机端的话,菜单显示是个问题.所以自己试着写了一个悬浮球菜单的效果. 好了,上代码. 这里有四个文件要用: jqurey.js//因为基于jq ...

  3. Android 悬浮窗、悬浮球开发

    原文:Android 悬浮窗.悬浮球开发 1.权限管理 直接看我另外一篇博客吧,传送门: https://my.oschina.net/u/1462828/blog/1933162 2.Base类Ba ...

  4. 仿iphone日历插件(beta)

    前言 小伙伴们好,很久不见了.最近工作进入正常期了,所以慢慢的悠闲的时间久没有了,所以不能每天水一篇了. 最近也在听师傅(http://home.cnblogs.com/u/aaronjs/)的教导开 ...

  5. 隐藏左侧快速导航除DMS导航树之外的其他区域

    <style type="text/css"> /*隐藏左侧快速导航除DMS导航树之外的其他区域*/ .ms-quicklaunchouter { display: n ...

  6. 高仿114la网址导航源码完整最新版

    给大家本人我精心模仿的高仿114la网址导航源码,我们都知道114la网址导航的影响力,喜欢的朋友可以下载学习一下.  由于文件较大,没有上传了,下载地址在下面有的. 附源码下载: 114la网站导航 ...

  7. Bootstrap导航悬浮顶部,stickUp

    stickUp 一个 jQuery 插件 这是一个简单的jQuery插件,它能让页面目标元素 “固定” 在浏览器窗口的顶部,即便页面在滚动,目标元素仍然能出现在设定的位置.此插件可以在多页面的网站上工 ...

  8. Confluence 6 如何配置快速导航的同时查找数量

    进入后台后查看快速导航的启用和可以同时查找的数量. 然后进行通过单击右上角的编辑(Edit)按钮进行编辑. 对配置进行配置,启用快速查询和可以同时使用的最大查询数量. https://www.cwik ...

  9. Confluence 6 配置快速导航

    当在 Confluence 中的快速导航进行查找的时候(请查看 Searching Confluence)能够帮助你显示页面下拉列表和其他的项目,这个是通过查找页面标题进行比对的.在默认情况下,这个功 ...

随机推荐

  1. Zend引擎探索 之 PHP中前置递增不返回左值

    首先来讲,一般我们对"左值"的理解就是可以出现在赋值运算符的左侧的标识符,也就是可以被赋值.这样讲也许并不十分确切,在不同的语言中对左值的定义也不尽相同.在这里我们讨论前置递增(和 ...

  2. RxSwift 系列(八) -- Error Handing Operators

    前言 本篇文章我们将学习RxSwift中的错误处理,包括: catchErrorJustReturn catchError retry retry(_:) catchErrorJustReturn 遇 ...

  3. 函数的形参和实参之arguments对象

    当函数调用函数时候传入的实参比函数声明时候制定的形参要少时候,剩余的形参就设置成了undefined.例如 function getPropertyNames(o,/*optional*/a){ va ...

  4. Python小代码_11_生成小于 n 的裴波那契数列

    def fib(n): a, b = 1, 1 while a < n: print(a, end=' ') a, b = b, a + b fib(100000) #输出结果 #1 1 2 3 ...

  5. C# ref与out

    ref参数是引用,out参数为输出参数.我写一个控制台的程序来说明一下两者的特点和区别: class Program { 3 public static void RefMethod( ref int ...

  6. 解读Raft(二 选举和日志复制)

    Leader election Raft采用心跳机制来触发Leader选举.Leader周期性的发送心跳(如果有正常的RPC的请求情况下可以不发心跳)包保持自己Leader的角色(避免集群中其他节点认 ...

  7. FJUT寒假作业涨姿势题解

    题意非常简单易懂,对于涨姿势0,数据非常小,比较容易想到的是直接循环暴力解题完成任务.把数据放入数组arr,循环i,j控制所有区间算和.结果记入vis. 到了涨姿势1,2,3,我们观察数据变化,发现数 ...

  8. 无需密码通过scp命令+key的方式实现文件传输

    如果觉得scp每次都要输入密码很麻烦, 那么这是解决方案.假设你平时在windows上开发,用户名是xiang, 你有一台Ubuntu服务器wdksw.com, 用户名是root.现在你准备上传一些文 ...

  9. Apache shiro集群实现 (四)shiro授权(Authentication)--访问控制

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  10. 高通msm8994性能及温度监测脚本

    [plain] view plain copystartTime=$(date +%Y-%m-%d-%H-%M-%S)  pathName="/data/cpu_logs"  fi ...