summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/imageshow/ImageSmallFilter.java
blob: 2a3ee28564fd859d579950728849ac3a98339fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.imageshow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.android.gallery3d.filtershow.FilterShowActivity;
import com.android.gallery3d.filtershow.filters.ImageFilter;
import com.android.gallery3d.filtershow.presets.ImagePreset;

public class ImageSmallFilter extends ImageShow implements View.OnClickListener {

    private static final String LOGTAG = "ImageSmallFilter";
    private FilterShowActivity mController = null;
    protected ImageFilter mImageFilter = null;
    private boolean mShowTitle = true;
    private boolean mSetBorder = false;
    protected final Paint mPaint = new Paint();
    protected boolean mIsSelected = false;

    // TODO: move this to xml.
    protected static int mMargin = 12;
    protected static int mTextMargin = 8;
    protected static int mBackgroundColor = Color.BLUE;
    protected final int mSelectedBackgroundColor = Color.WHITE;
    protected final int mTextColor = Color.WHITE;
    private ImageSmallFilter mNullFilter;

    private Bitmap mOverlayBitmap = null;
    private final int mOverlayTint = Color.argb(100, 0, 0, 0);

    public void setOverlayBitmap(Bitmap bitmap){
        mOverlayBitmap = bitmap;
    }

    public static void setMargin(int value) {
        mMargin = value;
    }

    public static void setTextMargin(int value) {
        mTextMargin = value;
    }

    public static void setDefaultBackgroundColor(int value) {
        mBackgroundColor = value;
    }

    public ImageSmallFilter(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
    }

    public ImageSmallFilter(Context context) {
        super(context);
        setOnClickListener(this);
    }

    public void setImageFilter(ImageFilter filter) {
        mImageFilter = filter;
        mImagePreset = new ImagePreset();
        mImagePreset.setName(filter.getName());
        filter.setImagePreset(mImagePreset);
        mImagePreset.add(mImageFilter);
    }

    public ImageFilter getImageFilter() {
        return mImageFilter;
    }

    @Override
    public void setSelected(boolean value) {
        if (mIsSelected != value) {
            invalidate();
        }
        mIsSelected = value;
    }

    public void setBorder(boolean value) {
        mSetBorder = value;
    }

    public void setController(FilterShowActivity activity) {
        mController = activity;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        int h = mTextSize + mTextPadding;
        setMeasuredDimension(parentHeight - h, parentHeight);
    }

    /**
     * Setting the nullFilter implies that the behavior of the button is toggle
     *
     * @param nullFilter
     */
    public void setNulfilter(ImageSmallFilter nullFilter) {
        mNullFilter = nullFilter;
    }

    @Override
    public void onClick(View v) {
        if (mController != null) {
            if (mImageFilter != null) {
                if (mIsSelected && mNullFilter != null) {
                    mNullFilter.onClick(v);
                }
                else {
                    mController.useImageFilter(this, mImageFilter, mSetBorder);
                }
            } else if (mImagePreset != null) {
                if (mIsSelected && mNullFilter != null) {
                    mNullFilter.onClick(v);
                }
                else {
                    mController.useImagePreset(this, mImagePreset);
                }
            }
        }
    }

    @Override
    public boolean updateGeometryFlags() {
        // We don't want to warn listeners here that the image size has changed, because
        // we'll be working with the small image...
        return false;
    }

    public void setShowTitle(boolean value) {
        mShowTitle = value;
        invalidate();
    }

    @Override
    public boolean showTitle() {
        return mShowTitle;
    }

    @Override
    public boolean showControls() {
        return false;
    }

    @Override
    public boolean showHires() {
        return false;
    }

    @Override
    public ImagePreset getImagePreset() {
        return mImagePreset;
    }

    @Override
    public void updateImagePresets(boolean force) {
        ImagePreset preset = getImagePreset();
        if (preset == null) {
            return;
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        requestFilteredImages();
        canvas.drawColor(mBackgroundColor);
        float textWidth = mPaint.measureText(mImageFilter.getName());
        int h = mTextSize + 2 * mTextPadding;
        int x = (int) ((getWidth() - textWidth) / 2);
        int y = getHeight();
        if (mIsSelected) {
            mPaint.setColor(mSelectedBackgroundColor);
            canvas.drawRect(0, mMargin, getWidth(), getWidth() + mMargin, mPaint);
        }
        Rect destination = new Rect(mMargin, 2*mMargin, getWidth() - mMargin, getWidth());
        drawImage(canvas, getFilteredImage(), destination);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        canvas.drawText(mImageFilter.getName(), x, y - mTextMargin, mPaint);
        if (mOverlayBitmap != null) {
            mPaint.setColor(mOverlayTint);
            canvas.drawRect(0, mMargin, getWidth(), getWidth() + mMargin, mPaint);
            Rect d = new Rect(0, mMargin, getWidth() - mMargin, getWidth());
            mPaint.setColor(Color.BLACK);
            drawImage(canvas, mOverlayBitmap, d);
        }
    }

    public void drawImage(Canvas canvas, Bitmap image, Rect destination) {
        if (image != null) {
            int iw = image.getWidth();
            int ih = image.getHeight();
            int x = 0;
            int y = 0;
            int size = 0;
            Rect source = null;
            if (iw > ih) {
                size = ih;
                x = (int) ((iw - size) / 2.0f);
                y = 0;
            } else {
                size = iw;
                x = 0;
                y = (int) ((ih - size) / 2.0f);
            }
            source = new Rect(x, y, x + size, y + size);
            canvas.drawBitmap(image, source, destination, mPaint);
        }
    }

}