Android中加入悬浮窗
2013-10-30 15:57
239 查看
悬浮窗可以一直在前台显示一些信息,无论当前的Activity是哪个应用的。比如显示当前的上下行网速等
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.IBinder;
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;
import android.view.WindowManager.LayoutParams;
/**
* 悬浮窗Service 该服务会在后台一直运行一个悬浮的透明的窗体。
*
* @author
*
*/
public class FloatingService extends Service {
private int statusBarHeight;// 状态栏高度
private View view;// 透明窗体
private boolean viewAdded = false;// 透明窗体是否已经显示
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
view = LayoutInflater.from(this).inflate(R.layout.floating, null);
windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
/*
* LayoutParams.TYPE_SYSTEM_ERROR:保证该悬浮窗所有View的最上层
* LayoutParams.FLAG_NOT_FOCUSABLE:该浮动窗不会获得焦点,但可以获得拖动
* PixelFormat.TRANSPARENT:悬浮窗透明
*/
layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_ERROR,
LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
// layoutParams.gravity = Gravity.RIGHT|Gravity.BOTTOM; //悬浮窗开始在右下角显示
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
view.setOnTouchListener(new OnTouchListener() {
float[] temp = new float[] { 0f, 0f };
public boolean onTouch(View v, MotionEvent event) {
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN: // 按下事件,记录按下时手指在悬浮窗的XY坐标值
temp[0] = event.getX();
temp[1] = event.getY();
break;
case MotionEvent.ACTION_MOVE:
refreshView((int) (event.getRawX() - temp[0]), (int) (event
.getRawY() - temp[1]));
break;
}
return true;
}
});
}
/**
* 刷新悬浮窗
*
* @param x
* 拖动后的X轴坐标
* @param y
* 拖动后的Y轴坐标
*/
public void refreshView(int x, int y) {
//状态栏高度不能立即取,不然得到的值是0
if(statusBarHeight == 0){
View rootView = view.getRootView();
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
statusBarHeight = r.top;
}
layoutParams.x = x;
// y轴减去状态栏的高度,因为状态栏不是用户可以绘制的区域,不然拖动的时候会有跳动
layoutParams.y = y - statusBarHeight;//STATUS_HEIGHT;
refresh();
}
/**
* 添加悬浮窗或者更新悬浮窗 如果悬浮窗还没添加则添加 如果已经添加则更新其位置
*/
private void refresh() {
if (viewAdded) {
windowManager.updateViewLayout(view, layoutParams);
} else {
windowManager.addView(view, layoutParams);
viewAdded = true;
}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
refresh();
}
/**
* 关闭悬浮窗
*/
public void removeView() {
if (viewAdded) {
windowManager.removeView(view);
viewAdded = false;
}
}
@Override
public void onDestroy() {
super.onDestroy();
removeView();
}
class StatusBarReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//intent.get
}
}
}
floating.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- android:updatePeriodMillis="10000"> -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/floating"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="http://blog.163.com/hero_213/blog/@drawable/upload"/>
<TextView
android:id="@+id/flowing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="right"
android:text="0K/s"
android:textColor="#000000"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="http://blog.163.com/hero_213/blog/@drawable/download"/>
<TextView
android:gravity="right"
android:id="@+id/flowspeed"
android:paddingRight="10.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="17K/s"
android:textColor="#000000"
/>
</LinearLayout>
<ImageButton
android:id="@+id/floating_button_hide"
android:background="@drawable/floatingwindowhidebutton"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="\?android:buttonStyleSmall" />
</LinearLayout>
最后还要在AndroidManifest.xml加一个权限
<!-- 悬浮窗(FloatingService) permission -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
相关文章推荐
- Android 系统拨号界面实现自定义悬浮布局
- Android悬浮缩放菜单的实现
- Android 沉浸式状态栏及悬浮效果
- Android 对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果
- [android]第一篇blog 关于android中加入外界的db文件进行数据库访问
- Android的输入框中加入清除按钮
- 如何在Android应用中加入广告
- Android Stuido加入Kotlin插件和Anko插件后,启动报错 Cannot load project: com.intellij.ide.plugins.PluginManager$St
- Android中可移动悬浮窗口 WindowManager详解
- Android第三方sdk加入App微信支付解决方案
- android源码下加入替换铃声文件,及设置默认铃声
- Android应用加入微信分享功能
- Android中悬浮窗口的实现原理和示例代码
- android客户端同时加入wifi定位
- Android图片滚动,加入自动播放功能,使用自定义属性实现,霸气十足!
- Android 可拖动的悬浮按钮
- Android 对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果
- android中加入广告篇一