OSDN Git Service

android-2.1_r1 snapshot
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / media / IconTitleDrawable.java
1 package com.cooliris.media;
2
3 import android.graphics.Canvas;
4 import android.graphics.ColorFilter;
5 import android.graphics.PixelFormat;
6 import android.graphics.Rect;
7 import android.graphics.drawable.Drawable;
8 import android.text.Layout;
9 import android.text.StaticLayout;
10 import android.text.TextPaint;
11 import android.text.TextUtils;
12
13 public final class IconTitleDrawable extends Drawable {
14     private final String mTitle;
15     private final int mTitleWidth;
16     private final Drawable mIcon;
17     private final Config mConfig;
18     private StaticLayout mTitleLayout = null;
19     private int mTitleY;
20
21     public IconTitleDrawable(String title, Drawable icon, Config config) {
22         mTitle = title;
23         mTitleWidth = (int) StaticLayout.getDesiredWidth(mTitle, config.mPaint);
24         mIcon = icon;
25         mConfig = config;
26     }
27
28     @Override
29     public int getIntrinsicWidth() {
30         Config config = mConfig;
31         return config.mTitleLeft + mTitleWidth + 15;
32     }
33
34     @Override
35     public int getIntrinsicHeight() {
36         Config config = mConfig;
37         return Math.max(config.mIconSize, config.mPaint.getFontMetricsInt(null));
38     }
39
40     @Override
41     public void draw(Canvas canvas) {
42         // Paint test = new Paint();
43         // test.setColor(0xff0000ff);
44         // canvas.drawRect(getBounds(), test);
45
46         Drawable icon = mIcon;
47         if (icon != null) {
48             icon.draw(canvas);
49         }
50         Rect bounds = getBounds();
51         int x = bounds.left + mConfig.mTitleLeft;
52         int y = mTitleY;
53         canvas.translate(x, y);
54         mTitleLayout.draw(canvas);
55         canvas.translate(-x, -y);
56     }
57
58     @Override
59     public int getOpacity() {
60         return PixelFormat.TRANSLUCENT;
61     }
62
63     @Override
64     public void setAlpha(int alpha) {
65     }
66
67     @Override
68     public void setColorFilter(ColorFilter cf) {
69     }
70
71     @Override
72     protected void onBoundsChange(Rect bounds) {
73         // Position the icon.
74         int left = bounds.left;
75         int top = bounds.top;
76         int right = bounds.right;
77         int height = bounds.bottom - top;
78         Config config = mConfig;
79         int iconLeft = left + config.mIconLeft;
80         int iconSize = config.mIconSize;
81         Drawable icon = mIcon;
82         if (icon != null) {
83             int iconY = top + (height - iconSize) / 2;
84             icon.setBounds(iconLeft, iconY, iconLeft + iconSize, top + iconSize);
85         }
86
87         // Layout the text.
88         int outerWidth = right - config.mTitleLeft;
89         String title = mTitle;
90         mTitleLayout = new StaticLayout(title, 0, title.length(), config.mPaint, outerWidth, Layout.Alignment.ALIGN_NORMAL, 1, 0,
91                 true, TextUtils.TruncateAt.MIDDLE, outerWidth);
92         mTitleY = top + (height - mTitleLayout.getHeight()) / 2;
93     }
94
95     public static final class Config {
96         private final int mIconLeft;
97         private final int mTitleLeft;
98         private final int mIconSize;
99         private final TextPaint mPaint;
100
101         public Config(int iconSpan, int iconSize, TextPaint paint) {
102             mIconLeft = (iconSpan - iconSize) / 2;
103             mTitleLeft = iconSpan;
104             mIconSize = iconSize;
105             mPaint = paint;
106         }
107     }
108 }