下面列出了android.view.WindowManager.LayoutParams#FLAG_NOT_FOCUSABLE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public WindowManager.LayoutParams creatParams(int type, FloatParams floatParams) {
//系统浮窗不能超出边界,更新xy
int ww = (w - floatParams.w) / 2;
int hh = (h - floatParams.h) / 2;
if (Math.abs(floatParams.x) > ww)
floatParams.x = floatParams.x > 0 ? ww : -ww;
if (Math.abs(floatParams.y) > hh)
floatParams.y = floatParams.y > 0 ? hh : -hh;
LayoutParams smallWindowParams = new LayoutParams();
smallWindowParams.type = type;
smallWindowParams.format = PixelFormat.RGBA_8888;
smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
//smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
smallWindowParams.width = floatParams.w;
smallWindowParams.height = floatParams.h;
smallWindowParams.x = floatParams.x;
smallWindowParams.y = floatParams.y;
return smallWindowParams;
}
/**
* attach handview to {@link WindowManager}
*
* @param y
*/
public void attachToWindow(int x, int y) {
if (this.getParent() != null) {
return;
}
mLp = new WindowManager.LayoutParams();
mLp.type = LayoutParams.TYPE_SYSTEM_ALERT;
mLp.format = PixelFormat.RGBA_8888;
mLp.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
mLp.gravity = Gravity.LEFT | Gravity.TOP;
mLp.width = WindowManager.LayoutParams.WRAP_CONTENT;
mLp.height = WindowManager.LayoutParams.WRAP_CONTENT;
mLp.x = x;
mOriY = y;
mLp.y = mOriY;
mWindowManager.addView(this, mLp);
}
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
int flags = (LayoutParams.FLAG_NOT_FOCUSABLE |
LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
LayoutParams.FLAG_NOT_TOUCH_MODAL |
LayoutParams.FLAG_LAYOUT_INSET_DECOR |
LayoutParams.FLAG_LAYOUT_IN_SCREEN |
LayoutParams.FLAG_SHOW_WHEN_LOCKED );
LayoutParams WPARAMS = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT, mStatusBarHeight, 0, 0,
LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
WPARAMS.packageName = getContext().getPackageName();
WPARAMS.setTitle(TAG);
WPARAMS.rotationAnimation = LayoutParams.ROTATION_ANIMATION_JUMPCUT;
WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
WPARAMS.screenBrightness = WPARAMS.buttonBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
return WPARAMS;
}
public void onButtonClick(View v) {
if (v == mCreateWindowButton) {
mFloatingButton = new Button(this);
mFloatingButton.setText("click me");
mLayoutParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0,
PixelFormat.TRANSPARENT);
mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_SHOW_WHEN_LOCKED;
mLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mLayoutParams.x = 100;
mLayoutParams.y = 300;
mFloatingButton.setOnTouchListener(this);
mWindowManager.addView(mFloatingButton, mLayoutParams);
}
}
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
int flags = (LayoutParams.FLAG_NOT_FOCUSABLE |
LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
LayoutParams.FLAG_NOT_TOUCH_MODAL |
LayoutParams.FLAG_LAYOUT_INSET_DECOR |
LayoutParams.FLAG_LAYOUT_IN_SCREEN |
LayoutParams.FLAG_SHOW_WHEN_LOCKED );
LayoutParams WPARAMS = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT, mStatusBarHeight, 0, 0,
LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
WPARAMS.packageName = getContext().getPackageName();
WPARAMS.setTitle(TAG);
WPARAMS.rotationAnimation = LayoutParams.ROTATION_ANIMATION_JUMPCUT;
WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
WPARAMS.screenBrightness = WPARAMS.buttonBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
return WPARAMS;
}
/**
* Sets whether the zoom controls should be focusable. If the controls are
* focusable, then trackball and arrow key interactions are possible.
* Otherwise, only touch interactions are possible.
*
* @param focusable Whether the zoom controls should be focusable.
*/
public void setFocusable(boolean focusable) {
int oldFlags = mContainerLayoutParams.flags;
if (focusable) {
mContainerLayoutParams.flags &= ~LayoutParams.FLAG_NOT_FOCUSABLE;
} else {
mContainerLayoutParams.flags |= LayoutParams.FLAG_NOT_FOCUSABLE;
}
if ((mContainerLayoutParams.flags != oldFlags) && mIsVisible) {
mWindowManager.updateViewLayout(mContainer, mContainerLayoutParams);
}
}
private Program prepare(Application application) {
metronome = new Metronome();
params = new LayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
// if (isOverlayApiDeprecated()) {
// params.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
// } else {
params.type = LayoutParams.TYPE_TOAST;
//}
params.flags = LayoutParams.FLAG_KEEP_SCREEN_ON | LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Seat.BOTTOM_RIGHT.getGravity();
params.x = 10;
app = application;
wm = WindowManager.class.cast(application.getSystemService(Context.WINDOW_SERVICE));
LayoutInflater inflater = LayoutInflater.from(app);
stageView = inflater.inflate(R.layout.stage, new RelativeLayout(app));
fpsText = stageView.findViewById(R.id.takt_fps);
listener(new Audience() {
@Override
public void heartbeat(double fps) {
if (fpsText != null) {
fpsText.setText(decimal.format(fps));
}
}
});
return this;
}
private Program prepare(Application application) {
metronome = new Metronome();
params = new LayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
// if (isOverlayApiDeprecated()) {
// params.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
// } else {
params.type = LayoutParams.TYPE_TOAST;
//}
params.flags = LayoutParams.FLAG_KEEP_SCREEN_ON | LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Seat.BOTTOM_RIGHT.getGravity();
params.x = 10;
app = application;
wm = WindowManager.class.cast(application.getSystemService(Context.WINDOW_SERVICE));
LayoutInflater inflater = LayoutInflater.from(app);
stageView = inflater.inflate(R.layout.stage, new RelativeLayout(app));
fpsText = (TextView) stageView.findViewById(R.id.takt_fps);
listener(new Audience() {
@Override
public void heartbeat(double fps) {
if (fpsText != null) {
fpsText.setText(decimal.format(fps));
}
}
});
return this;
}
private Program prepare(Application application) {
metronome = new Metronome();
params = new LayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
application.registerActivityLifecycleCallbacks(new LifecycleListener(this));
if (isOverlayApiDeprecated()) {
params.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
params.type = LayoutParams.TYPE_TOAST;
}
params.flags = LayoutParams.FLAG_KEEP_SCREEN_ON | LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_TOUCHABLE;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Seat.BOTTOM_RIGHT.getGravity();
params.x = 10;
app = application;
wm = WindowManager.class.cast(application.getSystemService(Context.WINDOW_SERVICE));
LayoutInflater inflater = LayoutInflater.from(app);
stageView = inflater.inflate(R.layout.stage, new RelativeLayout(app));
fpsText = stageView.findViewById(R.id.takt_fps);
listener(new Audience() {
@Override public void heartbeat(double fps) {
if (fpsText != null) {
fpsText.setText(decimal.format(fps));
}
}
});
return this;
}
/**
* Disposes of the popup window. This method can be invoked only after
* {@link #showAsDropDown(android.view.View)} has been executed. Failing
* that, calling this method will have no effect.
*
* @see #showAsDropDown(android.view.View)
*/
public void dismiss() {
if (!isShowing() || isTransitioningToDismiss()) {
return;
}
final PopupDecorView decorView = mDecorView;
final View contentView = mContentView;
final ViewGroup contentHolder;
final ViewParent contentParent = contentView.getParent();
if (contentParent instanceof ViewGroup) {
contentHolder = ((ViewGroup) contentParent);
} else {
contentHolder = null;
}
// Ensure any ongoing or pending transitions are canceled.
decorView.cancelTransitions();
mIsShowing = false;
mIsTransitioningToDismiss = true;
// This method may be called as part of window detachment, in which
// case the anchor view (and its root) will still return true from
// isAttachedToWindow() during execution of this method; however, we
// can expect the OnAttachStateChangeListener to have been called prior
// to executing this method, so we can rely on that instead.
final Transition exitTransition = mExitTransition;
if (exitTransition != null && decorView.isLaidOut()
&& (mIsAnchorRootAttached || mAnchorRoot == null)) {
// The decor view is non-interactive and non-IME-focusable during exit transitions.
final LayoutParams p = (LayoutParams) decorView.getLayoutParams();
p.flags |= LayoutParams.FLAG_NOT_TOUCHABLE;
p.flags |= LayoutParams.FLAG_NOT_FOCUSABLE;
p.flags &= ~LayoutParams.FLAG_ALT_FOCUSABLE_IM;
mWindowManager.updateViewLayout(decorView, p);
final View anchorRoot = mAnchorRoot != null ? mAnchorRoot.get() : null;
final Rect epicenter = getTransitionEpicenter();
// Once we start dismissing the decor view, all state (including
// the anchor root) needs to be moved to the decor view since we
// may open another popup while it's busy exiting.
decorView.startExitTransition(exitTransition, anchorRoot, epicenter,
new TransitionListenerAdapter() {
@Override
public void onTransitionEnd(Transition transition) {
dismissImmediate(decorView, contentHolder, contentView);
}
});
} else {
dismissImmediate(decorView, contentHolder, contentView);
}
// Clears the anchor view.
detachFromAnchor();
if (mOnDismissListener != null) {
mOnDismissListener.onDismiss();
}
}
/**
* 要显示在窗口最前面的方法
*
* @param context
* 调用对象Context getApplicationContext()
* @param window
* 调用对象 Window getWindow()
* @param floatingViewObj
* 要显示的浮动对象 View
*/
public static void show(Context context, View floatingViewObj) {
mFloatingViewObj = floatingViewObj;
view_obj = mFloatingViewObj;
Rect frame = new Rect();
// 这一句是关键,让其在top 层显示
// getWindow()
// window.getDecorView().getWindowVisibleDisplayFrame(frame);
TOOL_BAR_HIGH = frame.top;
wm = (WindowManager) context// getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;// 2;
// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
// params.type = 2003;//WindowManager.LayoutParams.TYPE_PHONE;
params.format = -2;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
// 设置悬浮窗口长宽数据
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = HWUtils.dip2px(context, 60);
// 设定透明度
params.alpha = 80;
// 设定内部文字对齐方式
params.gravity = Gravity.LEFT | Gravity.TOP;
screenWidth = wm.getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
screenHeight = wm.getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800px)
// 以屏幕左上角为原点,设置x、y初始值ֵ
// params.x = (int) (screenWidth - params.width);
String postions = HWPreferences.getData(context, "HW_postions");
String mY = HWPreferences.getData(context, "HW_currentY");
if (null != postions && null != mY && 0 < mY.length()) {
if (Boolean.valueOf(postions)) {
params.x = params.width;
} else {
params.x = (int) (screenWidth - params.width);
}
isLeftPosition = Boolean.valueOf(postions);
params.y = Integer.valueOf(mY) + 60;
} else {
params.x = params.width;
params.y = ((screenHeight - params.height) / 2) + 60;
}
// params.y = (int) y;
// tv = new MyTextView(TopFrame.this);
wm.addView(floatingViewObj, params);
wm.updateViewLayout(mFloatingViewObj, params);
isShow = true;
mIsShow = true; // 定时器检测浮标是否已经显示出来的标志
}
private void createFloatView() {
wmParams = new WindowManager.LayoutParams();
mWindowManager = (WindowManager) getApplication().getSystemService(android.app.Application.WINDOW_SERVICE);
wmParams.type = LayoutParams.TYPE_SYSTEM_ALERT;// 设置window
// type为TYPE_SYSTEM_ALERT
wmParams.format = PixelFormat.RGBA_8888;// 设置图片格式,效果为背景透明
wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.gravity = Gravity.LEFT | Gravity.TOP;// 默认位置:左上角
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.x = (WidgetUtils.getScreenWidth(getApplicationContext()) - wmParams.width) / 2;// 设置x、y初始值,相对于gravity
wmParams.y = 10;
// 获取浮动窗口视图所在布局
LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
mWindowManager.addView(mFloatLayout, wmParams);// 添加mFloatLayout
mFloatView = 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;
// Log.i(TAG, "RawX" + event.getRawX());
// Log.i(TAG, "X" + event.getX());
wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;// 减25为状态栏的高度
// Log.i(TAG, "RawY" + event.getRawY());
// Log.i(TAG, "Y" + event.getY());
mWindowManager.updateViewLayout(mFloatLayout, wmParams);// 刷新
return false; // 此处必须返回false,否则OnClickListener获取不到监听
}
});
mFloatView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
}
public FaceLockService()
{
binder = new Stub()
{
@Override
public void startUi( IBinder windowToken, int x, int y, int w, int h, boolean useLiveliness ) throws RemoteException
{
DisplayMetrics metrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getRealMetrics( metrics );
Log.d( "PicturePassword", "Appearing at " + ( y + h ) );
if ( y + h > metrics.heightPixels ) return;
LayoutParams p = new LayoutParams( LayoutParams.TYPE_APPLICATION_PANEL );
p.flags = LayoutParams.FLAG_HARDWARE_ACCELERATED | LayoutParams.FLAG_NOT_FOCUSABLE;
p.token = windowToken;
p.x = x;
p.y = y;
p.width = w;
p.height = h;
p.gravity = 8388659; // TODO: decompose, i have no idea what this means
FaceLockService.this.mLayoutParams = p;
FaceLockService.this.mHandler.obtainMessage( MSG_SERVICE_CONNECTED, p ).sendToTarget();
if ( PicturePasswordUtils.getLockedOut( FaceLockService.this ) )
{
lockOut( false );
}
}
@Override
public void stopUi() throws RemoteException
{
FaceLockService.this.mHandler.sendEmptyMessage( MSG_SERVICE_DISCONNECTED );
}
@Override
public void registerCallback( IFaceLockCallback callback ) throws RemoteException
{
FaceLockService.this.mCallback = callback;
}
@Override
public void unregisterCallback( IFaceLockCallback callback ) throws RemoteException
{
FaceLockService.this.mCallback = null;
}
@Override
public Bitmap getImage() throws RemoteException
{
return FaceLockService.this.mBitmap;
}
@Override
public boolean onTransact( int code, Parcel data, Parcel reply, int flags ) throws RemoteException
{
if ( code == 0x4747 )
{
reply.writeValue( getImage() );
return true;
}
return super.onTransact( code, data, reply, flags );
}
};
}