android 悬浮窗口
来源:互联网 发布:java程序员为什么转行 编辑:程序博客网 时间:2024/05/01 03:32
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>
- Android中悬浮窗口
- android 悬浮窗口
- android 悬浮窗口
- Android悬浮窗口基本知识
- android 悬浮窗口
- Android中悬浮窗口
- android 悬浮窗口
- Android 中悬浮窗口
- Android中悬浮窗口
- android 悬浮窗口
- Android悬浮窗口
- Android 悬浮窗口
- Android中悬浮窗口
- Android 悬浮窗口
- 实现android悬浮窗口
- android悬浮窗口
- Android实现悬浮窗口
- android 桌面悬浮窗口
- erlang网络编程的几个性能调优和注意点
- 创建没有标题的UIAlertView,创建没有按钮的UIAertView
- 《软件调试》学习笔记——004 (第一章 软件调试基础 part2)
- 使用InternetSetCookie操作cookie
- Linux命令详解 — time
- android 悬浮窗口
- 免费的晚餐--Google技术学习
- 成长的轨迹——linux
- 实习第一天
- 深入Cloud Foundry
- POI 样例
- VB mschart控件的使用
- wifi无线网卡 移植
- 用JFreeChart 生成报表