Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2009 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.cooliris.media;
     18 
     19 import android.graphics.Canvas;
     20 import android.graphics.ColorFilter;
     21 import android.graphics.PixelFormat;
     22 import android.graphics.Rect;
     23 import android.graphics.drawable.Drawable;
     24 import android.text.Layout;
     25 import android.text.StaticLayout;
     26 import android.text.TextPaint;
     27 import android.text.TextUtils;
     28 
     29 public final class IconTitleDrawable extends Drawable {
     30     private final String mTitle;
     31     private final int mTitleWidth;
     32     private final Drawable mIcon;
     33     private final Config mConfig;
     34     private StaticLayout mTitleLayout = null;
     35     private int mTitleY;
     36 
     37     public IconTitleDrawable(String title, Drawable icon, Config config) {
     38         mTitle = title;
     39         mTitleWidth = (int) StaticLayout.getDesiredWidth(mTitle, config.mPaint);
     40         mIcon = icon;
     41         mConfig = config;
     42     }
     43 
     44     @Override
     45     public int getIntrinsicWidth() {
     46         Config config = mConfig;
     47         return config.mTitleLeft + mTitleWidth + 15;
     48     }
     49 
     50     @Override
     51     public int getIntrinsicHeight() {
     52         Config config = mConfig;
     53         return Math.max(config.mIconSize, config.mPaint.getFontMetricsInt(null));
     54     }
     55 
     56     @Override
     57     public void draw(Canvas canvas) {
     58         // Paint test = new Paint();
     59         // test.setColor(0xff0000ff);
     60         // canvas.drawRect(getBounds(), test);
     61 
     62         Drawable icon = mIcon;
     63         if (icon != null) {
     64             icon.draw(canvas);
     65         }
     66         Rect bounds = getBounds();
     67         int x = bounds.left + mConfig.mTitleLeft;
     68         int y = mTitleY;
     69         canvas.translate(x, y);
     70         mTitleLayout.draw(canvas);
     71         canvas.translate(-x, -y);
     72     }
     73 
     74     @Override
     75     public int getOpacity() {
     76         return PixelFormat.TRANSLUCENT;
     77     }
     78 
     79     @Override
     80     public void setAlpha(int alpha) {
     81     }
     82 
     83     @Override
     84     public void setColorFilter(ColorFilter cf) {
     85     }
     86 
     87     @Override
     88     protected void onBoundsChange(Rect bounds) {
     89         // Position the icon.
     90         int left = bounds.left;
     91         int top = bounds.top;
     92         int right = bounds.right;
     93         int height = bounds.bottom - top;
     94         Config config = mConfig;
     95         int iconLeft = left + config.mIconLeft;
     96         int iconSize = config.mIconSize;
     97         Drawable icon = mIcon;
     98         if (icon != null) {
     99             int iconY = top + (height - iconSize) / 2;
    100             icon.setBounds(iconLeft, iconY, iconLeft + iconSize, top + iconSize);
    101         }
    102 
    103         // Layout the text.
    104         int outerWidth = right - config.mTitleLeft;
    105         String title = mTitle;
    106         mTitleLayout = new StaticLayout(title, 0, title.length(), config.mPaint, outerWidth, Layout.Alignment.ALIGN_NORMAL, 1, 0,
    107                 true, TextUtils.TruncateAt.MIDDLE, outerWidth);
    108         mTitleY = top + (height - mTitleLayout.getHeight()) / 2;
    109     }
    110 
    111     public static final class Config {
    112         private final int mIconLeft;
    113         private final int mTitleLeft;
    114         private final int mIconSize;
    115         private final TextPaint mPaint;
    116 
    117         public Config(int iconSpan, int iconSize, TextPaint paint) {
    118             mIconLeft = (iconSpan - iconSize) / 2;
    119             mTitleLeft = iconSpan;
    120             mIconSize = iconSize;
    121             mPaint = paint;
    122         }
    123     }
    124 }
    125