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
摘自 蘿卜白菜的博客