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
17package com.android.gallery3d.ui;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.text.Layout;
22import android.text.StaticLayout;
23import android.text.TextPaint;
24
25// MultiLineTexture is a texture shows the content of a specified String.
26//
27// To create a MultiLineTexture, use the newInstance() method and specify
28// the String, the font size, and the color.
29class MultiLineTexture extends CanvasTexture {
30    private final Layout mLayout;
31
32    private MultiLineTexture(Layout layout) {
33        super(layout.getWidth(), layout.getHeight());
34        mLayout = layout;
35    }
36
37    public static MultiLineTexture newInstance(
38            String text, int maxWidth, float textSize, int color,
39            Layout.Alignment alignment) {
40        TextPaint paint = StringTexture.getDefaultPaint(textSize, color);
41        Layout layout = new StaticLayout(text, 0, text.length(), paint,
42                maxWidth, alignment, 1, 0, true, null, 0);
43
44        return new MultiLineTexture(layout);
45    }
46
47    @Override
48    protected void onDraw(Canvas canvas, Bitmap backing) {
49        mLayout.draw(canvas);
50    }
51}
52