国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
向android 的狀態(tài)欄中加入快捷按鈕(home,back,menu等等)的方法(續(xù)) ...

上一篇文章中談到的加入快捷按鈕的方法,實(shí)際上還是太過(guò)麻煩。那篇博客是在我剛接觸android源代碼沒(méi)幾天時(shí),參考網(wǎng)上的介紹方法,自己看了下源代碼嘗試著寫(xiě)了一個(gè)。 不過(guò)那個(gè)方法,是我直接用貼圖的方法實(shí)現(xiàn)了按鈕特效,這實(shí)在是太浪費(fèi)了,最近一直仍有朋友問(wèn)我那篇文章中的問(wèn)題,我想還是重寫(xiě)一下,用一個(gè)更簡(jiǎn)單點(diǎn)的方法,直接使用android的ImageButton控件,通過(guò)其OnTouchListener方法操作即可,Button的高亮與否完全由系統(tǒng)處理,這樣也不會(huì)出現(xiàn)button高亮不消失的bug。以下方法在android 2.1 上編譯調(diào)試通過(guò)

1。 準(zhǔn)備資源,修改XML文件

和上篇文章一樣,準(zhǔn)備幾張圖,這里我們準(zhǔn)備添加home back和menu圖標(biāo),就需要準(zhǔn)備6張圖,三張普通狀態(tài),三張按下的高亮狀態(tài)圖標(biāo):

stat_home.png

stat_home_pressed.png

stat_back.png

stat_back_pressed.png

stat_menu.png

stat_menu_pressed.png

同時(shí),在 Frameworks/base/core/res/res/drawable下創(chuàng)建三個(gè)imageButton的xml文件:

xml_stat_home.xml

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <selector    
  3.     xmlns:android="http://schemas.android.com/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.android.com/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.android.com/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.apache.org/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.android.com/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。 添加按鈕的動(dòng)作效果

在statusBarView.java中,活的button的handler

類中新增加三個(gè)成員:

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

 

增加三個(gè)常量:

 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中,獲取幾個(gè)button 的handler,并設(shè)置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. };  

 

也就是簡(jiǎn)單的廣播一個(gè)intent消息給statusBarPolicy處理。

為防止點(diǎn)擊statusBar上的按鈕, 觸發(fā)標(biāo)題欄的expend事件, 修改一下函數(shù)onInterceptTouchEvent,點(diǎn)擊到不屬于button區(qū)域時(shí)才允許解析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,發(fā)送Intent消息需要content,這個(gè)目前只能在StatusBarService中添加一個(gè)方法:

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

 

要發(fā)送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

這個(gè)就要修改StatusBarPolicy.java了

首先,在構(gòu)造函數(shù)中加入Intent的filter,注冊(cè)號(hào)這個(gè)intent的receiver。

  

  1. filter.addAction(Intent.ACTION_ICONKEY_CHANGED);  
  

 

然后再private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() 加入Intent的receiver動(dòng)作;

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

 

及處理函數(shù):

  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通知欄屏蔽按鈕

當(dāng)拉出expand的通知欄時(shí),按鈕的響應(yīng)非常慢,這時(shí)最好將按鈕給屏蔽掉,我們?cè)?statusBarView.java中增加兩個(gè)方法:

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

 

拉出或收回通知欄中,就可以調(diào)用這個(gè)函數(shù)來(lái)顯示或隱藏這幾個(gè)按鈕。

修改文件: 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.   }  
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android Layout XML屬性研究
Android 用AudioManager類控制音量
Android2.2——API中文文檔3
android如何創(chuàng)建一個(gè)透明的ImageButton
[可疑樣本bangcleplugin] 我被這個(gè)手機(jī)木馬騙了接近2W
Android ImageView圖片自適應(yīng)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服