Home | History | Annotate | Download | only in screenshot
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.systemui.screenshot;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.graphics.Canvas;
     22 import android.graphics.Color;
     23 import android.graphics.Paint;
     24 import android.graphics.Point;
     25 import android.graphics.PorterDuff;
     26 import android.graphics.PorterDuffXfermode;
     27 import android.graphics.Rect;
     28 import android.util.AttributeSet;
     29 import android.view.View;
     30 
     31 /**
     32  * Draws a selection rectangle while taking screenshot
     33  */
     34 public class ScreenshotSelectorView extends View {
     35     private Point mStartPoint;
     36     private Rect mSelectionRect;
     37     private final Paint mPaintSelection, mPaintBackground;
     38 
     39     public ScreenshotSelectorView(Context context) {
     40         this(context, null);
     41     }
     42 
     43     public ScreenshotSelectorView(Context context, @Nullable AttributeSet attrs) {
     44         super(context, attrs);
     45         mPaintBackground = new Paint(Color.BLACK);
     46         mPaintBackground.setAlpha(160);
     47         mPaintSelection = new Paint(Color.TRANSPARENT);
     48         mPaintSelection.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
     49     }
     50 
     51     public void startSelection(int x, int y) {
     52         mStartPoint = new Point(x, y);
     53         mSelectionRect = new Rect(x, y, x, y);
     54     }
     55 
     56     public void updateSelection(int x, int y) {
     57         if (mSelectionRect != null) {
     58             mSelectionRect.left = Math.min(mStartPoint.x, x);
     59             mSelectionRect.right = Math.max(mStartPoint.x, x);
     60             mSelectionRect.top = Math.min(mStartPoint.y, y);
     61             mSelectionRect.bottom = Math.max(mStartPoint.y, y);
     62             invalidate();
     63         }
     64     }
     65 
     66     public Rect getSelectionRect() {
     67         return mSelectionRect;
     68     }
     69 
     70     public void stopSelection() {
     71         mStartPoint = null;
     72         mSelectionRect = null;
     73     }
     74 
     75     @Override
     76     public void draw(Canvas canvas) {
     77         canvas.drawRect(mLeft, mTop, mRight, mBottom, mPaintBackground);
     78         if (mSelectionRect != null) {
     79             canvas.drawRect(mSelectionRect, mPaintSelection);
     80         }
     81     }
     82 }
     83