Android中将Window分成多个级别。要想实现桌面歌词效果只要将Window的级别高于桌面的Window级别就行了,同时也具备可自由移动的悬浮窗口效果。下面看看代码:
package com.orgcent.desktop;
import
Android.app.Application;
import
Android.content.Context;
import
Android.view.LayoutInflater;
import
Android.view.MotionEvent;
import
Android.view.View;
import
Android.view.WindowManager;
import
Android.view.View.OnTouchListener;
public class BaseAppliction extends Application {
WindowManager mWM;
WindowManager.LayoutParams mWMParams;
@Override
public void onCreate() {
mWM =(WindowManager)getSystemService(Context.WINDOW_SERVICE);
final View win = LayoutInflater.from(this).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 = 2002; //type是关键,这里的2002表示系统级窗口,你也可以试试2003。
wmParams.format=1;
wmParams.flags= 40;
wmParams.width = 300;
wmParams.height = 200;
wm.addView(win, wmParams);
}
}
<LinearLayout xmlns:
Android="http://schemas.
Android.com/apk/res/
Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:background="#ffffff"
Android:orientation="vertical"
Android:padding="5dp" >
<TextView
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:gravity="center"
Android:textColor="#000000"
Android:text="我爱加上双方随即封锁酸辣粉丝激发了
http://orgcent.com"/>
</LinearLayout>
Demo下载地址:
http://code.google.com/p/android-custom-view/downloads/list