悬浮窗2种方式

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/start"/>
    <Button 
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/stop"/>

</LinearLayout>

float_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:orientation="vertical" >
    

    <Button 
        android:id="@+id/float_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/floating"/>
</LinearLayout>

result.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" >
    

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
</LinearLayout>


FloatWindowTest

package com.phicomm.hu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class FloatWindowTest extends Activity {

	private static final String TAG = "FloatWindowTest";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 移动、移除悬浮窗
		Button start = (Button) findViewById(R.id.start);
		Button stop = (Button) findViewById(R.id.stop);
		/**
		 * 启动
		 */
		start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				createFloatView();
			}
		});
		/**
		 * 移除
		 */
		stop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mFloatLayout != null) {
					mWindowManager.removeView(mFloatLayout);
					finish();
				}
			}
		});

	}

	/**
	 * 启动悬浮窗
	 */
	WindowManager mWindowManager;
	WindowManager.LayoutParams wmParams;

	// 悬浮窗布局
	LinearLayout mFloatLayout;
	// 悬浮窗的button
	Button mFloatView;

	private void createFloatView() {
		// 获取LayoutParams对象
		wmParams = new WindowManager.LayoutParams();

		// 获取的是LocalWindowManager对象
		mWindowManager = this.getWindowManager();
		// 设置window type
		wmParams.type = LayoutParams.TYPE_PHONE;
		// 设置图片格式,效果为背景透明
		wmParams.format = PixelFormat.RGBA_8888;
		// 该浮动窗不会获得焦点,但可以获得拖动
		wmParams.flags = 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;

		LayoutInflater inflater = this.getLayoutInflater();
		// 悬浮窗展现的布局
		mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout,
				null);
		/**
		 * view The view to be added to this window. params The LayoutParams to
		 * assign to view.
		 */
		mWindowManager.addView(mFloatLayout, wmParams);
		mFloatView = (Button) mFloatLayout.findViewById(R.id.float_id);

		/**
		 * 绑定触摸移动监听
		 */
		mFloatView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				wmParams.x = (int) event.getRawX() - mFloatLayout.getWidth()
						/ 2;
				// 25为状态栏高度
				wmParams.y = (int) event.getRawY() - mFloatLayout.getHeight()
						/ 2 - 40;
				mWindowManager.updateViewLayout(mFloatLayout, wmParams);
				return false;
			}
		});

		// 绑定点击监听
		mFloatView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FloatWindowTest.this,
						ResultActivity.class);
				startActivity(intent);
			}
		});

	}
}

ResultActivity(点击悬浮窗跳转到的界面)

package com.phicomm.hu;

import android.app.Activity;
import android.os.Bundle;

public class ResultActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
	}

}
**************************************************第二种方法************************************************

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/start_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn"/>
    <Button 
        android:id="@+id/remove_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stop"
        />
</LinearLayout>

float_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <Button 
        android:id="@+id/float_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/floating"/>
    
</LinearLayout>

MainActivity

package com.phicomm.hu;

import java.lang.reflect.Method;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	// 创建浮动窗口设置布局参数的对象

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//创建移除悬浮框
		Button start = (Button) findViewById(R.id.start_id);
		Button remove = (Button) findViewById(R.id.remove_id);
		/**
		 * 创建悬浮框
		 */
		start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, FxService.class);
				startService(intent);
				finish();
			}
		});
		/**
		 * 移除悬浮框
		 */
		remove.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, FxService.class);
				stopService(intent);
			}
		});

	}


}

FxService

package com.phicomm.hu;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class FxService extends Service {

	// 定义浮动窗口布局
	LinearLayout mFloatLayout;
	WindowManager.LayoutParams wmParams;
	// 创建浮动窗口设置布局参数的对象
	WindowManager mWindowManager;
	//悬浮窗布局的按钮
	Button mFloatView;

	@Override
	public void onCreate() {
		super.onCreate();
		createFloatView();
	}

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

	/**
	 * 创建悬浮框
	 */
	private void createFloatView() {
		wmParams = new WindowManager.LayoutParams();
		// 获取WindowManagerImpl.CompatModeWrapper
		mWindowManager = (WindowManager) getApplication().getSystemService(
				getApplication().WINDOW_SERVICE);
		// 设置window type
		wmParams.type = LayoutParams.TYPE_PHONE;
		// 设置图片格式,效果为背景透明
		wmParams.format = PixelFormat.RGBA_8888;
		// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
		wmParams.flags =
		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;

		LayoutInflater inflater = LayoutInflater.from(getApplication());
		// 获取浮动窗口视图所在布局
		mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout,
				null);
		// 添加mFloatLayout
		mWindowManager.addView(mFloatLayout, wmParams);

		// 浮动窗口按钮
		mFloatView = (Button) mFloatLayout.findViewById(R.id.float_id);

		mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
				.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		// 设置监听浮动窗口的触摸移动
		mFloatView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
				wmParams.x = (int) event.getRawX()
						- mFloatView.getMeasuredWidth() / 2;
				// 25为状态栏的高度
				wmParams.y = (int) event.getRawY()
						- mFloatView.getMeasuredHeight() / 2 - 25;
				// 刷新
				mWindowManager.updateViewLayout(mFloatLayout, wmParams);
				return false;
			}
		});

		mFloatView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(FxService.this, "onClick", Toast.LENGTH_SHORT)
						.show();
			}
		});
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mFloatLayout != null) {
			mWindowManager.removeView(mFloatLayout);
		}
	}

}




  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
该版本为目前网上最新版本。PKM2 是基于内容的个人知识管理系统,它可以将您看到的所有文字、图片信息全部转储为 HTML 格式文档保存到数据库中。这些信息包括:你的笔记、网上的网页内容、本地机器里的文档内容。PKM2 将这些资料全部保存到你的项目中进行管理,不会因系统或软件崩溃丢失数据。 PKM2 可以帮助您进行基于内容的采集、编辑、整理、检索、发布,为您的个人知识管理提供有力支持。 PKM2 适用于程序员、研究者、学生、信息专家、以及以互联网作为主要信息来源的网上阅读者、信息搜集者和信息发布者。 PKM2 的优点: 便携性:PKM2 是一款 绿色软件,您可以将它放在 U 盘或移动硬盘中,作为便携式个人知识库,成为您的“外脑”; 易用性:PKM2 是一款傻瓜式的软件,主要功能均通过拖放操作。网上的文字和图片等内容可以通过拖放到悬浮窗保存,目录的分类可以通过拖放重组,文章的分类也可以通过批量拖放重新分类;附件也可以通过向附件框拖放批量导入; 安全性:所有数据均保存在软件的 PROJECTS 目录的各个子项目中,拷入、拷出相应文件夹即可完成备份和恢复; 交互性:可以方便地进行数据的导入与导出。网上的页面数据和本地的文档(HTML、DOC、RTF、TEXT 等)都可存入或导入 PKM2。PKM2 中的数据可以导出为 HTML、DOC,或发布为 CHM 电子书、EXE 电子书、或直接发布为 WEB 系统,在网站上做内容发布。; 规范性:PKM2 的文档数据基于都柏林核心元数据集中十个元素(资源标识符、标题 、作者、关键词、分类、备注、创建者、创建日期、修改日期、资料来源)对资料进行标引,并在编辑器中集成了标引工具,对标题、作者、关键词和备注进行半自动标引; 开放性:PKM2 采用 HTML 标准管理资料,它将所有文件,转换为 HTML 格式,进行统一管理。基于HTML,用户可以按照统一的方式,编辑,管理文件。同时,由于 HTML 的 开放性,也使得用户可以方便的进行二次开发; 通用性:PKM2 采用 MS ACCESS 数据库,只要采用 WINDOWS 系统即可使用本系统,同时 ACCESS 也是目前通用性较好的数据库之一,使用常见的数据库转换工具可以将 ACCESS 数据库中的数据方便地转入其他数据库中。 PKM2 的缺点: 如果说 PKM2 有缺点,勿宁说定位和来源使然。它最初是作者开发自用的,因此,个性化和追求 DIY 的用户会发现有些不适应或不灵活。作者在收到 soberGGG 的一些反馈后说:以前从来没想到有人这样用软件(比如各快捷键)。但是,PKM2 在非常快速而不失方向地进行着更新,不断吸收着用户的反馈。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值