Html类ImageGetter接口
来源:互联网 发布:linux shell编程工具 编辑:程序博客网 时间:2024/05/20 14:25
在之前Html类支持的HTML标签文章中了解到当解析到<img>标签时就会回调getDrawable()方法,并需要返回一个Drawable对象;当前我们需要定义类并实现ImageGetter接口以及在getDrawable方法中做相应的处理,下面我们则来看看具体该如何处理;
具体代码:
-
-
-
-
- public class ImgLabelActivity extends Activity {
-
- private static final String TAG = "ImgLabelActivity";
-
- private TextView mTvOne;
-
- private TextView mTvTwo;
-
- private TextView mTvThree;
-
- private String picName = "networkPic.jpg";
-
- private NetworkImageGetter mImageGetter;
-
- private String htmlThree = "网络图片测试:" + "<img src='http://img.my.csdn.net/uploads/201307/14/1373780364_7576.jpg'>";
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_img_label);
-
- mTvOne = (TextView) this.findViewById(R.id.tv_img_label_one);
- String htmlOne = "本地图片测试:" + "<img src='/mnt/sdcard/imgLabel.jpg'>";
- mTvOne.setText(Html.fromHtml(htmlOne, new LocalImageGetter(), null));
-
- mTvTwo = (TextView) this.findViewById(R.id.tv_img_label_two);
- String htmlTwo = "项目图片测试:" + "<img src=\""+R.drawable.imagepro+"\">";
- mTvTwo.setText(Html.fromHtml(htmlTwo, new ProImageGetter(), null));
-
-
- mTvThree = (TextView) this.findViewById(R.id.tv_img_label_three);
- mImageGetter = new NetworkImageGetter();
- mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));
- }
-
-
-
-
- private final class LocalImageGetter implements Html.ImageGetter{
-
- @Override
- public Drawable getDrawable(String source) {
-
- Drawable drawable = Drawable.createFromPath(source);
-
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
-
- return drawable;
- }
- }
-
-
-
-
- private final class ProImageGetter implements Html.ImageGetter{
-
- @Override
- public Drawable getDrawable(String source) {
-
- int id = Integer.parseInt(source);
- Drawable drawable = getResources().getDrawable(id);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- return drawable;
- }
- }
-
-
-
-
- private final class NetworkImageGetter implements Html.ImageGetter{
-
- @Override
- public Drawable getDrawable(String source) {
-
- Drawable drawable = null;
-
- File file = new File(Environment.getExternalStorageDirectory(), picName);
-
- if(source.startsWith("http")) {
-
- if(file.exists()) {
-
- drawable = Drawable.createFromPath(file.getAbsolutePath());
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- } else {
-
- AsyncLoadNetworkPic networkPic = new AsyncLoadNetworkPic();
- networkPic.execute(source);
- }
- }
- return drawable;
- }
- }
-
-
-
-
- private final class AsyncLoadNetworkPic extends AsyncTask<String, Integer, Void>{
-
- @Override
- protected Void doInBackground(String... params) {
-
- loadNetPic(params);
- return null;
- }
-
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
-
- mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));
- }
-
- private void loadNetPic(String... params) {
- String path = params[0];
-
- File file = new File(Environment.getExternalStorageDirectory(), picName);
-
- InputStream in = null;
-
- FileOutputStream out = null;
-
- try {
- URL url = new URL(path);
-
- HttpURLConnection connUrl = (HttpURLConnection) url.openConnection();
-
- connUrl.setConnectTimeout(5000);
-
- connUrl.setRequestMethod("GET");
-
- if(connUrl.getResponseCode() == 200) {
-
- in = connUrl.getInputStream();
-
- out = new FileOutputStream(file);
-
- byte[] buffer = new byte[1024];
-
- int len;
-
- while((len = in.read(buffer))!= -1){
- out.write(buffer, 0, len);
- }
- } else {
- Log.i(TAG, connUrl.getResponseCode() + "");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
-
- if(in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(out != null) {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
需要注意的是:
在获取到drawable时需要为其设置边界,如没有设置的话TextView就不能显示该drawable了;
在加载网络图片时需要开启子线程去访问网络并将图片存储到本地,之后再次为其设置一次;
原文出处
0 0