1 /* 2 * Copyright (C) 2015 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.camera.ui; 18 19 import android.graphics.Canvas; 20 import android.graphics.ColorFilter; 21 import android.graphics.Paint; 22 import android.graphics.PixelFormat; 23 import android.graphics.Rect; 24 import android.graphics.RectF; 25 import android.graphics.drawable.Drawable; 26 27 /** 28 * Drawable that takes a {@link RectF} as a screen, and draws around that 29 * screen to fill margins between the screen and the edge of the {@link Canvas} 30 * when drawing. 31 */ 32 public class MarginDrawable extends Drawable { 33 34 private RectF mScreen = new RectF(0, 0, 0, 0); 35 private final Paint mPaint; 36 MarginDrawable(int color)37 public MarginDrawable(int color) { 38 super(); 39 40 mPaint = new Paint(); 41 mPaint.setAntiAlias(true); 42 mPaint.setColor(color); 43 } 44 45 /** 46 * Set the screen around which will be drawn margins. If the screen rect 47 * has no area (zero width or height), no margins will be drawn. 48 * 49 * @param screen A {@link RectF} describing the screen dimensions 50 */ setScreen(RectF screen)51 public void setScreen(RectF screen) { 52 mScreen.set(screen); 53 invalidateSelf(); 54 } 55 56 @Override draw(Canvas canvas)57 public void draw(Canvas canvas) { 58 RectF s = mScreen; 59 if (s.top < s.bottom && s.left < s.right) { 60 Rect cb = canvas.getClipBounds(); 61 if (s.top > 0) { 62 canvas.drawRect(0, 0, cb.right, s.top + 1, mPaint); 63 } 64 if (s.left > 0) { 65 canvas.drawRect(0, s.top, s.left + 1, s.bottom, mPaint); 66 } 67 if (s.right < cb.right) { 68 canvas.drawRect(s.right - 1, s.top, cb.right, s.bottom, mPaint); 69 } 70 if (s.bottom < cb.bottom) { 71 canvas.drawRect(0, s.bottom - 1, cb.right, cb.bottom, mPaint); 72 } 73 } 74 } 75 76 @Override setAlpha(int alpha)77 public void setAlpha(int alpha) { 78 } 79 80 @Override setColorFilter(ColorFilter cf)81 public void setColorFilter(ColorFilter cf) { 82 } 83 84 @Override getOpacity()85 public int getOpacity() { 86 return PixelFormat.OPAQUE; 87 } 88 } 89