ImageSpan.java revision 3001a035439d8134a7d70d796376d1dfbff3cdcd
1/* 2 * Copyright (C) 2006 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 android.text.style; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.graphics.BitmapFactory; 22import android.graphics.drawable.BitmapDrawable; 23import android.graphics.drawable.Drawable; 24import android.net.Uri; 25import android.util.Log; 26 27import java.io.InputStream; 28 29public class ImageSpan extends DynamicDrawableSpan { 30 private Drawable mDrawable; 31 private Uri mContentUri; 32 private int mResourceId; 33 private Context mContext; 34 private String mSource; 35 36 public ImageSpan(Bitmap b) { 37 this(b, ALIGN_BOTTOM); 38 } 39 40 /** 41 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or 42 * {@link DynamicDrawableSpan#ALIGN_BASELINE}. 43 */ 44 public ImageSpan(Bitmap b, int verticalAlignment) { 45 super(verticalAlignment); 46 mDrawable = new BitmapDrawable(b); 47 int width = mDrawable.getIntrinsicWidth(); 48 int height = mDrawable.getIntrinsicHeight(); 49 mDrawable.setBounds(0, 0, width > 0 ? width : 0, height > 0 ? height : 0); 50 } 51 52 public ImageSpan(Drawable d) { 53 this(d, ALIGN_BOTTOM); 54 } 55 56 /** 57 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or 58 * {@link DynamicDrawableSpan#ALIGN_BASELINE}. 59 */ 60 public ImageSpan(Drawable d, int verticalAlignment) { 61 super(verticalAlignment); 62 mDrawable = d; 63 } 64 65 public ImageSpan(Drawable d, String source) { 66 this(d, source, ALIGN_BOTTOM); 67 } 68 69 /** 70 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or 71 * {@link DynamicDrawableSpan#ALIGN_BASELINE}. 72 */ 73 public ImageSpan(Drawable d, String source, int verticalAlignment) { 74 super(verticalAlignment); 75 mDrawable = d; 76 mSource = source; 77 } 78 79 public ImageSpan(Context context, Uri uri) { 80 this(context, uri, ALIGN_BOTTOM); 81 } 82 83 /** 84 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or 85 * {@link DynamicDrawableSpan#ALIGN_BASELINE}. 86 */ 87 public ImageSpan(Context context, Uri uri, int verticalAlignment) { 88 super(verticalAlignment); 89 mContext = context; 90 mContentUri = uri; 91 } 92 93 public ImageSpan(Context context, int resourceId) { 94 this(context, resourceId, ALIGN_BOTTOM); 95 } 96 97 /** 98 * @param verticalAlignment one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or 99 * {@link DynamicDrawableSpan#ALIGN_BASELINE}. 100 */ 101 public ImageSpan(Context context, int resourceId, int verticalAlignment) { 102 super(verticalAlignment); 103 mContext = context; 104 mResourceId = resourceId; 105 } 106 107 @Override 108 public Drawable getDrawable() { 109 Drawable drawable = null; 110 111 if (mDrawable != null) { 112 drawable = mDrawable; 113 } else if (mContentUri != null) { 114 Bitmap bitmap = null; 115 try { 116 InputStream is = mContext.getContentResolver().openInputStream( 117 mContentUri); 118 bitmap = BitmapFactory.decodeStream(is); 119 drawable = new BitmapDrawable(bitmap); 120 is.close(); 121 } catch (Exception e) { 122 Log.e("sms", "Failed to loaded content " + mContentUri, e); 123 } 124 } else { 125 try { 126 drawable = mContext.getResources().getDrawable(mResourceId); 127 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 128 drawable.getIntrinsicHeight()); 129 } catch (Exception e) { 130 Log.e("sms", "Unable to find resource: " + mResourceId); 131 } 132 } 133 134 return drawable; 135 } 136 137 /** 138 * Returns the source string that was saved during construction. 139 */ 140 public String getSource() { 141 return mSource; 142 } 143 144} 145