android中悬浮显示可拖动的工具栏

jopen 10年前

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

MainActivity:

    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();            }                }  
</div> </div>

MyTextView:

    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(100, 140, 160, 150));            }                    @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(0, 0, len, 0, new 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, 0, 10, p);            }                    /**            * 更新浮动窗口位置参数            */            private void updatePosition() {                // View的当前位置                params.x = (int) (x - startX);                params.y = (int) (y - startY);                wm.updateViewLayout(this, params);            }        }  
</div> </div>

布局xml:

    <?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>  
</div> </div>

权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />    <uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />  
</div> </div> 来自:http://blog.csdn.net/u010142437/article/details/17509587