package com.lcjian.library.emotion;
|
|
import java.io.InputStream;
|
|
import android.content.Context;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.Drawable;
|
import android.net.Uri;
|
import android.text.style.DynamicDrawableSpan;
|
import android.util.Log;
|
|
public class EmotionSpan extends DynamicDrawableSpan {
|
|
private Drawable mDrawable;
|
private Uri mContentUri;
|
private int mResourceId;
|
private Context mContext;
|
private int mWidth;
|
private int mHeight;
|
|
public EmotionSpan(Context context, Bitmap b, int width, int height) {
|
super();
|
mContext = context;
|
mDrawable = new BitmapDrawable(context.getResources(), b);
|
if (width == 0 || height == 0) {
|
width = mDrawable.getIntrinsicWidth();
|
height = mDrawable.getIntrinsicHeight();
|
}
|
mDrawable.setBounds(0, 0, width > 0 ? width : 0, height > 0 ? height : 0);
|
mWidth = width;
|
mHeight = height;
|
}
|
|
public EmotionSpan(Context context, Uri uri, int width, int height) {
|
mContext = context;
|
mContentUri = uri;
|
mWidth = width;
|
mHeight = height;
|
}
|
|
public EmotionSpan(Context context, int resourceId, int width, int height) {
|
mContext = context;
|
mResourceId = resourceId;
|
mWidth = width;
|
mHeight = height;
|
}
|
|
|
@Override
|
public Drawable getDrawable() {
|
Drawable drawable = null;
|
|
if (mDrawable != null) {
|
drawable = mDrawable;
|
} else if (mContentUri != null) {
|
Bitmap bitmap = null;
|
try {
|
InputStream is = mContext.getContentResolver().openInputStream(mContentUri);
|
bitmap = BitmapFactory.decodeStream(is);
|
drawable = new BitmapDrawable(mContext.getResources(), bitmap);
|
drawable.setBounds(0, 0, mWidth, mHeight);
|
is.close();
|
} catch (Exception e) {
|
Log.e("sms", "Failed to loaded content " + mContentUri, e);
|
}
|
} else {
|
try {
|
drawable = mContext.getResources().getDrawable(mResourceId);
|
drawable.setBounds(0, 0, mWidth, mHeight);
|
} catch (Exception e) {
|
Log.e("sms", "Unable to find resource: " + mResourceId);
|
}
|
}
|
return drawable;
|
}
|
}
|