Float Window

出处:http://www.jb51.net/article/32321.htm


用过新版本android 360手机助手都人都对 360中只在桌面显示一个小小悬浮窗口羡慕不已吧? 
其实实现这种功能,主要有两步: 
1.判断当前显示的是为桌面。这个内容我在前面的帖子里面已经有过介绍,如果还没看过的赶快稳步看一下哦。 
2.使用windowManager往最顶层添加一个View 
.这个知识点就是为本文主要讲解的内容哦。在本文的讲解中,我们还会讲到下面的知识点: 
a.如果获取到状态栏的高度 
b.悬浮窗口的拖动 
c.悬浮窗口的点击事件 

FloatView 代码:

import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;

public class FloatView extends ImageView {
	private final static String TAG = "FloatView";
	private float mTouchX;
	private float mTouchY;
	private float x;
	private float y;
	private float mStartX;
	private float mStartY;
	private OnClickListener mClickListener;
	private WindowManager windowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
	// this windowManagerParams is system variable, to save window params.
	private WindowManager.LayoutParams windowManagerParams = ((FloatApplication) getContext().getApplicationContext()).getWindowParams();

	public FloatView(Context context) {
		super(context);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// Get the status bar height.
		Rect frame = new Rect();
		getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		Log.e(TAG,"statusBarHeight:" + statusBarHeight);
		// get the x and y, base on left-top point.
		x = event.getRawX();
		y = event.getRawY() - statusBarHeight; // statusBarHeight is the height of system status bar.
		Log.e(TAG, "currX" + x + "====currY" + y);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: // get the motion of finger
			// get the view x&y, base on left-top point
			mTouchX = event.getX();
			mTouchY = event.getY();
			mStartX = x;
			mStartY = y;
			Log.e(TAG, "startX" + mTouchX + "====startY" + mTouchY);
			break;
		case MotionEvent.ACTION_MOVE: // get the motion of finger.
			updateViewPosition();
			break;
		case MotionEvent.ACTION_UP: // get the motion of finger leave.
			updateViewPosition();
			mTouchX = mTouchY = 0;
			if ((x - mStartX) < 5 && (y - mStartY) < 5) {
				if (mClickListener != null) {
					mClickListener.onClick(this);
				}
			}
			break;
		}
		return true;
	}

	@Override
	public void setOnClickListener(OnClickListener l) {
		this.mClickListener = l;
	}

	private void updateViewPosition() {
		// update the window position
		windowManagerParams.x = (int) (x - mTouchX);
		windowManagerParams.y = (int) (y - mTouchY);
		windowManager.updateViewLayout(this, windowManagerParams); // refresh to show.
	}
}


代码解释: 
int statusBarHeight = frame.top; 
为获取状态栏的高度,为什么在event.getRawY()的时候减去状态栏的高度呢? 
因为我们的悬浮窗口不可能显示到状态栏中去,而后getRawY为获取到屏幕原点的距离。当我们屏幕处于全屏模式时,获取到的状态栏高度会变成0 
(x - mStartX) < 5 && (y - mStartY) < 5 
如果我们在触摸过程中,移动距离少于5 ,则视为点击,触发点击的回调。 
另外我们需要自定义一个application: 
import android.app.Application;
import android.view.WindowManager;

public class FloatApplication extends Application {
	private WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();

	public WindowManager.LayoutParams getWindowParams() {
		return windowParams;
	}
}

代码解释: 
自定义application的目的是为了保存windowParams的值 ,因为我们在拖动悬浮窗口的时候,如果每次都重新new一个layoutParams的话,在update 
的时候会在异常发现。 
windowParams的值也不一定非得在自定义application里面来保存,只要是全局的都行。 
最后我们再来看看Activity中的实现:

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

/**
 * @author torv
 *
 */
public class MainActivity extends Activity implements OnClickListener {
	private WindowManager windowManager = null;
	private WindowManager.LayoutParams windowManagerParams = null;
	private FloatView floatView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//cancel title bar
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);//full screen
		setContentView(R.layout.activity_main);
		createView();
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.activity_main, menu);
//		return true;
//	}

	public void onDestroy() {
		super.onDestroy();
		// destroy float window
		windowManager.removeView(floatView);
	}

	private void createView() {
		floatView = new FloatView((FloatApplication)getApplicationContext());
		floatView.setOnClickListener(this);
		floatView.setImageResource(R.drawable.ic_launcher); // use default icon.
		//get WindowManager
		windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		// set LayoutParams(global) params
		windowManagerParams = ((FloatApplication) getApplication()).getWindowParams();
		windowManagerParams.type = LayoutParams.TYPE_PHONE; // set window type
		windowManagerParams.format = PixelFormat.RGBA_8888; // set pic format, background transparent
		// set Window flag
		windowManagerParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_NOT_FOCUSABLE;
		/*
		 * PS: flag value should be: LayoutParams.FLAG_NOT_TOUCH_MODAL not effect event
		 * LayoutParams.FLAG_NOT_FOCUSABLE unable focus. LayoutParams.FLAG_NOT_TOUCHABLE
		 * unable touch focus.
		 */
		// adjust the window to left-top
		windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
		// set x.y base on left-top
		windowManagerParams.x = 0;
		windowManagerParams.y = 0;
		// set float window height, weight.
		windowManagerParams.width = LayoutParams.WRAP_CONTENT;
		windowManagerParams.height = LayoutParams.WRAP_CONTENT;
		// show my float view.
		windowManager.addView(floatView, windowManagerParams);
	}

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

代码解释: 
在activity中我们主要是添加悬浮窗,并且设置他的位置。另外需要注意flags的应用: 
LayoutParams.FLAG_NOT_TOUCH_MODAL 不影响后面的事件 
LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦 
LayoutParams.FLAG_NOT_TOUCHABLE 不可触摸 
最后我们在onDestroy()中移除到悬浮窗口。所以,我们测试的时候,记得按Home键来切换到桌面。 
最后千万记得,在androidManifest.xml中来申明我们需要用到的android.permission.SYSTEM_ALERT_WINDOW权限 
并且记得申明我们自定义的application哦。 
AndroidManifest.xml代码如下: 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.torv.pro"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="FloatApplication"> 
        <activity
            android:name="com.torv.pro.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




分析一下这段代码:#include "SmoothProfileOnXAxisMean.h" void SmoothProfileOnXAxisMean(hls::stream<float>& points_in_z, hls::stream<float>& smoothed_z, ap_uint<6> mean_win_size, float invalid_z) { #pragma HLS INTERFACE axis port=points_in_z #pragma HLS INTERFACE axis port=smoothed_z #pragma HLS INTERFACE ap_none port=mean_win_size #pragma HLS INTERFACE ap_none port=invalid_z #pragma HLS INTERFACE ap_ctrl_none port=return #pragma HLS PIPELINE II=1 // 使用移位寄存器实现窗口存储 static float shift_reg[33]; #pragma HLS ARRAY_PARTITION variable=shift_reg type=complete dim=1 // 窗口状态管理 static ap_uint<6> last_win_size = 0; static ap_uint<1> win_size_changed = 0; static int data_count = 0; // 并行计算单元 float window_sum = 0; int valid_count = 0; float center_value = 0; // 读取新数据(流控制) float new_value = (data_count < 3200) ? points_in_z.read() : invalid_z; // 窗口大小变化检测与处理 if (mean_win_size != last_win_size) { last_win_size = mean_win_size; win_size_changed = 1; } // 窗口大小变化时重置寄存器 if (win_size_changed) { #pragma HLS UNROLL for (int i = 0; i < 33; i++) { shift_reg[i] = invalid_z; } win_size_changed = 0; data_count = 0; } // 计算窗口中心位置 const int half_win = last_win_size >> 1; // 等价于/2 // 并行移位寄存器更新 #pragma HLS UNROLL for (int i = 32; i > 0; i--) { shift_reg[i] = shift_reg[i-1]; } shift_reg[0] = new_value; // 获取中心点值(用于边界判断) center_value = shift_reg[half_win]; // 并行窗口求和与有效计数 #pragma HLS UNROLL for (int i = 0; i < 33; i++) { #pragma HLS EXPRESSION_BALANCE bool in_window = (i < last_win_size); bool is_valid = (shift_reg[i] != invalid_z); if (in_window && is_valid) { window_sum += shift_reg[i]; valid_count++; } } // 边界处理逻辑 bool is_boundary = (data_count < last_win_size) || (data_count >= 3200); bool center_invalid = (center_value == invalid_z); // 输出决策 if (center_invalid || is_boundary) { smoothed_z.write(center_value); } else { float result = (valid_count > 0) ? (window_sum / valid_count) : invalid_z; smoothed_z.write(result); } // 更新数据计数器 if (data_count < 3200 + half_win) { data_count++; } }
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值