2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.ui;
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.graphics.Paint.FontMetricsInt;
24 class StringTexture extends CanvasTexture {
25 private static int DEFAULT_PADDING = 1;
27 private final String mText;
28 private final Paint mPaint;
29 private final FontMetricsInt mMetrics;
31 public StringTexture(String text, Paint paint,
32 FontMetricsInt metrics, int width, int height) {
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);
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);
54 return newInstance(text, paint);
58 protected void onDraw(Canvas canvas, Bitmap backing) {
59 canvas.translate(DEFAULT_PADDING, DEFAULT_PADDING - mMetrics.ascent);
60 canvas.drawText(mText, 0, 0, mPaint);