1 /* 2 * Copyright (C) 2010 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 17 package com.android.camera.ui; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Canvas; 21 import android.graphics.Paint; 22 import android.graphics.Paint.FontMetricsInt; 23 24 public class StringTexture extends CanvasTexture { 25 private static int DEFAULT_PADDING = 1; 26 27 private final String mText; 28 private final Paint mPaint; 29 private final FontMetricsInt mMetrics; 30 31 public StringTexture(String text, Paint paint, 32 FontMetricsInt metrics, int width, int height) { 33 super(width, height); 34 mText = text; 35 mPaint = paint; 36 mMetrics = metrics; 37 } 38 39 40 public static StringTexture newInstance(String text, Paint paint) { 41 FontMetricsInt metrics = paint.getFontMetricsInt(); 42 int width = (int) (.5f + paint.measureText(text)) + DEFAULT_PADDING * 2; 43 int height = metrics.bottom - metrics.top + DEFAULT_PADDING * 2; 44 return new StringTexture(text, paint, metrics, width, height); 45 } 46 47 public static StringTexture newInstance( 48 String text, float textSize, int color) { 49 Paint paint = new Paint(); 50 paint.setTextSize(textSize); 51 paint.setAntiAlias(true); 52 paint.setColor(color); 53 54 return newInstance(text, paint); 55 } 56 57 @Override 58 protected void onDraw(Canvas canvas, Bitmap backing) { 59 canvas.translate(DEFAULT_PADDING, DEFAULT_PADDING - mMetrics.ascent); 60 canvas.drawText(mText, 0, 0, mPaint); 61 } 62 } 63