OSDN Git Service

d6d3c86d4ed3a5512649fed1c405b4920c348f66
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / imageshow / ImageSmallBorder.java
1 /*
2  * Copyright (C) 2012 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.gallery3d.filtershow.imageshow;
18
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.Path;
25 import android.graphics.Rect;
26 import android.graphics.RectF;
27 import android.graphics.Region;
28 import android.util.AttributeSet;
29
30 public class ImageSmallBorder extends ImageSmallFilter {
31
32     // TODO: move this to xml.
33     protected final int mSelectedBackgroundColor = Color.WHITE;
34     protected final int mInnerBorderColor = Color.BLACK;
35     protected final int mInnerBorderWidth = 2;
36     protected final float mImageScaleFactor = 3.5f;
37
38     public ImageSmallBorder(Context context) {
39         super(context);
40     }
41
42     public ImageSmallBorder(Context context, AttributeSet attrs) {
43         super(context, attrs);
44     }
45
46     @Override
47     public void onDraw(Canvas canvas) {
48         requestFilteredImages();
49         canvas.drawColor(mBackgroundColor);
50         // TODO: simplify & make faster...
51         RectF border = new RectF(mMargin, 2*mMargin, getWidth() - mMargin - 1, getWidth());
52
53         if (mIsSelected) {
54             mPaint.setColor(mSelectedBackgroundColor);
55             canvas.drawRect(0, mMargin, getWidth(), getWidth() + mMargin, mPaint);
56         }
57
58         mPaint.setColor(mInnerBorderColor);
59         mPaint.setStrokeWidth(mInnerBorderWidth);
60         Path path = new Path();
61         path.addRect(border, Path.Direction.CCW);
62         mPaint.setStyle(Paint.Style.STROKE);
63         canvas.drawPath(path, mPaint);
64         mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
65         canvas.save();
66         canvas.clipRect(mMargin + 1, 2*mMargin, getWidth() - mMargin - 2, getWidth() - 1,
67                 Region.Op.INTERSECT);
68         canvas.translate(mMargin + 1, 2*mMargin + 1);
69         canvas.scale(mImageScaleFactor, mImageScaleFactor);
70         Rect d = new Rect(0, 0, getWidth(), getWidth());
71         drawImage(canvas, getFilteredImage(), d);
72         canvas.restore();
73     }
74
75     @Override
76     public void drawImage(Canvas canvas, Bitmap image, Rect d) {
77         if (image != null) {
78             int iw = image.getWidth();
79             int ih = image.getHeight();
80             Rect s = new Rect(0, 0, iw, iw);
81             canvas.drawBitmap(image, s, d, mPaint);
82         }
83     }
84 }