android imagegetter,java – Html.ImageGetter TextView

最简单的解决方案是:

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;

import org.pskink.soom.R;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.graphics.drawable.LevelListDrawable;

import android.os.AsyncTask;

import android.os.Bundle;

import android.text.Html;

import android.text.Html.ImageGetter;

import android.text.Spanned;

import android.util.Log;

import android.widget.TextView;

public class TestImageGetter extends Activity implements ImageGetter {

private final static String TAG = "TestImageGetter";

private TextView mTv;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test_image_getter);

String source = "this is a test of ImageGetter it contains " +

"two images:
" +

"dac_logo.png%5C%22
and
" +

"icon_search.png%5C%22";

Spanned spanned = Html.fromHtml(source, this, null);

mTv = (TextView) findViewById(R.id.text);

mTv.setText(spanned);

}

@Override

public Drawable getDrawable(String source) {

LevelListDrawable d = new LevelListDrawable();

Drawable empty = getResources().getDrawable(R.drawable.ic_launcher);

d.addLevel(0, 0, empty);

d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

new LoadImage().execute(source, d);

return d;

}

class LoadImage extends AsyncTask {

private LevelListDrawable mDrawable;

@Override

protected Bitmap doInBackground(Object... params) {

String source = (String) params[0];

mDrawable = (LevelListDrawable) params[1];

Log.d(TAG, "doInBackground " + source);

try {

InputStream is = new URL(source).openStream();

return BitmapFactory.decodeStream(is);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(Bitmap bitmap) {

Log.d(TAG, "onPostExecute drawable " + mDrawable);

Log.d(TAG, "onPostExecute bitmap " + bitmap);

if (bitmap != null) {

BitmapDrawable d = new BitmapDrawable(bitmap);

mDrawable.addLevel(1, 1, d);

mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());

mDrawable.setLevel(1);

// i don't know yet a better way to refresh TextView

// mTv.invalidate() doesn't work as expected

CharSequence t = mTv.getText();

mTv.setText(t);

}

}

}

}

图像下载后有一种不太优雅的方式重新布局TextView:

// i don't know yet a better way to refresh TextView

// mTv.invalidate() doesn't work as expected

CharSequence t = mTv.getText();

mTv.setText(t);

如果有人知道更好的解决方案,请告诉我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值