TextView、EditText图文结合

TextView图文结合使用

String html = 
"下面是图片了"+"src='http://www.qqpk.cn/Article/UploadFiles/201411/20141116135722282.jpg'/>"+"
     还有一张"+"src='http://img.61gequ.com/allimg/2011-4/201142614314278502.jpg' />";
//获取图片
Spanned sp = Html.fromHtml(html, new Html.ImageGetter() {  
    @Override  
    public Drawable getDrawable(String source) {  
        InputStream is = null;  
        try {  
            is = (InputStream) new URL(source).getContent();  
            Drawable d = Drawable.createFromStream(is, "src");  
            d.setBounds(0, 0, d.getIntrinsicWidth(),  
                    d.getIntrinsicHeight());  
            is.close();  
            return d;  
        } catch (Exception e) {  
            return null;  
        }  
    }  
}, null);  
textView.setText(sp);  

EditText图文结合使用

Bitmap loadedImage = BitmapFactory.decodeFile(path);
loadedImage = Bitmap.createBitmap(loadedImage, 0, 0, loadedImage.getWidth(), loadedImage.getHeight(), matrix,        true);
ImageSpan imageSpan = new ImageSpan(context, loadedImage);
String tempUrl = "";
SpannableString spannableString = new SpannableString(tempUrl);
spannableString.setSpan(imageSpan, 0, tempUrl.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 将选择的图片追加到EditText中光标所在位置
int index = et.getSelectionStart(); 
// 获取光标所在位置
Editable edit_text = et.getEditableText();
if (index < 0 || index >= edit_text.length()) {
    edit_text.append(spannableString);
} else {
    edit_text.insert(index, spannableString);
}

参考博客:晓峰残月

你可能感兴趣的:(TextView、EditText图文结合)