ImageFilterBorder.java revision 71f04cbaedbb89e313e0b86b531640db2d3f6016
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
17package com.android.gallery3d.filtershow.filters;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Rect;
22import android.graphics.drawable.Drawable;
23
24import com.android.gallery3d.R;
25
26public class ImageFilterBorder extends ImageFilter {
27    private static final float NINEPATCH_ICON_SCALING = 10;
28    private static final float BITMAP_ICON_SCALING = 1 / 3.0f;
29    Drawable mNinePatch = null;
30
31    @Override
32    public ImageFilter clone() throws CloneNotSupportedException {
33        ImageFilterBorder filter = (ImageFilterBorder) super.clone();
34        filter.setDrawable(mNinePatch);
35        return filter;
36    }
37
38    public ImageFilterBorder(Drawable ninePatch) {
39        setFilterType(TYPE_BORDER);
40        mName = "Border";
41        mNinePatch = ninePatch;
42    }
43
44    public boolean isNil() {
45        if (mNinePatch == null) {
46            return true;
47        }
48        return false;
49    }
50
51    @Override
52    public int getTextId() {
53        return R.string.borders;
54    }
55
56    @Override
57    public boolean showParameterValue() {
58        return false;
59    }
60
61    @Override
62    public boolean showEditingControls() {
63        return false;
64    }
65
66    @Override
67    public boolean showUtilityPanel() {
68        return false;
69    }
70
71    @Override
72    public boolean same(ImageFilter filter) {
73        boolean isBorderFilter = super.same(filter);
74        if (!isBorderFilter) {
75            return false;
76        }
77        if (!(filter instanceof ImageFilterBorder)) {
78            return false;
79        }
80        ImageFilterBorder borderFilter = (ImageFilterBorder) filter;
81        if (mNinePatch != borderFilter.mNinePatch) {
82            return false;
83        }
84        return true;
85    }
86
87    public void setDrawable(Drawable ninePatch) {
88        // TODO: for now we only use nine patch
89        mNinePatch = ninePatch;
90    }
91
92    public Bitmap applyHelper(Bitmap bitmap, float scale1, float scale2 ) {
93        int w = bitmap.getWidth();
94        int h = bitmap.getHeight();
95        Rect bounds = new Rect(0, 0, (int) (w * scale1), (int) (h * scale1));
96        Canvas canvas = new Canvas(bitmap);
97        canvas.scale(scale2, scale2);
98        mNinePatch.setBounds(bounds);
99        mNinePatch.draw(canvas);
100        return bitmap;
101    }
102
103    @Override
104    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
105        if (mNinePatch == null) {
106            return bitmap;
107        }
108        float scale2 = scaleFactor * 2.0f;
109        float scale1 = 1 / scale2;
110        return applyHelper(bitmap, scale1, scale2);
111    }
112
113    @Override
114    public Bitmap iconApply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
115        if (mNinePatch == null) {
116            return bitmap;
117        }
118        float scale1 = NINEPATCH_ICON_SCALING;
119        float scale2 = BITMAP_ICON_SCALING;
120        return applyHelper(bitmap, scale1, scale2);
121    }
122}
123