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.gallery3d.glrenderer; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.graphics.Paint.FontMetricsInt; 23 import android.graphics.Typeface; 24 import android.text.TextPaint; 25 import android.text.TextUtils; 26 import android.util.FloatMath; 27 28 // StringTexture is a texture shows the content of a specified String. 29 // 30 // To create a StringTexture, use the newInstance() method and specify 31 // the String, the font size, and the color. 32 public class StringTexture extends CanvasTexture { 33 private final String mText; 34 private final TextPaint mPaint; 35 private final FontMetricsInt mMetrics; 36 37 private StringTexture(String text, TextPaint paint, 38 FontMetricsInt metrics, int width, int height) { 39 super(width, height); 40 mText = text; 41 mPaint = paint; 42 mMetrics = metrics; 43 } 44 45 public static TextPaint getDefaultPaint(float textSize, int color) { 46 TextPaint paint = new TextPaint(); 47 paint.setTextSize(textSize); 48 paint.setAntiAlias(true); 49 paint.setColor(color); 50 paint.setShadowLayer(2f, 0f, 0f, Color.BLACK); 51 return paint; 52 } 53 54 public static StringTexture newInstance( 55 String text, float textSize, int color) { 56 return newInstance(text, getDefaultPaint(textSize, color)); 57 } 58 59 public static StringTexture newInstance( 60 String text, float textSize, int color, 61 float lengthLimit, boolean isBold) { 62 TextPaint paint = getDefaultPaint(textSize, color); 63 if (isBold) { 64 paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); 65 } 66 if (lengthLimit > 0) { 67 text = TextUtils.ellipsize( 68 text, paint, lengthLimit, TextUtils.TruncateAt.END).toString(); 69 } 70 return newInstance(text, paint); 71 } 72 73 private static StringTexture newInstance(String text, TextPaint paint) { 74 FontMetricsInt metrics = paint.getFontMetricsInt(); 75 int width = (int) FloatMath.ceil(paint.measureText(text)); 76 int height = metrics.bottom - metrics.top; 77 // The texture size needs to be at least 1x1. 78 if (width <= 0) width = 1; 79 if (height <= 0) height = 1; 80 return new StringTexture(text, paint, metrics, width, height); 81 } 82 83 @Override 84 protected void onDraw(Canvas canvas, Bitmap backing) { 85 canvas.translate(0, -mMetrics.ascent); 86 canvas.drawText(mText, 0, 0, mPaint); 87 } 88 } 89