说起悬浮框,估计有人想起360安全卫士的那个桌面悬浮框,也有人会说UC浏览器浏览网页时那里也有一个手势的悬浮框,是的。今天我在这里主要讲UC的这样,基于某个应用内的,而不是基于桌面的。
实现不难,先让我们瞧瞧布局先:
悬浮框的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/float_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
</RelativeLayout>
首先,我们需要取得LayoutParms的对象mParams,使用mParams对象来设置windowManager的属性需要设置的参数有type、format、flags、gravty、起始坐标等,然后使用LayoutIflater来找到悬浮框布局,最后使用mWindowManager.addView(mLayout, mParams);来把布局添加到界面上。当然,我们可以设置它为移动或者固定,具体方法通过LayoutParms的flags属性来设置,该属性有FLAG_NOT_TOUCH_MODAL、FLAG_NOT_FOCUSABLE、FLAG_NOT_TOUCHABLE三种值。
package com.hjhrq1991.floatwindowdemo;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
*
* @Filename:floatwindowdemo→com.hjhrq1991.floatwindowdemo→MainActivity.java
* @Description:悬浮窗口演示demo
*
* @author:hjhrq1991
* @version:v1.0 2014-9-14 上午1:43:40
*/
public class MainActivity extends Activity implements OnTouchListener,
OnClickListener {
private WindowManager mWindowManager;
private LayoutParams wmParams;
private RelativeLayout mLayout;
private ImageView mView;
private int width, height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 获取LayoutParams对象
wmParams = new WindowManager.LayoutParams();
// 获取的是LocalWindowManager对象
mWindowManager = this.getWindowManager();
width = mWindowManager.getDefaultDisplay().getWidth();
height = mWindowManager.getDefaultDisplay().getHeight();
initView();
}
/**
*
* @Description: 初始化悬浮窗口
* @param
* @return void
* @throws
*/
private void initView() {
// 设置window type
wmParams.type = LayoutParams.TYPE_PHONE;
// 设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
//
// flags属性: 不可触摸|不接受任何事件|不影响后面的事件响应
// FLAG_NOT_TOUCH_MODAL|FLAG_NOT_FOCUSABLE|FLAG_NOT_TOUCHABLE
wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
// 设置参考坐标,建议使用左上角。
wmParams.gravity = Gravity.TOP | Gravity.LEFT;
// 设置起始位置
wmParams.x = 2 * width / 3;
wmParams.y = 2 * height / 3;
// 设置宽长参数
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
LayoutInflater inflater = this.getLayoutInflater();
mLayout = (RelativeLayout) inflater
.inflate(R.layout.float_layout, null);
mWindowManager.addView(mLayout, wmParams);
mView = (ImageView) mLayout.findViewById(R.id.float_view);
mView.setOnTouchListener(this);
mView.setOnClickListener(this);
}
/**
* 触摸事件
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
wmParams.x = (int) event.getRawX() - mLayout.getWidth() / 2;
wmParams.y = (int) event.getRawY() - mLayout.getHeight() / 2 - 50;
mWindowManager.updateViewLayout(mLayout, wmParams);
return false;
}
/**
* 点击事件
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "可以设置点击事件", Toast.LENGTH_SHORT)
.show();
}
}
代码写完了不代表就可以立马跑起来,现在跑起来必定会崩溃,因为我们需要在manifest.xml里添加权限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />添加完后就可以跑起来了
如有转载,请注明出处:http://blog.csdn.net/hjhrq1991