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
17package com.android.camera.ui;
18
19import android.graphics.Canvas;
20import android.graphics.ColorFilter;
21import android.graphics.Paint;
22import android.graphics.PixelFormat;
23import android.graphics.Rect;
24import android.graphics.RectF;
25import 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 */
32public class MarginDrawable extends Drawable {
33
34    private RectF mScreen = new RectF(0, 0, 0, 0);
35    private final Paint mPaint;
36
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     */
51    public void setScreen(RectF screen) {
52        mScreen.set(screen);
53        invalidateSelf();
54    }
55
56    @Override
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
77    public void setAlpha(int alpha) {
78    }
79
80    @Override
81    public void setColorFilter(ColorFilter cf) {
82    }
83
84    @Override
85    public int getOpacity() {
86        return PixelFormat.OPAQUE;
87    }
88}
89