admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
    }
}