android 悬浮窗口

来源:互联网 发布:java程序员为什么转行 编辑:程序博客网 时间:2024/05/01 03:32
package com.test;


import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;

public class FloatPop extends Activity {
    WindowManager wm = null;
    WindowManager.LayoutParams wmParams = null;
    View view;
    private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent service = new Intent();
        service.setClass(this, TopFloatService.class);        
        startService(service);
       // view = LayoutInflater.from(this).inflate(R.layout.floating, null);
        //createView();
    }
    
    private void createView() {
        // ��ȡWindowManager
        
        wm = (WindowManager) FloatPop.this.getSystemService("window");
        // ����LayoutParams(ȫ�ֱ�������ز���
        //wmParams =  ((ApplicationTest) getApplication()).getMywmParams();
         wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;// �������ṩ���û���������������Ӧ�ó����Ϸ���������״̬������
        wmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// �������κΰ����¼�
        wmParams.gravity = Gravity.LEFT | Gravity.TOP; // ������������Ͻ�
        // ����Ļ���Ͻ�Ϊԭ�㣬����x��y��ʼֵ
        wmParams.x = 0;
        wmParams.y = 0;
        // ������ڳ������
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.format = PixelFormat.RGBA_8888;

        wm.addView(view, wmParams);

        view.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                // ��ȡ�����Ļ����꣬������Ļ���Ͻ�Ϊԭ��
                x = event.getRawX();
                // 25��ϵͳ״̬���ĸ߶�,Ҳ����ͨ��õ�׼ȷ��ֵ���Լ�΢��������
                y = event.getRawY()-25 ;
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // ��ȡ���View����꣬���Դ�View���Ͻ�Ϊԭ��
                    mTouchStartX = event.getX();
                    mTouchStartY = event.getY()+view.getHeight()/2;
                    break;
                case MotionEvent.ACTION_MOVE:
                    updateViewPosition();
                    break;
                case MotionEvent.ACTION_UP:
                    updateViewPosition();
                    mTouchStartX = mTouchStartY = 0;
                    break;
                }
                return true;
            }

        });
    }
    
    private void updateViewPosition() {
        // ���¸�������λ�ò���
        wmParams.x = (int) (x - mTouchStartX);
        wmParams.y = (int) (y - mTouchStartY);
        wm.updateViewLayout(view, wmParams);
    }

}





package com.test;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;

public class TopFloatService extends Service {

    WindowManager wm = null;
    WindowManager.LayoutParams wmParams = null;
    View view;
    private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    
    @Override
    public void onCreate() {
        super.onCreate();
        view = LayoutInflater.from(this).inflate(R.layout.floating, null);
        createView();
    }

    private void createView() {
        // ��ȡWindowManager
        
        wm = (WindowManager) TopFloatService.this.getSystemService("window");
        // ����LayoutParams(ȫ�ֱ�������ز���
        //wmParams =  ((ApplicationTest) getApplication()).getMywmParams();
         wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;// �������ṩ���û���������������Ӧ�ó����Ϸ���������״̬������
        wmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// �������κΰ����¼�
        wmParams.gravity = Gravity.LEFT | Gravity.TOP; // ������������Ͻ�
        // ����Ļ���Ͻ�Ϊԭ�㣬����x��y��ʼֵ
        wmParams.x = 0;
        wmParams.y = 0;
        // ������ڳ������
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.format = PixelFormat.RGBA_8888;

        wm.addView(view, wmParams);

        view.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                // ��ȡ�����Ļ����꣬������Ļ���Ͻ�Ϊԭ��
                x = event.getRawX();
                // 25��ϵͳ״̬���ĸ߶�,Ҳ����ͨ��õ�׼ȷ��ֵ���Լ�΢��������
                y = event.getRawY()-25 ;
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // ��ȡ���View����꣬���Դ�View���Ͻ�Ϊԭ��
                    mTouchStartX = event.getX();
                    mTouchStartY = event.getY()+view.getHeight()/2;
                    break;
                case MotionEvent.ACTION_MOVE:
                    updateViewPosition();
                    break;
                case MotionEvent.ACTION_UP:
                    updateViewPosition();
                    mTouchStartX = mTouchStartY = 0;
                    break;
                }
                return true;
            }

        });
    }
    
    private void updateViewPosition() {
        // ���¸�������λ�ò���
        wmParams.x = (int) (x - mTouchStartX);
        wmParams.y = (int) (y - mTouchStartY);
        wm.updateViewLayout(view, wmParams);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FloatPop"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<service android:name=".TopFloatService"/>
    </application>
</manifest>


原创粉丝点击