Android4.1 Open Menu的过程

2024-01-02 08:08
文章标签 过程 open menu android4.1

本文主要是介绍Android4.1 Open Menu的过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



1. PhoneWindow.onKeyDown() 

     1. onKeyDownPanel.

      当Menu键按下去之后,会产生一个KeyEvent,是keyDown事件,如果Activity没有处理这个Menu Down事件,就会由PhonwWindow默认onKeyDown处理。

在onKeyDown中只要就是调用了onKeyDownPanel把事件传递给对应的panel。     

    protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) {switch (keyCode) {... ...case KeyEvent.KEYCODE_MENU: {onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event);return true;}... ...}return false;}

1.1  PhoneWindow.onKeyDownPanel(int featureId, KeyEvent event)

      1. getPanelState()

      根据featureId获得对应的PanelFeatureState,PanelFeatureState用来保存一个panel对象,比如option menu是对应一个PanelFeatureState 和 featureId。

      2. preparePanel()

       由于是第一次得到PanelFeatureState,st.isOpen返回false,所以就去preparePanel。

    public final boolean onKeyDownPanel(int featureId, KeyEvent event) {final int keyCode = event.getKeyCode();if (event.getRepeatCount() == 0) {// The panel key was pushed, so set the chording keymPanelChordingKey = keyCode;PanelFeatureState st = getPanelState(featureId, true);if (!st.isOpen) {return preparePanel(st, event);}}return false;}

1.1.1 PhoneWindow. getPanelState(int featureId, boolean required, PanelFeatureState convertPanelState)

         这个getPanelState也很简单,如果存在与featureId对应的PanelFeatureState就直接返回,没有的就new一个新的。也就是如果如果我们再当前Activity中,如果是第一次按menu的话,应该是不存在FEATURE_OPTIONS_PANEL相对应的PanelFeatureState。于是就会new一个新的出来。

    private PanelFeatureState getPanelState(int featureId, boolean required,PanelFeatureState convertPanelState) {PanelFeatureState[] ar;if ((ar = mPanels) == null || ar.length <= featureId) {PanelFeatureState[] nar = new PanelFeatureState[featureId + 1];if (ar != null) {System.arraycopy(ar, 0, nar, 0, ar.length);}mPanels = ar = nar;}PanelFeatureState st = ar[featureId];if (st == null) {ar[featureId] = st = (convertPanelState != null)? convertPanelState: new PanelFeatureState(featureId);}return st;}


1.1.2 PhoneWindow.preparePanel(PanelFeatureState st, KeyEvent event)

        1. 通过Callback(Activity)的onCreatePanelView去创建一个panel,用户可以复写这个方法, 如果用户没有复写,则st.createPanelView = null。

        2. initializePanelMenu(), 初始化st中的MenuBuilder,用于以后创建Menu。

        3. 调用cb.onPreparePanel,就会调用Activity的onPreaparePanel方法,而在onPreparePanel中会调用onPrepareOptionsMenu去完成 option menu的prepare工作。完成之后把st中的状态设置一下。 st.isPrepared = true;   st.isHandled = false;   mPreparedPanel = st;

    public final boolean preparePanel(PanelFeatureState st, KeyEvent event) {final Callback cb = getCallback();if (cb != null) {st.createdPanelView = cb.onCreatePanelView(st.featureId);}if (st.createdPanelView == null) {// Init the panel state's menu--return false if init failedif (st.menu == null || st.refreshMenuContent) {if (st.menu == null) {if (!initializePanelMenu(st) || (st.menu == null)) {return false;}}if (mActionBar != null) {if (mActionMenuPresenterCallback == null) {mActionMenuPresenterCallback = new ActionMenuPresenterCallback();}mActionBar.setMenu(st.menu, mActionMenuPresenterCallback);}st.menu.stopDispatchingItemsChanged();st.refreshMenuContent = false;}// Callback and return if the callback does not want to show the menu// Preparing the panel menu can involve a lot of manipulation;// don't dispatch change events to presenters until we're done.st.menu.stopDispatchingItemsChanged();if (!cb.onPreparePanel(st.featureId, st.createdPanelView, st.menu)) {if (mActionBar != null) {// The app didn't want to show the menu for now but it still exists.// Clear it out of the action bar.mActionBar.setMenu(null, mActionMenuPresenterCallback);}st.menu.startDispatchingItemsChanged();return false;}... ...}// Set other statest.isPrepared = true;st.isHandled = false;mPreparedPanel = st;return true;}


2. PhoneWindow.onKeyUp()

     跟KeyDown对应,按键起来之后会有一个up事件。而Menu的up事件也是又PhoneWindow来处理。

     1. onKeyUpPanel

    protected boolean onKeyUp(int featureId, int keyCode, KeyEvent event) {... ...case KeyEvent.KEYCODE_MENU: {onKeyUpPanel(featureId < 0 ? FEATURE_OPTIONS_PANEL : featureId,event);return true;}... ...}

2.1 PhoneWindow.onKeyUpPanel(int featureId, KeyEvent event)

     1.  如果panel在前面的down事件中已经prepare成功了,调用  openPanel(st, event);去打开option menu。并且把    playSoundEffect 设为 true;

          PS: 会打出Event log。

     2. 

    public final void onKeyUpPanel(int featureId, KeyEvent event) {// The panel key was released, so clear the chording keyif (mPanelChordingKey != 0) {... ...            boolean playSoundEffect = false;final PanelFeatureState st = getPanelState(featureId, true);if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null &&mActionBar.isOverflowReserved()) {if (mActionBar.getVisibility() == View.VISIBLE) {if (!mActionBar.isOverflowMenuShowing()) {if (!isDestroyed() && preparePanel(st, event)) {playSoundEffect = mActionBar.showOverflowMenu();}} else {playSoundEffect = mActionBar.hideOverflowMenu();}}} else {if (st.isOpen || st.isHandled) {// Play the sound effect if the user closed an open menu (and not if// they just released a menu shortcut)playSoundEffect = st.isOpen;// Close menuclosePanel(st, true);} else if (st.isPrepared) {boolean show = true;if (st.refreshMenuContent) {// Something may have invalidated the menu since we prepared it.// Re-prepare it to refresh.st.isPrepared = false;show = preparePanel(st, event);}if (show) {// Write 'menu opened' to event logEventLog.writeEvent(50001, 0);// Show menuopenPanel(st, event);playSoundEffect = true;}}}if (playSoundEffect) {AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);if (audioManager != null) {audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);} else {Log.w(TAG, "Couldn't get audio manager");}}}}

2.1.1 PhoneWindow.openPanel(PanelFeatureState st, KeyEvent event)

      在打开panel之前,会对平台的屏幕进行检测,如果发现不是手机,就直接返回。

      1. initializePanelDecor(PanelFeatureState st)  //初始化对应panel的DecorView; 如果menu没有item的话,就直接return

      2. initializePanelContent(PanelFeatureState st), //初始化Panel的MenuView,并赋给st.shownPanelView;

      3. 把st.shownPanelView寄到decorView中,然后通过wm.addView(st.decorView, lp);把新建出来的option menu的decorView加到WMS中。

     这样option menu就显示出来了。

   private void openPanel(PanelFeatureState st, KeyEvent event) {Callback cb = getCallback();if ((cb != null) && (!cb.onMenuOpened(st.featureId, st.menu))) {// Callback doesn't want the menu to open, reset any stateclosePanel(st, true);return;}final WindowManager wm = getWindowManager();int width = WRAP_CONTENT;if (st.decorView == null || st.refreshDecorView) {if (st.decorView == null) {// Initialize the panel decor, this will populate st.decorViewif (!initializePanelDecor(st) || (st.decorView == null))return;} else if (st.refreshDecorView && (st.decorView.getChildCount() > 0)) {// Decor needs refreshing, so remove its viewsst.decorView.removeAllViews();}// This will populate st.shownPanelViewif (!initializePanelContent(st) || !st.hasPanelItems()) {return;}ViewGroup.LayoutParams lp = st.shownPanelView.getLayoutParams();if (lp == null) {lp = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);}int backgroundResId;if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {// If the contents is fill parent for the width, set the// corresponding backgroundbackgroundResId = st.fullBackground;width = MATCH_PARENT;} else {// Otherwise, set the normal panel backgroundbackgroundResId = st.background;}st.decorView.setWindowBackground(getContext().getResources().getDrawable(backgroundResId));ViewParent shownPanelParent = st.shownPanelView.getParent();if (shownPanelParent != null && shownPanelParent instanceof ViewGroup) {((ViewGroup) shownPanelParent).removeView(st.shownPanelView);}st.decorView.addView(st.shownPanelView, lp);/** Give focus to the view, if it or one of its children does not* already have it.*/if (!st.shownPanelView.hasFocus()) {st.shownPanelView.requestFocus();}} else if (!st.isInListMode()) {width = MATCH_PARENT;} else if (st.createdPanelView != null) {// If we already had a panel view, carry width=MATCH_PARENT through// as we did above when it was created.ViewGroup.LayoutParams lp = st.createdPanelView.getLayoutParams();if (lp != null && lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {width = MATCH_PARENT;}}st.isOpen = true;st.isHandled = false;WindowManager.LayoutParams lp = new WindowManager.LayoutParams(width, WRAP_CONTENT,st.x, st.y, WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM| WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,st.decorView.mDefaultOpacity);if (st.isCompact) {lp.gravity = getOptionsPanelGravity();sRotationWatcher.addWindow(this);} else {lp.gravity = st.gravity;}lp.windowAnimations = st.windowAnimations;wm.addView(st.decorView, lp);// Log.v(TAG, "Adding main menu to window manager.");}

2.1.1.1     PhoneWindow.initializePanelDecor(PanelFeatureState st) 

        new出一个DecorView并且赋值给st.decorView。

    protected boolean initializePanelDecor(PanelFeatureState st) {st.decorView = new DecorView(getContext(), st.featureId);st.gravity = Gravity.CENTER | Gravity.BOTTOM;st.setStyle(getContext());return true;}

2.1.1.2 PhoneWindow. initializePanelContent(PanelFeatureState st) 

     //初始化Panel的MenuView,并赋给st.shownPanelView;

    protected boolean initializePanelContent(PanelFeatureState st) {if (st.createdPanelView != null) {st.shownPanelView = st.createdPanelView;return true;}if (st.menu == null) {return false;}if (mPanelMenuPresenterCallback == null) {mPanelMenuPresenterCallback = new PanelMenuPresenterCallback();}MenuView menuView = st.isInListMode()? st.getListMenuView(getContext(), mPanelMenuPresenterCallback): st.getIconMenuView(getContext(), mPanelMenuPresenterCallback);st.shownPanelView = (View) menuView;if (st.shownPanelView != null) {// Use the menu View's default animations if it has anyfinal int defaultAnimations = menuView.getWindowAnimations();if (defaultAnimations != 0) {st.windowAnimations = defaultAnimations;}return true;} else {return false;}}




这篇关于Android4.1 Open Menu的过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/561849

相关文章

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

作业提交过程之HDFSMapReduce

作业提交全过程详解 (1)作业提交 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。 第2步:Client向RM申请一个作业id。 第3步:RM给Client返回该job资源的提交路径和作业id。 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。 第5步:Client提交完资源后,向RM申请运行MrAp

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

Solr 使用Facet分组过程中与分词的矛盾解决办法

对于一般查询而言  ,  分词和存储都是必要的  .  比如  CPU  类型  ”Intel  酷睿  2  双核  P7570”,  拆分成  ”Intel”,”  酷睿  ”,”P7570”  这样一些关键字并分别索引  ,  可能提供更好的搜索体验  .  但是如果将  CPU  作为 Facet  字段  ,  最好不进行分词  .  这样就造成了矛盾  ,  解决方法

Python:豆瓣电影商业数据分析-爬取全数据【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】

**爬取豆瓣电影信息,分析近年电影行业的发展情况** 本文是完整的数据分析展现,代码有完整版,包含豆瓣电影爬取的具体方式【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】   最近MBA在学习《商业数据分析》,大实训作业给了数据要进行数据分析,所以先拿豆瓣电影练练手,网络上爬取豆瓣电影TOP250较多,但对于豆瓣电影全数据的爬取教程很少,所以我自己做一版。 目

ORACLE语法-包(package)、存储过程(procedure)、游标(cursor)以及java对Result结果集的处理

陈科肇 示例: 包规范 CREATE OR REPLACE PACKAGE PACK_WMS_YX IS-- Author : CKZ-- Created : 2015/8/28 9:52:29-- Purpose : 同步数据-- Public type declarations,游标 退休订单TYPE retCursor IS REF CURSOR;-- RETURN vi_co_co

Open a folder or workspace... (File -> Open Folder)

问题:vscode Open with Live Server 时 显示Open a folder or workspace... (File -> Open Folder)报错 解决:不可以单独打开文件1.html ; 需要在文件夹里打开 像这样

android java.io.IOException: open failed: ENOENT (No such file or directory)-api23+权限受权

问题描述 在安卓上,清单明明已经受权了读写文件权限,但偏偏就是创建不了目录和文件 调用mkdirs()总是返回false. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_E

OpenStack创建虚拟机过程

OpenStack创建虚拟机过程 一、在分析OpenStack创建虚拟机的过程之前,先来梳理一下需要用用到哪些组件。 二、每一步都需要去keystone去进行验证,下图有详细的流程。 登录界面或命令行通过RESTful API向keystone获取认证信息。keystone通过用户请求认证信息,并生成auth-token返回给对应的认证请求。界面或命令行通过RESTful API

Maven生命周期:深入理解构建过程

目录 1. Maven生命周期简介 2. 默认生命周期的阶段 3. 清理生命周期 4. 站点生命周期 5. Maven生命周期的灵活性 6. 结论         在Java开发中,Maven是一个不可或缺的工具,它通过自动化项目的构建、依赖管理和文档生成等任务,极大地提高了开发效率。Maven的核心之一是其构建生命周期,它定义了项目构建过程中的一系列阶段。在这篇文章中,我们将深