Android-WindowManager 显示/隐藏/状态更新

主要步骤
1.RemoteViewManager类对自定义WindowManager显示/隐藏/状态更新进行管理
2.自定义WindowManager类
3.提供出显示/隐藏/状态刷新接口
注意点
  • 1.自定义WindowManager初始化布局位置属性设置
  • 2.频繁操作隐藏和显示,出现隐藏不了问题
1.抽象出WindowManager状态的接口
public interface IRemoteCtrl {
/**
 * 显示
 */
public void showWindow();

/**
 * 隐藏
 */
public void hideWindow();

/**
 * 状态监听
 */
public void enterPage(String page);
}
2.自定义WindowManager类

加载xml文件-初始化其相关的属性值-状态更新

public class BTRemoteWindow  extends RelativeLayout{
private static final String TAG BTRemoteWindow.class.getSimpleName();
Context context;
private WindowManager.LayoutParams btWindowParams;

private View gView;
private WindowManager mWindowManager;

private TextView type;

private TextView talkTime;

private LinearLayout mSwitchLayout;

public BTRemoteWindow(Context context) {
    super(context);
    this.context = context;
    mWindowManager = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    gView = LayoutInflater.from(context).inflate(R.layout.view_remote_window, null);
    initView(gView);
    addView(gView);
}

private void initView(View gView) {
    type = (TextView)gView.findViewById(R.id.type);
    talkTime = (TextView)gView.findViewById(R.id.talkTime);
    mSwitchLayout = (LinearLayout) gView.findViewById(R.id.switchToTalkActivity);
    mSwitchLayout.setOnClickListener(mOnClickListener);     
}


/**
 * 单击事件
 */
private OnClickListener mOnClickListener = new OnClickListener() {
    
    @Override
    public void onClick(View view) {
        switch(view.getId()) {
        case R.id.switchToTalkActivity:
            
            break;
        }
    }
};

/**
 * 初始化布局位置
 */
private WindowManager.LayoutParams windowParams() {
    if (btWindowParams == null) {
        btWindowParams = new WindowManager.LayoutParams();
        btWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        btWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                //| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        btWindowParams.format = PixelFormat.RGBA_8888;
        btWindowParams.gravity = Gravity.LEFT;
    }
    
    //btWindowParams.width = 70;
    //btWindowParams.height = 400;
    //btWindowParams.x = 0;
    btWindowParams.y = 0;
    
    return btWindowParams;
}

/**
 * 显示小窗口
 */
public void showWindow() {
    Log.i(TAG, "showWindow");
    if (this.getParent() == null) {
        mWindowManager.addView(this, windowParams());           
    } else {
        mWindowManager.updateViewLayout(this, windowParams());
    }
}

/**
 * 隐藏小窗口
 */
public void hideWindow() {
    Log.i(TAG, "hideWindow");
    if (this.getParent() != null) {
        mWindowManager.removeView(this);
    }
}

/**
 * 设置状态值
 */
public void setTalkType(String typeStr){
    type.setText(typeStr);
}
}
3. RemoteViewManager类对自定义WindowManager显示/隐藏/状态更新进行管理
public class RemoteViewManager implements IRemoteCtrl, ThreadForever{
private RemoteViewManager() { }
private static RemoteViewManager mRemoteViewManager;

public static RemoteViewManager getInstance() {
    if (mRemoteViewManager == null) {
        mRemoteViewManager = new RemoteViewManager();
    }
    
    return mRemoteViewManager;
}

private static final String TAG = RemoteViewManager.class.getSimpleName();
private Context context;
/** 远端窗口 */
private BTRemoteWindow mBTRemoteWindow;
/** 当前是否显示窗口 */
private boolean mIsShow = false;

Handler handler;
/**
 * 构造弹出框
 */
public void init(Context context) {
    this.context = context;
    mBTRemoteWindow = new BTRemoteWindow(context);
    handler = new Handler();
}

private boolean isInitOK() {
    return this.context != null;
}

/**
 * 显示
 */
public void showWindow() {
    if (!isInitOK()) {
        L.i(TAG, "尚未初始化,show请求失败!");
        return;
    }
    
    if (!mIsShow) {
        mIsShow = true;
        handler.post(new Runnable() {
            
            @Override
            public void run() {
                mBTRemoteWindow.showWindow();
            }
        });
    }
}

/**
 * 隐藏
 */
public void hideWindow() {
    if (!isInitOK()) {
        L.i(TAG, "尚未初始化,hide请求失败!");
        return;
    }
    
    if (mIsShow) {
        mIsShow = false;
        handler.post(new Runnable() {
            
            @Override
            public void run() {
                mBTRemoteWindow.hideWindow();
            }
        });
    }
}

public boolean isShow() {
    return mIsShow;
}

/**
 * 状态监听
 */
public void enterPage(final String page) {
    if (!isInitOK()) {
        L.i(TAG, "尚未初始化,notifyState请求失败!");
        return;
    }
    
    if (page == null || "".equals(page.trim())) {
        L.w(TAG, "页面参数传入异常:" + page);
        return;
    }
    
    handler.post(new Runnable() {
        
        @Override
        public void run() {
            String nameStr = null; //自己赋值
            String numStr = null; //自己赋值
                          
            if (T.PageCmd.PAGE_COMING.equals(page)) {
                mBTRemoteWindow.enterComming(nameStr, numStr);
            } else if (T.PageCmd.PAGE_OUTING.equals(page)) {
                mBTRemoteWindow.enterOuting(nameStr, numStr);
            } else if (T.PageCmd.PAGE_TALKING.equals(page)) {
                mBTRemoteWindow.enterTalking(nameStr, numStr);
            }
        }
    });
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,454评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,319评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,227评论 6 342
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,462评论 4 61
  • 被誉为文学史上百科全书的《红楼梦》,有不少涉及节庆的描写,可都没有构成独立的章节。唯有贾妃元春回家省亲那次的元宵节...
    马风阅读 1,166评论 9 21

友情链接更多精彩内容