下面的示例是仿QQ音乐之类的歌词栏,可以进行拖动和隐藏,代码如下:

MainActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.home.danglingactivity; 
   
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.WindowManager.LayoutParams; 
import android.widget.Button; 
   
public class MainActivity extends Activity implements OnClickListener { 
    private MyTextView tv; 
    private Button btn; 
   
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_main); 
        btn = (Button) findViewById(R.id.main_btn); 
        btn.setOnClickListener(this); 
    
   
    @Override 
    public void onClick(View v) { 
        if (tv != null && tv.isShown()) { 
            hide(); 
        else
            show(); 
        
    
   
    /**
     * 隐藏显示歌词的TextView
     */ 
    private void hide() { 
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
        wm.removeView(tv); 
        btn.setText("显示歌词"); 
    
   
    /**
     * 显示悬浮的TextView
     */ 
    private void show() { 
        Rect frame = new Rect(); 
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
        MyTextView.TOOL_BAR_HIGH = frame.top; 
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
        WindowManager.LayoutParams params = MyTextView.params; 
   
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 
                | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; 
        params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL 
                | LayoutParams.FLAG_NOT_FOCUSABLE; 
        params.width = WindowManager.LayoutParams.FILL_PARENT; 
        params.height = WindowManager.LayoutParams.WRAP_CONTENT; 
        params.alpha = 80
        params.gravity = Gravity.LEFT | Gravity.TOP; 
        // 设置x、y初始值 
        params.x = 0
        params.y = wm.getDefaultDisplay().getHeight(); 
   
        tv = new MyTextView(MainActivity.this); 
        wm.addView(tv, params); 
        btn.setText("隐藏歌词"); 
    
   
    @Override 
    protected void onDestroy() { 
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
        if (tv != null && tv.isShown()) { 
            wm.removeView(tv); 
        
        super.onDestroy(); 
    
   
}

MyTextView:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.home.danglingactivity; 
   
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.LinearGradient; 
import android.graphics.Paint; 
import android.graphics.Shader; 
import android.graphics.Shader.TileMode; 
import android.graphics.Typeface; 
import android.view.MotionEvent; 
import android.view.WindowManager; 
import android.widget.TextView; 
   
public class MyTextView extends TextView { 
    private final String TAG = MyTextView.class.getSimpleName(); 
    public static int TOOL_BAR_HIGH = 0
    public static WindowManager.LayoutParams params = new WindowManager.LayoutParams(); 
    private float startX; 
    private float startY; 
    private float x; 
    private float y; 
    private String text = "我是不是你最疼爱的人,你为什么不说话,握住是你冰冷的手动也不动让我好难过";; 
    private float float1 = 0.0f; 
    private float float2 = 0.01f; 
    WindowManager wm = (WindowManager) getContext().getApplicationContext() 
            .getSystemService(Context.WINDOW_SERVICE); 
   
    public MyTextView(Context context) { 
        super(context); 
        this.setBackgroundColor(Color.argb(100140160150)); 
    
   
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        // 触摸点相对于屏幕左上角坐标 
        x = event.getRawX(); 
        y = event.getRawY() - TOOL_BAR_HIGH; 
        switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
            startX = event.getX(); 
            startY = event.getY(); 
            break
        case MotionEvent.ACTION_MOVE: 
            updatePosition(); 
            break
        case MotionEvent.ACTION_UP: 
            updatePosition(); 
            startX = startY = 0
            break
        
        return true
    
   
    @Override 
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas); 
        float1 += 0.001f; 
        float2 += 0.001f; 
        if (float2 > 1.0) { 
            float1 = 0.0f; 
            float2 = 0.01f; 
        
        this.setText(""); 
        float len = this.getTextSize() * text.length(); 
        Shader shader = new LinearGradient(00, len, 0new int[] { 
                Color.YELLOW, Color.RED }, new float[] { float1, float2 }, 
                TileMode.CLAMP); 
        Paint p = new Paint(); 
        p.setShader(shader); 
        p.setTypeface(Typeface.DEFAULT_BOLD); 
        canvas.drawText(text, 010, p); 
    
   
    /**
     * 更新浮动窗口位置参数
     */ 
    private void updatePosition() { 
        // View的当前位置 
        params.x = (int) (x - startX); 
        params.y = (int) (y - startY); 
        wm.updateViewLayout(this, params); 
    
}

布局xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"
   
    <Button 
        android:id="@+id/main_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:text="显示歌词" /> 
   
</LinearLayout>

权限:

1
2
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
<uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />

来自:http://blog.csdn.net/u010142437/article/details/17509587

創作者介紹

資訊園

shadow 發表在 痞客邦 PIXNET 留言(0) 人氣()