summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/imageshow/ImageSmallFilter.java
blob: 698c4ea7aa4a5cdfb34408f671545755b4087723 (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

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;
    private 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 final int mBackgroundColor = Color.argb(255, 30, 32, 40);
    protected final int mSelectedBackgroundColor = Color.WHITE;
    protected final int mTextColor = Color.WHITE;

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

    public static void setTextMargin(int value) {
        mTextMargin = 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);
    }

    @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);
    }

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

    @Override
    public void updateImage() {
        // We don't want to warn listeners here that the image size has changed, because
        // we'll be working with the small image...
        mForegroundImage = getOriginalFrontBitmap();
    }

    @Override
    protected Bitmap getOriginalFrontBitmap() {
        if (mImageLoader == null) {
            return null;
        }
        return mImageLoader.getOriginalBitmapSmall();
    }

    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 void onDraw(Canvas canvas) {
        getFilteredImage();
        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, mFilteredImage, destination);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        canvas.drawText(mImageFilter.getName(), x, y - mTextMargin, mPaint);
    }

    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);
        }
    }

}