CategoryView.java revision f5eedf1635eba7edfa7d41fd4e991cced978c4b2
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.util.Log;
28import android.view.View;
29import com.android.gallery3d.R;
30import com.android.gallery3d.filtershow.FilterShowActivity;
31import com.android.gallery3d.filtershow.filters.FilterRepresentation;
32
33public class CategoryView extends View implements View.OnClickListener {
34
35    private static final String LOGTAG = "CategoryView";
36    private Paint mPaint = new Paint();
37    private Action mAction;
38    private Rect mTextBounds = new Rect();
39    private static int sMargin = 16;
40    private static int sTextSize = 32;
41    private int mTextColor;
42    private int mBackgroundColor;
43
44    public static void setTextSize(int size) {
45        sTextSize = size;
46    }
47
48    public static void setMargin(int margin) {
49        sMargin = margin;
50    }
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    }
59
60    public void drawText(Canvas canvas, String text) {
61        if (text == null) {
62            return;
63        }
64        text = text.toUpperCase();
65        mPaint.reset();
66        mPaint.setColor(mTextColor);
67        mPaint.setTextSize(sTextSize);
68        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
69        mPaint.setAntiAlias(true);
70        float textWidth = mPaint.measureText(text);
71        mPaint.getTextBounds(text, 0, text.length(), mTextBounds);
72        int x = (int) (canvas.getWidth() - textWidth - sMargin);
73        int y = canvas.getHeight() - sMargin;
74        canvas.drawText(text, x, y, mPaint);
75    }
76
77    public void onDraw(Canvas canvas) {
78        canvas.drawColor(mBackgroundColor);
79        if (mAction != null) {
80            drawText(canvas, mAction.getName());
81            if (mAction.getImage() == null) {
82                mAction.setImageFrame(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
83            } else {
84                Bitmap bitmap = mAction.getImage();
85                canvas.drawBitmap(bitmap, 0, 0, mPaint);
86            }
87        }
88    }
89
90    public void setAction(Action action) {
91        mAction = action;
92        invalidate();
93    }
94
95    public FilterRepresentation getRepresentation() {
96        return mAction.getRepresentation();
97    }
98
99    @Override
100    public void onClick(View view) {
101        FilterShowActivity activity = (FilterShowActivity) getContext();
102        activity.showRepresentation(mAction.getRepresentation());
103    }
104}
105