Android实现桌面歌词(类似TTPlayer)
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
摘自 萝卜白菜的博客
补充:移动开发 , Android ,