android6.0在状态栏添加一键截屏

  • 在配置文件frameworks/base/packages/SystemUI/res/values/config.xml下添加screenshot

[html]  view plain  copy
  1. <string name="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()中添加

[java]  view plain  copy
  1. else if(tileSpec.equals(“screenshot”)) return new 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类的实现如下所示:

[java]  view plain  copy
  1. public class ScreenShotTile extends QSTile {  
  2.     private static final String TAG = "ScreenShotTile";  
  3.     private static final int SCREEN_SHOT_MESSAGE = 10000;  
  4.     private static GlobalScreenshot mScreenshot;  
  5.       
  6.     Handler mHandler = new Handler() {  
  7.         public void handleMessage(Message msg) {  
  8.             switch (msg.what) {  
  9.             case TAKE_SCREEN_SHOT_MESSAGE:  
  10.                 final Messenger callback = msg.replyTo;  
  11.                 if (mScreenshot == null) {  
  12.                     mScreenshot = new GlobalScreenshot(mContext);  
  13.                 }  
  14.                 mScreenshot.takeScreenshot(new Runnable() {  
  15.                     @Override public void run() {  
  16.                         Message reply = Message.obtain(null1);  
  17.                         try {  
  18.                             if(callback != null){  
  19.                                 callback.send(reply);  
  20.                             }  
  21.                         }catch(RemoteException e){  
  22.                         }  
  23.                     }  
  24.                 }, msg.arg1 > 0, msg.arg2 > 0);  
  25.                 break;                      
  26.             default:  
  27.                 break;  
  28.             }  
  29.         }  
  30.     };  
  31.       
  32.     public TakeScreenShotTile(Host host) {  
  33.         super(host);  
  34.     }  
  35.   
  36.     @Override  
  37.     protected BooleanState newTileState() {  
  38.         return new BooleanState();  
  39.     }  
  40.   
  41.     @Override  
  42.     public void setListening(boolean listening) {  
  43.     }  
  44.   
  45.     @Override  
  46.     protected void handleClick() {  
  47.         mHost.startSettingsActivity(new Intent());  
  48.         Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);  
  49.         mHandler.sendMessageDelayed(msg,1000);  
  50.     }  
  51.     @Override  
  52.     protected void handleLongClick() {  
  53.         mHost.startSettingsActivity(new Intent());  
  54.         Message msg = mHandler.obtainMessage(SCREEN_SHOT_MESSAGE);  
  55.         mHandler.sendMessageDelayed(msg,1000);  
  56.     }  
  57.   
  58.     @Override  
  59.     protected void handleUpdateState(BooleanState state, Object arg) {  
  60.         state.value=false;  
  61.         state.autoMirrorDrawable=false;  
  62.         state.label = mContext.getString(R.string.screenshot);//加载字符串资源  
  63.         state.icon = ResourceIcon.get(R.drawable.ic_qs_screenshot);//加载图片资源
  64. @Override
  65.  public int getMetricsCategory(){ 
  66. return MetricsLogger.QS_SCREEN_SHOT; 
  67. }
 
  

  • 在frameworks/base/packages/SystemUI/res/中添加对应的字符串和相应分辨率下的图片资源。

1.      在frameworks/base/packages/SystemUI/res/values/strings.xml添加

[html]  view plain  copy
  1. <string name=screenshot>ScreenShotstring>  

2.      在frameworks/base/packages/SystemUI/res/drawable目录下新建文件ic_qs_screenshot.xml,文件内容如下:

[html]  view plain  copy
  1. <vector xmlns: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是这次修改添加上的。


     



你可能感兴趣的:(android,状态栏,截屏)