android6.0在狀態列新增一鍵截圖
阿新 • • 發佈:2018-12-29
- 在配置檔案frameworks/base/packages/SystemUI/res/values/config.xml下新增screenshot
- <stringname="quick_setting_tiles_default"translatable="false">
- wifi,bt,inversion,dnd,cell,airplane,rotation,flashlight,location,cast,hotspot,screenshot
- </string>
- 在frameworks/base/packages/SystemUI/src/com/android.systemui/statusbar/phone/QSTileHost.java中得到config.xml中的資料,並進行判斷,在方法createTile()中新增
- 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類的實現如下所示:
- publicclass ScreenShotTile extends QSTile<QSTile.BooleanState> {
- privatestaticfinal String TAG = "ScreenShotTile";
- privatestaticfinalint SCREEN_SHOT_MESSAGE = 10000;
- privatestatic GlobalScreenshot mScreenshot;
- Handler mHandler = new Handler() {
- publicvoid handleMessage(Message msg) {
- switch (msg.what) {
- case TAKE_SCREEN_SHOT_MESSAGE:
- final Messenger callback = msg.replyTo;
- if (mScreenshot == null) {
- mScreenshot = new GlobalScreenshot(mContext);
- }
- mScreenshot.takeScreenshot(new Runnable() {
- @Overridepublicvoid run() {
- Message reply = Message.obtain(null, 1);
- try {
- if(callback != null){
- callback.send(reply);
- }
- }catch(RemoteException e){
- }
- }
- }, msg.arg1 > 0, msg.arg2 > 0);
- break;
- default:
- break;
- }
- }
- };
- public TakeScreenShotTile(Host host) {
- super(host);
- }
- @Override
- protected BooleanState newTileState() {
- returnnew BooleanState();
- }
- @Override
- publicvoid setListening(boolean listening) {
- }
- @Override
- protectedvoid handleClick() {
- mHost.startSettingsActivity(new Intent());
- Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);
- mHandler.sendMessageDelayed(msg,1000);
- }
- @Override
- protectedvoid handleLongClick() {
- mHost.startSettingsActivity(new Intent());
- Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);
- mHandler.sendMessageDelayed(msg,1000);
- }
- @Override
- protectedvoid handleUpdateState(BooleanState state, Object arg) {
- state.value=false;
- state.autoMirrorDrawable=false;
- state.label = mContext.getString(R.string.screenshot);//載入字串資源
- state.icon = ResourceIcon.get(R.drawable.ic_qs_screenshot);//載入圖片資源}
- @Override
- public int getMetricsCategory(){
- return MetricsLogger.QS_SCREEN_SHOT;
- }
- }
- 在frameworks/base/packages/SystemUI/res/中新增對應的字串和相應解析度下的圖片資源。
1. 在frameworks/base/packages/SystemUI/res/values/strings.xml新增
- <stringname=screenshot>ScreenShot</string>
2. 在frameworks/base/packages/SystemUI/res/drawable目錄下新建檔案ic_qs_screenshot.xml,檔案內容如下:
- <vectorxmlns:android="http://schemas.android.com/apk/res/android"
- android:width="64dp"
- android:height="64dp"
- android:viewportWidth="48.0"
- android:viewportHeight="48.0"
- <path
- android:fillColor="#FFFFFFFF"
- android:pathData="M 48.00,0.00
- C 48.00,0.00 48.00,48.00 48.00,48.00
- 48.00,48.00 0.00,48.00 0.00,48.00
- 0.00,48.00 0.00,0.00 0.00,0.00
- 0.00,0.00 48.00,0.00 48.00,0.00 Z
- M 12.74,5.34
- C 10.77,4.60 8.82,7.07 7.61,8.31
- 4.18,11.82 4.05,13.40 9.00,15.00
- 9.00,15.00 9.00,39.00 9.00,39.00
- 9.00,39.00 33.00,39.00 33.00,39.00
- 33.44,40.36 33.66,42.07 35.26,42.66
- 37.23,43.40 39.18,40.93 40.39,39.69
- 43.82,36.18 43.95,34.60 39.00,33.00
- 39.00,33.00 39.00,15.00 39.00,15.00
- 31.35,20.68 33.00,24.31 33.00,33.00
- 33.00,33.00 18.00,33.00 18.00,33.00
- 23.35,27.65 37.10,14.14 40.00,8.00
- 40.00,8.00 39.00,7.00 39.00,7.00
- 33.12,9.79 19.63,25.36 15.00,31.00
- 15.00,31.00 15.00,15.00 15.00,15.00
- 22.33,15.00 27.29,16.58 31.00,9.00
- 31.00,9.00 15.00,9.00 15.00,9.00
- 14.56,7.64 14.34,5.93 12.74,5.34 Z"/>
- </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,同時呼叫了父類的構造方法,因此需要新增一個引數。
最後的那個null是這次修改新增上的。public CustomHost(Context context){ super(context,...............,null,null,null); }