前两天看到部分音频播放器可以实现在桌面上显示歌词,360那个浮动的桌面提示,想不到怎么实现。然后查了些资料大致了解了下,比想象的简单多了。先看效果图吧。
白色的字体是我加入进去的,此时是可以允许切换背景的。且按住白色背景可以拖动。
其实主要就是注册一个view到windowsManager上去,然后对它的参数配置成可显示到桌面上的参数就可以了。
下面是代码
activity
就是简单的一个按钮 layout就不写了
package com.cfuture.desktop;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class AndroidDesktopActivity extends Activity {
//一个有只有一个按钮的activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void funClick(View v){
//按钮被点击
this.startService(new Intent(this,Mser.class));
// new TableShowView(this).fun(); 如果只是在activity中启动
// 当activity跑去后台的时候[暂停态,或者销毁态] 我们设置的显示到桌面的view也会消失
// 所以这里采用的是启动一个服务,服务中创建我们需要显示到table上的view,并将其注册到windowManager上
this.finish();
}
}
然后是一个服务
package com.cfuture.desktop;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class Mser extends Service {
//服务
//这个类纯蛋疼用 只是为了在activity点击button后 在开启一个service
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
}
最后是一个复写的view其实不复写view 也行 总之是能传入context对象就可以的了 不过如果想要实现歌词那种或者可以动态改变内容的 就要复写view分支的类了。
TableShowView
package com.cfuture.desktop;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class TableShowView extends View {
//如果是想显示歌词则继承TextView并复写ondraw方法。
//开启一个线程不断的调用ondraw方法去更改你所写的继承自TextView的内容
//这里随便写了个集成自view的= =这个不是重点
Context c;
WindowManager mWM;//WindowManager
WindowManager.LayoutParams mWMParams;//WindowManager参数
public TableShowView(Context context) {
// TODO Auto-generated constructor stub
super(context);
c = context;
}
public void fun() {
//设置载入view WindowManager参数
mWM = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
final View win = LayoutInflater.from(c).inflate(R.layout.ctrl_window,
null);
//这里是随便载入的一个布局文件
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 (action == MotionEvent.ACTION_DOWN) {
lastX = x;
lastY = y;
} else if (action == MotionEvent.ACTION_MOVE) {
mWMParams.x += (int) (x - lastX);
mWMParams.y += (int) (y - lastY);
mWM.updateViewLayout(win, mWMParams);
}
return true;
}
});
WindowManager wm = mWM;
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
mWMParams = wmParams;
wmParams.type = 2003; // type是关键,这里的2002表示系统级窗口,你也可以试试2003。
wmParams.flags = 40;// 这句设置桌面可控
wmParams.width = 300;
wmParams.height = 200;
wm.addView(win, wmParams);//这句是重点 给WindowManager中丢入刚才设置的值 只有addview后才能显示到页面上去。
//注册到WindowManager win是要刚才随便载入的layout,wmParams是刚才设置的WindowManager参数集
//效果是将win注册到WindowManager中并且它的参数是wmParams中设置饿
}
}
写的很杂 就酱子吧