分享

向android 的状态栏中加入快捷按钮(home,back,menu等等)的方法(续) ...

 ssp2 2010-12-17

上一篇文章中谈到的加入快捷按钮的方法,实际上还是太过麻烦。那篇博客是在我刚接触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

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

 

xml_stat_back.xml

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

xml_stat_menu.xml

 

 

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

 

修改status_bar.xml,如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--   
  3. /* apps/common/assets/default/default/skins/StatusBar.xml   
  4. **   
  5. ** Copyright 2006, The Android Open Source Project   
  6. **   
  7. ** Licensed under the Apache License, Version 2.0 (the "License");    
  8. ** you may not use this file except in compliance with the License.    
  9. ** You may obtain a copy of the License at    
  10. **   
  11. **     http://www./licenses/LICENSE-2.0    
  12. **   
  13. ** Unless required by applicable law or agreed to in writing, software    
  14. ** distributed under the License is distributed on an "AS IS" BASIS,    
  15. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    
  16. ** See the License for the specific language governing permissions and    
  17. ** limitations under the License.   
  18. */   
  19. -->  
  20.   
  21. <!--    android:background="@drawable/status_bar_closed_default_background" -->  
  22. <com.android.server.status.StatusBarView xmlns:android="http://schemas./apk/res/android"    
  23.     android:background="@drawable/statusbar_background"  
  24.     android:orientation="vertical"  
  25.     android:focusable="true"  
  26.     android:descendantFocusability="afterDescendants"  
  27.     >  
  28.   
  29.     <LinearLayout android:id="@+id/icons"  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="fill_parent"  
  32.         android:orientation="horizontal">  
  33.   
  34.     <ImageButton android:id="@+id/go_home"  
  35.         android:layout_width="36px"  
  36.         android:layout_height="36px"  
  37.         android:layout_marginRight="15px"  
  38.         android:layout_marginLeft="10px"  
  39.         android:layout_marginTop="5px"  
  40.         android:clickable="true"    
  41.         android:background="@drawable/xml_stat_home"  
  42.         />    
  43.     
  44.            
  45.         <com.android.server.status.IconMerger android:id="@+id/notificationIcons"  
  46.             android:layout_width="0dip"  
  47.             android:layout_weight="1"  
  48.             android:layout_height="fill_parent"  
  49.             android:layout_alignParentLeft="true"  
  50.             android:paddingLeft="6dip"  
  51.             android:gravity="center_vertical"  
  52.             android:orientation="horizontal"/>     
  53.                
  54.         <LinearLayout android:id="@+id/statusIcons"  
  55.             android:layout_width="wrap_content"  
  56.             android:layout_height="fill_parent"  
  57.             android:layout_alignParentRight="true"  
  58.             android:paddingRight="6dip"  
  59.             android:gravity="center_vertical"  
  60.             android:orientation="horizontal"/>       
  61.   
  62.       <ImageButton android:id="@+id/pop_menu"  
  63.         android:layout_width="36px"  
  64.         android:layout_height="36px"  
  65.         android:layout_marginRight="15px"  
  66.         android:layout_marginLeft="10px"  
  67.         android:layout_marginTop="5px"  
  68.         android:clickable="true"    
  69.         android:background="@drawable/xml_stat_menu"  
  70.         />           
  71.      <ImageButton android:id="@+id/go_back"  
  72.         android:layout_width="36px"  
  73.         android:layout_height="36px"  
  74.         android:layout_marginRight="15px"  
  75.         android:layout_marginLeft="10px"  
  76.         android:layout_marginTop="5px"  
  77.         android:clickable="true"    
  78.         android:background="@drawable/xml_stat_back"  
  79.         />            
  80.     </LinearLayout>  
  81.            
  82.     <LinearLayout android:id="@+id/ticker"  
  83.         android:layout_width="fill_parent"  
  84.         android:layout_height="fill_parent"  
  85.         android:paddingLeft="6dip"  
  86.         android:animationCache="false"  
  87.         android:orientation="horizontal" >  
  88.         <ImageSwitcher android:id="@+id/tickerIcon"  
  89.             android:layout_width="wrap_content"  
  90.             android:layout_height="fill_parent"  
  91.             android:layout_marginRight="8dip"  
  92.             >  
  93.             <com.android.server.status.AnimatedImageView  
  94.                 android:layout_width="36dip"  
  95.                 android:layout_height="36dip"  
  96.                 />  
  97.             <com.android.server.status.AnimatedImageView  
  98.                 android:layout_width="36dip"  
  99.                 android:layout_height="36dip"  
  100.                 />  
  101.         </ImageSwitcher>  
  102.         <com.android.server.status.TickerView android:id="@+id/tickerText"  
  103.             android:layout_width="0dip"  
  104.             android:layout_weight="1"  
  105.             android:layout_height="wrap_content"  
  106.             android:paddingTop="2dip"  
  107.             android:paddingRight="10dip">  
  108.             <TextView  
  109.                 android:layout_width="fill_parent"  
  110.                 android:layout_height="wrap_content"  
  111.                 android:singleLine="true"  
  112.                 android:textColor="#ff000000" />  
  113.             <TextView  
  114.                 android:layout_width="fill_parent"  
  115.                 android:layout_height="wrap_content"  
  116.                 android:singleLine="true"  
  117.                 android:textColor="#ff000000" />  
  118.         </com.android.server.status.TickerView>  
  119.     </LinearLayout>  
  120.   
  121.     <com.android.server.status.DateView android:id="@+id/date"  
  122.         android:layout_width="wrap_content"  
  123.         android:layout_height="fill_parent"  
  124.         android:singleLine="true"  
  125.         android:textSize="20sp"  
  126.         android:textStyle="bold"  
  127.         android:gravity="center_vertical|left"  
  128.         android:paddingLeft="6px"  
  129.         android:paddingRight="6px"  
  130.         android:textColor="?android:attr/textColorPrimaryInverse"  
  131.         android:background="@drawable/statusbar_background"  
  132.         />  
  133. </com.android.server.status.StatusBarView>  

 

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

2。 添加按钮的动作效果

在statusBarView.java中,活的button的handler

类中新增加三个成员:

  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事件,添加如下代码:

  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事件添加如下:

  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:

  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中添加一个方法:

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

 

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

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

  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。

  

  1. filter.addAction(Intent.ACTION_ICONKEY_CHANGED);  
  

 

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

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

 

及处理函数:

  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中增加两个方法:

  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

  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.   }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多