博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android窗口浮在屏幕上效果
阅读量:7229 次
发布时间:2019-06-29

本文共 4271 字,大约阅读时间需要 14 分钟。

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btn = (Button) findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                funClick(null);            }        });    }    public void funClick(View v) {        // 按钮被点击        this.startService(new Intent(this, Mser.class));        // new TableShowView(this).fun(); 如果只是在activity中启动        // 当activity跑去后台的时候[暂停态,或者销毁态] 我们设置的显示到桌面的view也会消失        // 所以这里采用的是启动一个服务,服务中创建我们需要显示到table上的view,并将其注册到windowManager上        this.finish();    }}
View Code

 

import android.app.Service;import android.content.Intent;import android.os.IBinder;public class Mser extends Service {    @Override    public IBinder onBind(Intent intent) {        return null;    }    public void onCreate() {        // 创建service时一个 实例化一个TableShowView对象并且调用他的fun()方法把它注册到windowManager上        super.onCreate();        new TableShowView(this).fun();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        return super.onStartCommand(intent, flags, startId);    }}
View Code

 

import android.content.Context;import android.graphics.Color;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.widget.Toast;public class TableShowView extends View {    // 如果是想显示歌词则继承TextView并复写ondraw方法。    // 开启一个线程不断的调用ondraw方法去更改你所写的继承自TextView的内容    // 这里随便写了个集成自view的= =这个不是重点    Context c;    WindowManager mWM;      WindowManager.LayoutParams mWMParams;     View win;    int tag = 0;    int oldOffsetX;    int oldOffsetY;    public TableShowView(Context context) {        super(context);        c = context;    }    public void fun() {        // 设置载入view WindowManager参数        mWM = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);        win = LayoutInflater.from(c).inflate(R.layout.ctrl_window, null);        win.setBackgroundColor(Color.TRANSPARENT);        // 这里是随便载入的一个布局文件        win.setOnTouchListener(new OnTouchListener() {            // 触屏监听            float lastX, lastY;            public boolean onTouch(View v, MotionEvent event) {                final int action = event.getAction();                float x = event.getX();                float y = event.getY();                if (tag == 0) {                    oldOffsetX = mWMParams.x; // 偏移量                    oldOffsetY = mWMParams.y; // 偏移量                }                if (action == MotionEvent.ACTION_DOWN) {                    lastX = x;                    lastY = y;                } else if (action == MotionEvent.ACTION_MOVE) {                    mWMParams.x += (int) (x - lastX); // 偏移量                    mWMParams.y += (int) (y - lastY); // 偏移量                    tag = 1;                    mWM.updateViewLayout(win, mWMParams);                }                else if (action == MotionEvent.ACTION_UP) {                    int newOffsetX = mWMParams.x;                    int newOffsetY = mWMParams.y;                    if (oldOffsetX == newOffsetX && oldOffsetY == newOffsetY) {                        Toast.makeText(c, "你点到我了……疼!!!!", 1).show();                    } else {                        tag = 0;                    }                }                return true;            }        });        WindowManager wm = mWM;        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();        mWMParams = wmParams;        wmParams.type = 2003; // type是关键,这里的2003表示系统级窗口        wmParams.flags = 40;// 这句设置桌面可控        wmParams.width = 50;        wmParams.height = 50;        wmParams.format = -3; // 透明        wm.addView(win, wmParams);// 这句是重点 给WindowManager中丢入刚才设置的值                                    // 只有addview后才能显示到页面上去。        // 注册到WindowManager win是要刚才随便载入的layout,wmParams是刚才设置的WindowManager参数集        // 效果是将win注册到WindowManager中并且它的参数是wmParams中设置饿    }}
View Code

DEMO完整路径下载:

转载地址:http://cgbfm.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
我的友情链接
查看>>
mariadb安装
查看>>
vue+vuex+axios+echarts画一个动态更新的中国地图
查看>>
5.8 volumetric post-processing--game programming gems5 笔记
查看>>
8086的地址空间
查看>>
Android开发动画效果被遮掉的解决方法
查看>>
Apache2.2.17源码编译安装以及配置虚拟主机
查看>>
2017年开发语言排名
查看>>
读二进制表的显示 Binary Watch
查看>>
我的友情链接
查看>>
linux基础:10、基础命令(4)
查看>>
linux中强大的screen命令
查看>>
放开那个程序员
查看>>
构建高性能数据库缓存之Redis(一)
查看>>
测试驱动开发
查看>>
解决MySQL不允许从远程访问
查看>>
puppet介绍及基于httpd实例部署
查看>>
UML常用工具之三--RSA
查看>>
iis7 appcmd的基础命令及简单用法
查看>>