1. 程式人生 > >android6.0在狀態列新增一鍵截圖

android6.0在狀態列新增一鍵截圖

  • 在配置檔案frameworks/base/packages/SystemUI/res/values/config.xml下新增screenshot
  1. <stringname="quick_setting_tiles_default"translatable="false">
  2.     wifi,bt,inversion,dnd,cell,airplane,rotation,flashlight,location,cast,hotspot,screenshot
  3. </string>
  • 在frameworks/base/packages/SystemUI/src/com/android.systemui/statusbar/phone/QSTileHost.java中得到config.xml中的資料,並進行判斷,在方法createTile()中新增
  1. elseif(tileSpec.equals(“screenshot”)) returnnew ScreenShotTile(this); 
  • 在QSTileHost.java中還需要往構造方法中傳入一個ScreenShotController物件,需要新增如下內容:

    import com.mediatek.systemui.statusbar.policy.ScreenShotController;
    
    private final ScreenShotController mScreenShotController;
    
    public QSTileHost(…………ScreenShotController screenshot){
    ………….
    mScreenShotController=screenshot;
    ………….
    }
    
    public ScreenShotController getScreenShotController(){
    	return mScreenShotController;
    }
    
  • 這裡涉及到了ScreenShotController,於是我們要去定義它,ScreenShotController是一個介面,內容如下:
    package com.mediatek.systemui.statusbar.policy;
    
    public interface ScreenShotController{
    		boolean isSupportScreenShot();
    }
    
  • 完成ScreenShotTile類,在這個方法中需要完成狀態列圖示顯示和截圖操作,可以參考其他生成Tile的方法來完成顯示圖示的方法。該類繼承了QSTile類,所以要清楚QSTile這個類的內容。QSTile類中需要新增的內容如下:
    import com.mediatek.systemui.statusbar.policy.ScreenShotController;
    public interface Host{
    ……
    ScreenShotController getScreenShotController();
    …….
    }
  • ScreenShotTile類的實現如下所示:
  1. publicclass ScreenShotTile extends QSTile<QSTile.BooleanState> {  
  2.     privatestaticfinal String TAG = "ScreenShotTile";  
  3.     privatestaticfinalint SCREEN_SHOT_MESSAGE = 10000;  
  4.     privatestatic GlobalScreenshot mScreenshot;  
  5.     Handler mHandler = new Handler() {  
  6.         publicvoid handleMessage(Message msg) {  
  7.             switch (msg.what) {  
  8.             case TAKE_SCREEN_SHOT_MESSAGE:  
  9.                 final Messenger callback = msg.replyTo;  
  10.                 if (mScreenshot == null) {  
  11.                     mScreenshot = new GlobalScreenshot(mContext);  
  12.                 }  
  13.                 mScreenshot.takeScreenshot(new Runnable() {  
  14.                     @Overridepublicvoid run() {  
  15.                         Message reply = Message.obtain(null1);  
  16.                         try {  
  17.                             if(callback != null){  
  18.                                 callback.send(reply);  
  19.                             }  
  20.                         }catch(RemoteException e){  
  21.                         }  
  22.                     }  
  23.                 }, msg.arg1 > 0, msg.arg2 > 0);  
  24.                 break;                      
  25.             default:  
  26.                 break;  
  27.             }  
  28.         }  
  29.     };  
  30.     public TakeScreenShotTile(Host host) {  
  31.         super(host);  
  32.     }  
  33.     @Override
  34.     protected BooleanState newTileState() {  
  35.         returnnew BooleanState();  
  36.     }  
  37.     @Override
  38.     publicvoid setListening(boolean listening) {  
  39.     }  
  40.     @Override
  41.     protectedvoid handleClick() {  
  42.         mHost.startSettingsActivity(new Intent());  
  43.         Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);  
  44.         mHandler.sendMessageDelayed(msg,1000);  
  45.     }  
  46.     @Override
  47.     protectedvoid handleLongClick() {  
  48.         mHost.startSettingsActivity(new Intent());  
  49.         Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);  
  50.         mHandler.sendMessageDelayed(msg,1000);  
  51.     }  
  52.     @Override
  53.     protectedvoid handleUpdateState(BooleanState state, Object arg) {  
  54.         state.value=false;  
  55.         state.autoMirrorDrawable=false;  
  56.         state.label = mContext.getString(R.string.screenshot);//載入字串資源
  57.         state.icon = ResourceIcon.get(R.drawable.ic_qs_screenshot);//載入圖片資源
  58. @Override
  59.  public int getMetricsCategory(){ 
  60. return MetricsLogger.QS_SCREEN_SHOT; 
  61. }
  • 在frameworks/base/packages/SystemUI/res/中新增對應的字串和相應解析度下的圖片資源。

1.      在frameworks/base/packages/SystemUI/res/values/strings.xml新增

  1. <stringname=screenshot>ScreenShot</string>

2.      在frameworks/base/packages/SystemUI/res/drawable目錄下新建檔案ic_qs_screenshot.xml,檔案內容如下:

  1. <vectorxmlns:android="http://schemas.android.com/apk/res/android"
  2.           android:width="64dp"
  3.           android:height="64dp"
  4.           android:viewportWidth="48.0"
  5.           android:viewportHeight="48.0"
  6.     <path
  7.         android:fillColor="#FFFFFFFF"
  8.         android:pathData="M 48.00,0.00  
  9.            C 48.00,0.00 48.00,48.00 48.00,48.00  
  10.              48.00,48.00 0.00,48.00 0.00,48.00  
  11.              0.00,48.00 0.00,0.00 0.00,0.00  
  12.              0.00,0.00 48.00,0.00 48.00,0.00 Z  
  13.            M 12.74,5.34  
  14.            C 10.77,4.60 8.82,7.07 7.61,8.31  
  15.              4.18,11.82 4.05,13.40 9.00,15.00  
  16.              9.00,15.00 9.00,39.00 9.00,39.00  
  17.              9.00,39.00 33.00,39.00 33.00,39.00  
  18.              33.44,40.36 33.66,42.07 35.26,42.66  
  19.              37.23,43.40 39.18,40.93 40.39,39.69  
  20.              43.82,36.18 43.95,34.60 39.00,33.00  
  21.              39.00,33.00 39.00,15.00 39.00,15.00  
  22.              31.35,20.68 33.00,24.31 33.00,33.00  
  23.              33.00,33.00 18.00,33.00 18.00,33.00  
  24.              23.35,27.65 37.10,14.14 40.00,8.00  
  25.              40.00,8.00 39.00,7.00 39.00,7.00  
  26.              33.12,9.79 19.63,25.36 15.00,31.00  
  27.              15.00,31.00 15.00,15.00 15.00,15.00  
  28.              22.33,15.00 27.29,16.58 31.00,9.00  
  29.              31.00,9.00 15.00,9.00 15.00,9.00  
  30.              14.56,7.64 14.34,5.93 12.74,5.34 Z"/>
  31. </vector>

這裡使用了vector繪製向量圖的方式生成狀態列圖示,而不是載入具體的圖片資源,而path裡的內容決定了圖片的樣式。

  • 在frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java中修改進行截圖操作的類和方法的許可權,如下所示:

classGlobalScreenshot 宣告為 public

method takeScreenshot 宣告為public

GlobalScreenshot是實現截圖操作,圖片儲存,截圖動畫的類.

實現截圖的操作的方法是takeScreenshot();

  • PhoneStatusBar中定義了一個QSTileHost物件,實參需要傳入一個ScreensShotController物件,這裡傳入的是ScreensShotController的實現類ScreensShotControllerImpl的物件,修改如下

    import com.mediatek.systemui.statusbar.policy.ScreenShotControllerImpl;
    ScreenShotControllerImpl mScreenShitController;
    
    final QSTileHost qsh=new QSTileHost(.................mScreenShotController);
  • ScreenShotControllerImpl是介面ScreenShotController的實現類,內容如下:

    package com.mediatek.systemui.statusbar.policy;
    import android.content.Context;
    public class ScreenShotControllerImpl implements ScreenShotController{
    private static final String TAG="ScreenShotControllerImpl";
    private final Context mContext;
    public ScreenShotControllerImpl(Context context){
    mContext=context;
    }
    @Override
    public boolean isSupportScreenShot(){
    return ture;
    }
    }
  • QStuner中存在一個類繼承了QSTileHost,同時呼叫了父類的構造方法,因此需要新增一個引數。

    public CustomHost(Context context){
    super(context,...............,null,null,null);
    }
    最後的那個null是這次修改新增上的。