点击屏幕引出或显示部件的方法

最近开发中涉及到一些部件需要,点击一下屏幕,部件显示,再点击一次部件隐藏,如果3s钟不点击屏幕,部件自动隐藏。

这个功能android4.3已经提供一些类:SystemUiHiderHoneycomb,SystemUiHiderBase,SystemUiHider

如果需要可以直接将这三个文件导入到自己的工程。

1、SystemUiHiderHoneycomb.java

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
/**
 * An API 11+ implementation of {@link SystemUiHider}. Uses APIs available in
 * Honeycomb and later (specifically {@link View#setSystemUiVisibility(int)}) to
 * show and hide the system UI.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SystemUiHiderHoneycomb extends SystemUiHiderBase {
/**
* Flags for {@link View#setSystemUiVisibility(int)} to use when showing the
* system UI.
*/
private int mShowFlags;


/**
* Flags for {@link View#setSystemUiVisibility(int)} to use when hiding the
* system UI.
*/
private int mHideFlags;


/**
* Flags to test against the first parameter in
* {@link android.view.View.OnSystemUiVisibilityChangeListener#onSystemUiVisibilityChange(int)}
* to determine the system UI visibility state.
*/
private int mTestFlags;


/**
* Whether or not the system UI is currently visible. This is cached from
* {@link android.view.View.OnSystemUiVisibilityChangeListener}.
*/
private boolean mVisible = true;


/**
* Constructor not intended to be called by clients. Use
* {@link SystemUiHider#getInstance} to obtain an instance.
*/
protected SystemUiHiderHoneycomb(Activity activity, View anchorView,
int flags) {
super(activity, anchorView, flags);


mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE;
mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if ((mFlags & FLAG_FULLSCREEN) != 0) {
// If the client requested fullscreen, add flags relevant to hiding
// the status bar. Note that some of these constants are new as of
// API 16 (Jelly Bean). It is safe to use them, as they are inlined
// at compile-time and do nothing on pre-Jelly Bean devices.
mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
}
if ((mFlags & FLAG_HIDE_NAVIGATION) != 0) {
// If the client requested hiding navigation, add relevant flags.
mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
mTestFlags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
}


/** {@inheritDoc} */
@Override
public void setup() {
mAnchorView
.setOnSystemUiVisibilityChangeListener(mSystemUiVisibilityChangeListener);
}


/** {@inheritDoc} */
@Override
public void hide() {
mAnchorView.setSystemUiVisibility(mHideFlags);
}


/** {@inheritDoc} */
@Override
public void show() {
mAnchorView.setSystemUiVisibility(mShowFlags);
}


/** {@inheritDoc} */
@Override
public boolean isVisible() {
return mVisible;
}
private View.OnSystemUiVisibilityChangeListener mSystemUiVisibilityChangeListener = new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int vis) {
// Test against mTestFlags to see if the system UI is visible.
if ((vis & mTestFlags) != 0) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// Pre-Jelly Bean, we must manually hide the action bar
// and use the old window flags API.
mActivity.getActionBar().hide();
mActivity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}


// Trigger the registered listener and cache the visibility
// state.
mOnVisibilityChangeListener.onVisibilityChange(false);
mVisible = false;


} else {
mAnchorView.setSystemUiVisibility(mShowFlags);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// Pre-Jelly Bean, we must manually show the action bar
// and use the old window flags API.

//mActivity.getActionBar().show();
mActivity.getWindow().setFlags(0,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// Trigger the registered listener and cache the visibility
// state.
mOnVisibilityChangeListener.onVisibilityChange(true);
mVisible = true;
}
}
};
}

2、SystemUiHiderBase.java

import android.app.Activity;
import android.view.View;
import android.view.WindowManager;


/**
 * A base implementation of {@link SystemUiHider}. Uses APIs available in all
 * API levels to show and hide the status bar.
 */
public class SystemUiHiderBase extends SystemUiHider {
/**
* Whether or not the system UI is currently visible. This is a cached value
* from calls to {@link #hide()} and {@link #show()}.
*/
private boolean mVisible = true;


/**
* Constructor not intended to be called by clients. Use
* {@link SystemUiHider#getInstance} to obtain an instance.
*/
protected SystemUiHiderBase(Activity activity, View anchorView, int flags) {
super(activity, anchorView, flags);
}


@Override
public void setup() {
if ((mFlags & FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES) == 0) {
mActivity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}


@Override
public boolean isVisible() {
return mVisible;
}
@Override
public void hide() {
if ((mFlags & FLAG_FULLSCREEN) != 0) {
mActivity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mOnVisibilityChangeListener.onVisibilityChange(false);
mVisible = false;
}
@Override
public void show() {
if ((mFlags & FLAG_FULLSCREEN) != 0) {
mActivity.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mOnVisibilityChangeListener.onVisibilityChange(true);
mVisible = true;
}
}

3、SystemUiHider.java

import android.app.Activity;
import android.os.Build;
import android.view.View;


public abstract class SystemUiHider {
public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1;
public static final int FLAG_FULLSCREEN = 0x2;
public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN | 0x4;


/**
* The activity associated with this UI hider object.
*/
protected Activity mActivity;


/**
* The view on which {@link View#setSystemUiVisibility(int)} will be called.
*/
protected View mAnchorView;
protected int mFlags;


/**
* The current visibility callback.
*/
protected OnVisibilityChangeListener mOnVisibilityChangeListener = sDummyListener;
public static SystemUiHider getInstance(Activity activity, View anchorView,
int flags) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return new SystemUiHiderHoneycomb(activity, anchorView, flags);
} else {
return new SystemUiHiderBase(activity, anchorView, flags);
}
}
protected SystemUiHider(Activity activity, View anchorView, int flags) {
mActivity = activity;
mAnchorView = anchorView;
mFlags = flags;
}
public abstract void setup();


/**
* Returns whether or not the system UI is visible.
*/
public abstract boolean isVisible();


/**
* Hide the system UI.
*/
public abstract void hide();
/**
* Show the system UI.
*/
public abstract void show();


/**
* Toggle the visibility of the system UI.
*/
public void toggle() {
if (isVisible()) {
hide();
} else {
show();
}
}


/**
* Registers a callback, to be triggered when the system UI visibility
* changes.
*/
public void setOnVisibilityChangeListener(
OnVisibilityChangeListener listener) {
if (listener == null) {
listener = sDummyListener;
}


mOnVisibilityChangeListener = listener;
}


/**
* A dummy no-op callback for use when there is no other listener set.
*/
private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() {
@Override
public void onVisibilityChange(boolean visible) {
}
};


/**
* A callback interface used to listen for system UI visibility changes.
*/
public interface OnVisibilityChangeListener {
/**
* Called when the system UI visibility has changed.

* @param visible
*            True if the system UI is visible.
*/
public void onVisibilityChange(boolean visible);
}
}

4、具体使用这些类来实现功能的例子: 

点击mGestureImageView(一个imageview)的上下两段区域,显示或隐藏上下两段部件。

private static final int AUTO_HIDE_DELAY_MILLIS = 3000;  //自动隐藏时间为3s
private SystemUiHider mSystemUiHider;
private boolean mFloatWindows = false; //是否在点击有效区域


mSystemUiHider = SystemUiHider.getInstance(((ScreenShareMain)getActivity()), mGestureImageView,HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
int mControlsButtonWidth;
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.

//上段部件显示或隐藏动画
if (mControlsHeight == 0) {
mControlsHeight = mFilesTitleBar.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
mFilesTitleBar
.animate()
.translationY(visible ? 0 : -mControlsHeight)
.setDuration(mShortAnimTime);


//下段部件显示或隐藏动画
mGalleryFramelayout
.animate()
.translationY(visible ? 0 : 200)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
mFilesTitleBar.setVisibility(visible ? View.VISIBLE
: View.GONE);
mGalleryFramelayout.setVisibility(visible ? View.VISIBLE
: View.GONE);


}


if (visible && AUTO_HIDE) {
// Schedule a hide().
Log.i(TAG,"delayedHide     haha ");
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});

mGestureImageView.setOnTouchListener(mDelayHideTouchListener);

mGestureImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG,"onClick");
if(mFloatWindows){
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
}
});

delayedHide(AUTO_HIDE_DELAY_MILLIS);




Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
if( null != mSystemUiHider )
{
mSystemUiHider.hide();
}
}
};




private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}


//是否在有效区域内点击

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
float x = motionEvent.getX();
float y = motionEvent.getY();
WindowManager wm = (WindowManager)((ScreenShareMain)getActivity()).getSystemService(Context.WINDOW_SERVICE);  
           int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
           Log.i(TAG,"width: "+width);
           Log.i(TAG,"height: "+height);
if (y > height-200 ||  y < 100) {
mFloatWindows = true;
} else {
mFloatWindows = false;
}
}
}
return false;
}
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值