![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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(); }}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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); }}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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中设置饿 }}