CategoryView.java revision 00259461be82e601b58d3e970afbf0c012c5f3e7
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.setTextSize(sTextSize); 66 mPaint.setTypeface(Typeface.DEFAULT_BOLD); 67 float textWidth = mPaint.measureText(text); 68 mPaint.getTextBounds(text, 0, text.length(), mTextBounds); 69 int x = (int) (canvas.getWidth() - textWidth - sMargin); 70 int y = canvas.getHeight() - sMargin; 71 canvas.drawText(text, x, y, mPaint); 72 } 73 74 public void onDraw(Canvas canvas) { 75 canvas.drawColor(mBackgroundColor); 76 if (mAction != null) { 77 mPaint.reset(); 78 mPaint.setAntiAlias(true); 79 if (mAction.getImage() == null) { 80 mAction.setImageFrame(new Rect(0, 0, canvas.getWidth(), canvas.getHeight())); 81 } else { 82 Bitmap bitmap = mAction.getImage(); 83 canvas.drawBitmap(bitmap, 0, 0, mPaint); 84 } 85 mPaint.setColor(mBackgroundColor); 86 mPaint.setStyle(Paint.Style.STROKE); 87 mPaint.setStrokeWidth(3); 88 drawText(canvas, mAction.getName()); 89 mPaint.setColor(mTextColor); 90 mPaint.setStyle(Paint.Style.FILL); 91 mPaint.setStrokeWidth(1); 92 drawText(canvas, mAction.getName()); 93 } 94 } 95 96 public void setAction(Action action) { 97 mAction = action; 98 invalidate(); 99 } 100 101 public FilterRepresentation getRepresentation() { 102 return mAction.getRepresentation(); 103 } 104 105 @Override 106 public void onClick(View view) { 107 FilterShowActivity activity = (FilterShowActivity) getContext(); 108 activity.showRepresentation(mAction.getRepresentation()); 109 } 110} 111