CategoryView.java revision 2332f652d86e98887996505002d34b4f7940c34a
1/*
2 * Copyright (C) 2013 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.category;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.Canvas;
23import android.graphics.Color;
24import android.graphics.Paint;
25import android.graphics.Rect;
26import android.graphics.Typeface;
27import android.view.View;
28import com.android.gallery3d.R;
29import com.android.gallery3d.filtershow.FilterShowActivity;
30import com.android.gallery3d.filtershow.filters.FilterRepresentation;
31import com.android.gallery3d.filtershow.ui.SelectionRenderer;
32
33public class CategoryView extends View implements View.OnClickListener {
34
35    private static final String LOGTAG = "CategoryView";
36    public static final int VERTICAL = 0;
37    public static final int HORIZONTAL = 1;
38    private Paint mPaint = new Paint();
39    private Action mAction;
40    private Rect mTextBounds = new Rect();
41    private int mMargin = 16;
42    private int mTextSize = 32;
43    private int mTextColor;
44    private int mBackgroundColor;
45    private Paint mSelectPaint;
46    CategoryAdapter mAdapter;
47    private int mSelectionStroke;
48    private Paint mBorderPaint;
49    private int mBorderStroke;
50    private int mOrientation = VERTICAL;
51
52    public CategoryView(Context context) {
53        super(context);
54        setOnClickListener(this);
55        Resources res = getResources();
56        mBackgroundColor = res.getColor(R.color.filtershow_categoryview_background);
57        mTextColor = res.getColor(R.color.filtershow_categoryview_text);
58        mSelectionStroke = res.getDimensionPixelSize(R.dimen.thumbnail_margin);
59        mTextSize = res.getDimensionPixelSize(R.dimen.category_panel_text_size);
60        mMargin = res.getDimensionPixelOffset(R.dimen.category_panel_margin);
61        mSelectPaint = new Paint();
62        mSelectPaint.setStyle(Paint.Style.FILL);
63        mSelectPaint.setColor(res.getColor(R.color.filtershow_category_selection));
64        mBorderPaint = new Paint(mSelectPaint);
65        mBorderPaint.setColor(Color.BLACK);
66        mBorderStroke = mSelectionStroke / 3;
67    }
68
69    private void computeTextPosition(String text) {
70        if (text == null) {
71            return;
72        }
73        mPaint.setTextSize(mTextSize);
74        if (mOrientation == VERTICAL) {
75            text = text.toUpperCase();
76            // TODO: set this in xml
77            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
78        }
79        mPaint.getTextBounds(text, 0, text.length(), mTextBounds);
80    }
81
82    public void drawText(Canvas canvas, String text) {
83        if (text == null) {
84            return;
85        }
86        float textWidth = mPaint.measureText(text);
87        int x = (int) (canvas.getWidth() - textWidth - mMargin);
88        if (mOrientation == HORIZONTAL) {
89            x = (int) ((canvas.getWidth() - textWidth) / 2.0f);
90        }
91        if (x < 0) {
92            // If the text takes more than the view width,
93            // justify to the left.
94            x = mMargin;
95        }
96        int y = canvas.getHeight() - mMargin;
97        canvas.drawText(text, x, y, mPaint);
98    }
99
100    @Override
101    public CharSequence getContentDescription () {
102        if (mAction != null) {
103            return mAction.getName();
104        }
105        return null;
106    }
107
108    @Override
109    public void onDraw(Canvas canvas) {
110        canvas.drawColor(mBackgroundColor);
111        if (mAction != null) {
112            mPaint.reset();
113            mPaint.setAntiAlias(true);
114            computeTextPosition(mAction.getName());
115            if (mAction.getImage() == null) {
116                mAction.setImageFrame(new Rect(0, 0, getWidth(), getHeight()), mOrientation);
117            } else {
118                Bitmap bitmap = mAction.getImage();
119                canvas.save();
120                Rect clipRect = new Rect(mSelectionStroke, mSelectionStroke,
121                        getWidth() - mSelectionStroke,
122                        getHeight() - 2* mMargin - mTextSize);
123                int offsetx = 0;
124                int offsety = 0;
125                if (mOrientation == HORIZONTAL) {
126                    canvas.clipRect(clipRect);
127                    offsetx = - (bitmap.getWidth() - clipRect.width()) / 2;
128                    offsety = - (bitmap.getHeight() - clipRect.height()) / 2;
129                }
130                canvas.drawBitmap(bitmap, offsetx, offsety, mPaint);
131                canvas.restore();
132                if (mAdapter.isSelected(this)) {
133                    if (mOrientation == HORIZONTAL) {
134                        SelectionRenderer.drawSelection(canvas, 0, 0,
135                                getWidth(), getHeight() - mMargin - mTextSize,
136                                mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint);
137                    } else {
138                        SelectionRenderer.drawSelection(canvas, 0, 0,
139                                Math.min(bitmap.getWidth(), getWidth()),
140                                Math.min(bitmap.getHeight(), getHeight()),
141                                mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint);
142                    }
143                }
144            }
145            mPaint.setColor(mBackgroundColor);
146            mPaint.setStyle(Paint.Style.STROKE);
147            mPaint.setStrokeWidth(3);
148            drawText(canvas, mAction.getName());
149            mPaint.setColor(mTextColor);
150            mPaint.setStyle(Paint.Style.FILL);
151            mPaint.setStrokeWidth(1);
152            drawText(canvas, mAction.getName());
153        }
154    }
155
156    public void setAction(Action action, CategoryAdapter adapter) {
157        mAction = action;
158        mAdapter = adapter;
159        invalidate();
160    }
161
162    public FilterRepresentation getRepresentation() {
163        return mAction.getRepresentation();
164    }
165
166    @Override
167    public void onClick(View view) {
168        FilterShowActivity activity = (FilterShowActivity) getContext();
169        activity.showRepresentation(mAction.getRepresentation());
170        mAdapter.setSelected(this);
171    }
172
173    public void setOrientation(int orientation) {
174        mOrientation = orientation;
175    }
176}
177