鲁班软件官网:向android 的状态栏中加入快捷按钮(home,back,menu等等)的方法(续) ...

来源:百度文库 编辑:九乡新闻网 时间:2024/07/04 18:37:01
向android 的状态栏中加入快捷按钮(home,back,menu等等)的方法(续)

上一篇文章中谈到的加入快捷按钮的方法,实际上还是太过麻烦。那篇博客是在我刚接触android源代码没几天时,参考网上的介绍方法,自己看了下源代码尝试着写了一个。 不过那个方法,是我直接用贴图的方法实现了按钮特效,这实在是太浪费了,最近一直仍有朋友问我那篇文章中的问题,我想还是重写一下,用一个更简单点的方法,直接使用android的ImageButton控件,通过其OnTouchListener方法操作即可,Button的高亮与否完全由系统处理,这样也不会出现button高亮不消失的bug。以下方法在android 2.1 上编译调试通过

1。 准备资源,修改XML文件

和上篇文章一样,准备几张图,这里我们准备添加home back和menu图标,就需要准备6张图,三张普通状态,三张按下的高亮状态图标:

stat_home.png

stat_home_pressed.png

stat_back.png

stat_back_pressed.png

stat_menu.png

stat_menu_pressed.png

同时,在 Frameworks/base/core/res/res/drawable下创建三个imageButton的xml文件:

xml_stat_home.xml

view plaincopy to clipboardprint

  1.  version="1.0" encoding="utf-8"?>    
  2.     
  •     xmlns:android="http://schemas.android.com/apk/res/android">    
  •         
  •         android:state_focused="true"    
  •         android:state_pressed="false"    
  •         android:drawable="@drawable/stat_home" />    
  •         
  •         android:state_focused="true"    
  •         android:state_pressed="true"    
  •         android:drawable="@drawable/stat_home_pressed" />    
  •         
  •         android:state_focused="false"    
  •         android:state_pressed="true"    
  •         android:drawable="@drawable/stat_home_pressed" />    
  •         
  •         android:drawable="@drawable/stat_home" />    
  •    
  •  

    xml_stat_back.xml

    view plaincopy to clipboardprint

    1.  version="1.0" encoding="utf-8"?>    
    2.     
    3.     xmlns:android="http://schemas.android.com/apk/res/android">    
    4.         
    5.         android:state_focused="true"    
    6.         android:state_pressed="false"    
    7.         android:drawable="@drawable/stat_back" />    
    8.         
    9.         android:state_focused="true"    
    10.         android:state_pressed="true"    
    11.         android:drawable="@drawable/stat_back_pressed" />    
    12.         
    13.         android:state_focused="false"    
    14.         android:state_pressed="true"    
    15.         android:drawable="@drawable/stat_back_pressed" />    
    16.         
    17.         android:drawable="@drawable/stat_back" />    
    18.    

    xml_stat_menu.xml

     

      view plaincopy to clipboardprint

    1.  version="1.0" encoding="utf-8"?>    
    2.     
  •     xmlns:android="http://schemas.android.com/apk/res/android">    
  •         
  •         android:state_focused="true"    
  •         android:state_pressed="false"    
  •         android:drawable="@drawable/stat_menu" />    
  •         
  •         android:state_focused="true"    
  •         android:state_pressed="true"    
  •         android:drawable="@drawable/stat_menu_pressed" />    
  •         
  •         android:state_focused="false"    
  •         android:state_pressed="true"    
  •         android:drawable="@drawable/stat_menu_pressed" />    
  •         
  •         android:drawable="@drawable/stat_menu" />    
  •    
  •  

    修改status_bar.xml,如下:

    view plaincopy to clipboardprint

    1.  version="1.0" encoding="utf-8"?>  
    2.   
    3.  xmlns:android="http://schemas.android.com/apk/res/android"    
    4.     android:background="@drawable/statusbar_background"  
    5.     android:orientation="vertical"  
    6.     android:focusable="true"  
    7.     android:descendantFocusability="afterDescendants"  
    8.     >  
    9.   
    10.      android:id="@+id/icons"  
    11.         android:layout_width="fill_parent"  
    12.         android:layout_height="fill_parent"  
    13.         android:orientation="horizontal">  
    14.   
    15.      android:id="@+id/go_home"  
    16.         android:layout_width="36px"  
    17.         android:layout_height="36px"  
    18.         android:layout_marginRight="15px"  
    19.         android:layout_marginLeft="10px"  
    20.         android:layout_marginTop="5px"  
    21.         android:clickable="true"    
    22.         android:background="@drawable/xml_stat_home"  
    23.         />    
    24.     
    25.            
    26.          android:id="@+id/notificationIcons"  
    27.             android:layout_width="0dip"  
    28.             android:layout_weight="1"  
    29.             android:layout_height="fill_parent"  
    30.             android:layout_alignParentLeft="true"  
    31.             android:paddingLeft="6dip"  
    32.             android:gravity="center_vertical"  
    33.             android:orientation="horizontal"/>     
    34.                
    35.          android:id="@+id/statusIcons"  
    36.             android:layout_width="wrap_content"  
    37.             android:layout_height="fill_parent"  
    38.             android:layout_alignParentRight="true"  
    39.             android:paddingRight="6dip"  
    40.             android:gravity="center_vertical"  
    41.             android:orientation="horizontal"/>       
    42.   
    43.        android:id="@+id/pop_menu"  
    44.         android:layout_width="36px"  
    45.         android:layout_height="36px"  
    46.         android:layout_marginRight="15px"  
    47.         android:layout_marginLeft="10px"  
    48.         android:layout_marginTop="5px"  
    49.         android:clickable="true"    
    50.         android:background="@drawable/xml_stat_menu"  
    51.         />           
    52.       android:id="@+id/go_back"  
    53.         android:layout_width="36px"  
    54.         android:layout_height="36px"  
    55.         android:layout_marginRight="15px"  
    56.         android:layout_marginLeft="10px"  
    57.         android:layout_marginTop="5px"  
    58.         android:clickable="true"    
    59.         android:background="@drawable/xml_stat_back"  
    60.         />            
    61.       
    62.            
    63.      android:id="@+id/ticker"  
    64.         android:layout_width="fill_parent"  
    65.         android:layout_height="fill_parent"  
    66.         android:paddingLeft="6dip"  
    67.         android:animationCache="false"  
    68.         android:orientation="horizontal" >  
    69.          android:id="@+id/tickerIcon"  
    70.             android:layout_width="wrap_content"  
    71.             android:layout_height="fill_parent"  
    72.             android:layout_marginRight="8dip"  
    73.             >  
    74.               
    75.                 android:layout_width="36dip"  
    76.                 android:layout_height="36dip"  
    77.                 />  
    78.               
    79.                 android:layout_width="36dip"  
    80.                 android:layout_height="36dip"  
    81.                 />  
    82.           
    83.          android:id="@+id/tickerText"  
    84.             android:layout_width="0dip"  
    85.             android:layout_weight="1"  
    86.             android:layout_height="wrap_content"  
    87.             android:paddingTop="2dip"  
    88.             android:paddingRight="10dip">  
    89.               
    90.                 android:layout_width="fill_parent"  
    91.                 android:layout_height="wrap_content"  
    92.                 android:singleLine="true"  
    93.                 android:textColor="#ff000000" />  
    94.               
    95.                 android:layout_width="fill_parent"  
    96.                 android:layout_height="wrap_content"  
    97.                 android:singleLine="true"  
    98.                 android:textColor="#ff000000" />  
    99.           
    100.       
    101.   
    102.      android:id="@+id/date"  
    103.         android:layout_width="wrap_content"  
    104.         android:layout_height="fill_parent"  
    105.         android:singleLine="true"  
    106.         android:textSize="20sp"  
    107.         android:textStyle="bold"  
    108.         android:gravity="center_vertical|left"  
    109.         android:paddingLeft="6px"  
    110.         android:paddingRight="6px"  
    111.         android:textColor="?android:attr/textColorPrimaryInverse"  
    112.         android:background="@drawable/statusbar_background"  
    113.         />  
    114.   

     

    如上篇,修改statusbar的高度,编译一下,即可看到效果。

    2。 添加按钮的动作效果

    在statusBarView.java中,活的button的handler

    类中新增加三个成员:

    view plaincopy to clipboardprint

    1. ImageButton mHomeBtn;   
    2. ImageButton mBackBtn;   
    3. ImageButton mMenuBtn;  

     

    增加三个常量:

     public static final int RESV_KEY_HOME = KeyEvent.KEYCODE_HOME;
     public static final int RESV_KEY_BACK = KeyEvent.KEYCODE_BACK;
     public static final int RESV_KEY_MENU = KeyEvent.KEYCODE_MENU;;

    在onFinishInflate中,获取几个button 的handler,并设置touch事件,添加如下代码:

    view plaincopy to clipboardprint

    1. mHomeBtn = (ImageButton)findViewById(R.id.go_home);   
    2. mBackBtn = (ImageButton)findViewById(R.id.go_back);   
    3. mMenuBtn = (ImageButton)findViewById(R.id.pop_menu);   
    4.   
    5. mHomeBtn.setOnTouchListener(homeOnTouch);   
    6. mBackBtn.setOnTouchListener(backOnTouch);   
    7. mMenuBtn.setOnTouchListener(menuOnTouch);   

     

    各button的touch事件添加如下:

    view plaincopy to clipboardprint

    1. private void sendKeyIntent(int keycode){   
    2.     Intent intent = new Intent(Intent.ACTION_ICONKEY_CHANGED);   
    3.     intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);   
    4.     intent.putExtra("keycode",   keycode);   
    5.     mService.sendIntent(intent);               
    6. }   
    7.   
    8. private OnTouchListener homeOnTouch = new OnTouchListener(){   
    9.     //@Override      
    10.        public boolean onTouch(View v, MotionEvent event)    
    11.        {      
    12.            // TODO Auto-generated method stub        
    13.            switch (event.getAction()) {   
    14.                case MotionEvent.ACTION_UP:   
    15.                {   
    16.                 sendKeyIntent(RESV_KEY_HOME);              
    17.             }   
    18.                break;   
    19.            }   
    20.            return false;      
    21.        }    
    22. };   
    23.   
    24. private OnTouchListener backOnTouch = new OnTouchListener(){   
    25.     //@Override      
    26.        public boolean onTouch(View v, MotionEvent event)    
    27.        {      
    28.            // TODO Auto-generated method stub        
    29.            switch (event.getAction()) {   
    30.                case MotionEvent.ACTION_UP:   
    31.                {   
    32.                 sendKeyIntent(RESV_KEY_BACK);       
    33.                }   
    34.                break;   
    35.            }   
    36.            return false;      
    37.        }    
    38. };   
    39.   
    40. private OnTouchListener menuOnTouch = new OnTouchListener(){   
    41.     //@Override      
    42.        public boolean onTouch(View v, MotionEvent event)    
    43.        {      
    44.            // TODO Auto-generated method stub        
    45.            switch (event.getAction()) {   
    46.                case MotionEvent.ACTION_UP:   
    47.                {   
    48.                 sendKeyIntent(RESV_KEY_MENU);     
    49.                }   
    50.                break;   
    51.            }   
    52.            return false;      
    53.        }    
    54. };  

     

    也就是简单的广播一个intent消息给statusBarPolicy处理。

    为防止点击statusBar上的按钮, 触发标题栏的expend事件, 修改一下函数onInterceptTouchEvent,点击到不属于button区域时才允许解析Motion的event:

    view plaincopy to clipboardprint

    1.     public boolean onInterceptTouchEvent(MotionEvent event) {   
    2.         if(  (event.getX() > mHomeBtn.getRight())       
    3.             &&  (event.getX() < mMenuBtn.getLeft())){         
    4.             return mService.interceptTouchEvent(event)       
    5.                  true : super.onInterceptTouchEvent(event);        
    6.             }        
    7.         return false;   
    8.         //return mService.interceptTouchEvent(event)                  
    9.         //  ? true : super.onInterceptTouchEvent(event);   
    10.     }   
    11. }  
      

     

    修改StatusBarService.java,发送Intent消息需要content,这个目前只能在StatusBarService中添加一个方法:

    view plaincopy to clipboardprint

    1. void sendIntent(Intent intent)   
    2.     {   
    3.      mContext.sendBroadcast(intent);   
    4.     }  
     

     

    要发送intent,需要自己添加Intent:

    在framework/base/core/java/android/content/intent.java中增加

    view plaincopy to clipboardprint

    1. @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)      
    2. public static final String ACTION_ICONKEY_CHANGED = "android.intent.action.ICONKEY_CHANGED";   

     

    接收并处理intent, 如前篇:

    接收并处理intent

    这个就要修改StatusBarPolicy.java了

    首先,在构造函数中加入Intent的filter,注册号这个intent的receiver。

      view plaincopy to clipboardprint

    1. filter.addAction(Intent.ACTION_ICONKEY_CHANGED);  
      

     

    然后再private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() 加入Intent的receiver动作;

    view plaincopy to clipboardprint

    1. else if (action.equals(Intent.ACTION_ICONKEY_CHANGED)) {   
    2.                 Log.d(TAG, "Received ACTION_ICONKEY_CHANGED");   
    3.                 updateIconKeyAction(intent);   
    4.             }  

     

    及处理函数:

    view plaincopy to clipboardprint

    1. private final void updateIconKeyAction(Intent intent){   
    2.     int     keycode = intent.getIntExtra("keycode", -1);   
    3.     IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));   
    4.        
    5.     if(keycode != -1){   
    6.         long now = SystemClock.uptimeMillis();   
    7.   
    8.            KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keycode, 0);   
    9.            KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, keycode, 0);   
    10.   
    11.         try {   
    12.             wm.injectKeyEvent(down, false);   
    13.         }catch (RemoteException e) {   
    14.             Log.i("Input""DeadOjbectException");   
    15.         }   
    16.   
    17.         try{   
    18.             wm.injectKeyEvent(up, false);   
    19.         }catch(RemoteException e) {   
    20.             Log.i("Input""DeadOjbectException");   
    21.         }   
    22.     }   
    23. }  

     

    3. StatusBar通知栏屏蔽按钮

    当拉出expand的通知栏时,按钮的响应非常慢,这时最好将按钮给屏蔽掉,我们在 statusBarView.java中增加两个方法:

    view plaincopy to clipboardprint

    1. public void hiddenHotIcons(){   
    2.     mHomeBtn.setVisibility(View.INVISIBLE);   
    3.     mBackBtn.setVisibility(View.INVISIBLE);   
    4.     mMenuBtn.setVisibility(View.INVISIBLE);   
    5. }   
    6.   
    7. public void showHotIcons(){   
    8.     mHomeBtn.setVisibility(View.VISIBLE);   
    9.     mBackBtn.setVisibility(View.VISIBLE);   
    10.     mMenuBtn.setVisibility(View.VISIBLE);   
    11. }  

     

    拉出或收回通知栏中,就可以调用这个函数来显示或隐藏这几个按钮。

    修改文件: statusBarService.java

    view plaincopy to clipboardprint

    1.   void performExpand() {   
    2.       if (SPEW) Log.d(TAG, "performExpand: mExpanded=" + mExpanded);   
    3.       if ((mDisabled & StatusBarManager.DISABLE_EXPAND) != 0) {   
    4.           return ;   
    5.       }   
    6.       if (mExpanded) {   
    7.           return;   
    8.       }   
    9.   
    10.       // It seems strange to sometimes not expand...   
    11.       if (false) {   
    12.           synchronized (mNotificationData) {   
    13.               if (mNotificationData.size() == 0) {   
    14.                   return;   
    15.               }   
    16.           }   
    17.       }   
    18.          
    19.       mExpanded = true;   
    20.       makeExpandedVisible();   
    21. mStatusBarView.hiddenHotIcons(); // Changed!!!   
    22.       updateExpandedViewPos(EXPANDED_FULL_OPEN);   
    23.   
    24.       if (false) postStartTracing();   
    25.   }   
    26.   
    27.   void performCollapse() {   
    28.       if (SPEW) Log.d(TAG, "performCollapse: mExpanded=" + mExpanded   
    29.               + " mExpandedVisible=" + mExpandedVisible);   
    30.          
    31.       if (!mExpandedVisible) {   
    32.           return;   
    33.       }   
    34.       mExpandedVisible = false;   
    35.       panelSlightlyVisible(false);   
    36.       mExpandedParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;   
    37.       mExpandedParams.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;   
    38.       mExpandedDialog.getWindow().setAttributes(mExpandedParams);   
    39.       mTrackingView.setVisibility(View.GONE);   
    40.   
    41. mStatusBarView.showHotIcons(); // Changed!!!!   
    42.       if ((mDisabled & StatusBarManager.DISABLE_NOTIFICATION_ICONS) == 0) {   
    43.           setNotificationIconVisibility(true, com.android.internal.R.anim.fade_in);   
    44.       }   
    45.       setDateViewVisibility(false, com.android.internal.R.anim.fade_out);   
    46.          
    47.       if (!mExpanded) {   
    48.           return;   
    49.       }   
    50.       mExpanded = false;   
    51.   }