网路图片加载方法:
1 ImageGetter中判断图片是否已经加载过,如果本地已经存在直接从本地加载
2 图片本地不存在,开启线程加载图片,下载完毕后,重新调用ImageGetter加载图片
主要代码:
1 TextView加载html
htmlText4 = (TextView) findViewById( R.id.htmlText4); Spanned span = Html.fromHtml( htmlStr,imgGetter2,null ); htmlText4.setText(span);
2 ImageGetter 使用
//加载网络图片 ImageGetter imgGetter2 = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) {//参数为image的src属性 Drawable drawable = null; String photoPath = FileUtils.getPhotoImgPath(); String fileName = FileUtils.getFileName(source ); File imageFile = new File(photoPath,fileName); if(imageFile.exists()){//本地存在直接加载本地图片 //获取本地文件返回Drawable drawable=Drawable.createFromPath( imageFile.getAbsolutePath()); //设置图片边界 // drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.setBounds(0, 0, 500, 500); }else{//图片没有家再过,则重新加载图片 loadImage(source); } return drawable; } };
3 开启线程加载图片
//加载图片 private void loadImage(final String imageUrl){ new Thread(){ public void run() { Bitmap bitmap = ApiClient.getNetBitmap( imageUrl); String photoPath = FileUtils.getPhotoImgPath(); String fileName = FileUtils.getFileName(imageUrl ); try { ImageUtils.saveImageToSD(HtmlActivity.this, photoPath+"/"+fileName, bitmap, 100); } catch (IOException e) { e.printStackTrace(); } handler.sendEmptyMessage(1); }; }.start(); }
4 图片加载后,重新调用ImageGetter
//加载图片后显示html文字 private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Spanned span = Html.fromHtml( htmlStr,imgGetter2,null ); htmlText4.setText(span); }; };
5 图片保存到本地的路径
/** * 得到图片临时存储路径 * @return */ public static String getPhotoImgPath(){ String savePath = ""; // 判断是否挂载了SD卡 String storageState = Environment .getExternalStorageState(); if (storageState.equals(Environment.MEDIA_MOUNTED)) { savePath = Environment .getExternalStorageDirectory() + "/Test/Camera/";// 存放照片的文件夹 File savedir = new File(savePath); if (!savedir.exists()) { savedir.mkdirs(); } } return savePath; }
相关推荐
TextView显示html的文本、图片
android Textview 实现html 图片点击 两种方式 一种是用webview ,调用jsInterface实现 另一种是 textview 使用Html.from()方法
android下textview 显示html片段,可以显示文字和图片混排,图片可以是网络图片
Android TextView显示html的demo ,演示如何在TextView上显示html效果,包括图片,
Android中TextView显示图片简单的Demo,利用TextView解析和显示HTML代码的功能实现。
Android TextView可以支持简单的... 1、图片不能居中显示 2、图片的大小被缩小 3、图片是在ImageGetter中获取,ImageGetter不能和ImageSpan同时使用; 笔者通过参考大量资料和源码,解决了上述3个问题。
textview加载带图片的html片段并实现按图片原始比例缩放图片
TextView加载网络图片HTMLIMG,实用型的东西,拿出来大家学习学习
实现TextView 显示Gif 动态图片
html文件常常是含有图片,如果需要在Textview中实现文字和图片的混排,需要使用ImageGetter。ImageGetter是Html类中一个接口,作用是给img标签获取图片内容,主要提供了一个getDrawable的方法。但很多情况下,需要...
自定义TextView显示Gif,先使用GifDecoder将Gif文件解析成一张张图片,然后通过ImageSpan将图片显示在TextView中,定义一个线程不断刷新图片
TextView加载Html图片
TextView使用HTML处理字体样式、显示图片等
一、[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android.text.Html; TextView t3 = (TextView) findViewById(R.id.text3); t3.setText( ...
TextView控件显示HTML图片资源实例
Android的TextView中显示图片和图像的方法
用TextView加载HTML文件并显示图片
可直接运行,方便直接添加到项目中 解决了android 加载Html页面只出现文字,图片加载不出来,直接显示小方块的问题
tv.setText(Html.fromHtml(html1, new URLImageParser(tv), null));一行代码解决TextView 加载HTML图片问题
查看文章:[Android教程]TextView显示Html类解析的网页和图片及自定义标签 3、在TextView四周显示图片 mTVText.setText("setCompoundDrawables"); Drawable d = getResources().getDrawable(R.drawable.ic_launcher)...