• Android EditText载入HTML内容(内容包括网络图片)


    android中的Html.fromHtml能够用来载入HTML的内容。fromHtml有三个參数须要设置,第一个是要显示的html内容,第二个就是要说的重点,ImageGetter,用来处理图片载入的,第三个 TagHandler能够设置为null。接下来我们来解说下ImageGetter。网上非常多的方法都是直接引用本地的图片。是同步的,比方:

    private ImageGetter imageGetter = new ImageGetter() {
    		@Override
    		public Drawable getDrawable(String source) {
    			String f = pic_path.substring(0, 1);
    			String url = pic_path.substring(2);
    			if (f.equals("1")) {
    				try {
    					ContentResolver cr = getActivity().getContentResolver();
    					Uri uri = Uri.parse(url);
    					Bitmap bitmap = getimage(cr, uri);
    					return getMyDrawable(bitmap);
    				} catch (Exception e) {
    					e.printStackTrace();
    					return null;
    				}
    			} else {
    				return null;
    			}
    		}
    	};
    上面的代码是我做的一个项目里面用到的引用本地图片的方法,重写imagegetter,然后用ContentResolver来读取图片转换为Bitmap,然后再进行显示。但是。非常多时候会我们都须要引用的是网络图片的。那这种方法即可不通了。查找了非常多资料。假设直接在里面用异步的方法来载入图片的话。显示出来的是一个正方形的点的。那么问题来了,我们应该怎么去载入网络图片呢?

    首先。我们先创建一个URLDrawable,让它去继承BitmapDrawable,重写draw方法,这个有什么用呢?这个能够让你载入图片的时候显示初始的图片,也就是载入中的图片。

    URLDrawable.java:

    public class URLDrawable extends BitmapDrawable {
        // the drawable that you need to set, you could set the initial drawing
        // with the loading image if you need to
        protected Drawable drawable;
    
        @Override
        public void draw(Canvas canvas) {
            // override the draw to facilitate refresh function later
            if(drawable != null) {
                drawable.draw(canvas);
            }
        }
    }

    接下来就是重写ImageGetter

    URLImageParser继承ImageGetter

    放源代码

    URLImageParser.java

    public class URLImageParser implements ImageGetter {
        Context c;
        EditText container;
    
        /***
         * 构建URLImageParser将运行AsyncTask,刷新容器
         * @param t
         * @param c
         */
        public URLImageParser(EditText t, Context c) {
            this.c = c;
            this.container = t;
        }
    
        public Drawable getDrawable(String source) {
            URLDrawable urlDrawable = new URLDrawable();
    
            // 获得实际的源
            ImageGetterAsyncTask asyncTask = 
                new ImageGetterAsyncTask( urlDrawable);
    
            asyncTask.execute(source);
    
            //返回引用URLDrawable我将改变从src与实际图像标记
            return urlDrawable;
        }
    
        public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {
            URLDrawable urlDrawable;
    
            public ImageGetterAsyncTask(URLDrawable d) {
                this.urlDrawable = d;
            }
    
            @Override
            protected Drawable doInBackground(String... params) {
                String source = params[0];
                return fetchDrawable(source);
            }
    
            @Override
            protected void onPostExecute(Drawable result) {
            	// 设置正确的绑定依据HTTP调用的结果
                Log.d("height",""+result.getIntrinsicHeight()); 
                Log.d("width",""+result.getIntrinsicWidth()); 
                urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight());  
    
                // 改变当前可提取的參考结果从HTTP调用
                urlDrawable.drawable = result; 
    
                // 绘制图像容器
                URLImageParser.this.container.invalidate();
    
                // For ICS
                URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight()));
    
                // Pre ICS
                URLImageParser.this.container.setEllipsize(null);
            }
    
            /***
             * 得到Drawable的URL 
             * @param urlString
             * @return
             */
            public Drawable fetchDrawable(String urlString) {
                try {
                    InputStream is = fetch(urlString);
                    Drawable drawable = Drawable.createFromStream(is, "src");
                    drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 
                            + drawable.getIntrinsicHeight()); 
                    return drawable;
                } catch (Exception e) {
                    return null;
                } 
            }
    
            private InputStream fetch(String urlString) throws MalformedURLException, IOException {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet request = new HttpGet(urlString);
                HttpResponse response = httpClient.execute(request);
                return response.getEntity().getContent();
            }
        }
    }

    代码里的凝视也很的清楚明了了,这里就不用反复说明了,最重要的就是要重写onPostExecute。这种方法是载入完成之后UI的刷新用的,须要对drawable进行重绘才干在EditText中显示出来。并且不会和文字的位置重叠。是不是很的简单?



  • 相关阅读:
    Spring IOC
    C++ 内存模型
    C++ 多态
    Java 多态
    Java 自动装箱与自动拆箱
    C++ priority_queue
    多个页面使用到一些名称类的同一个接口,借助vuex实现
    element-ui自定义表单验证
    vue项目中导出excel文件
    数组对象根据某个属性进行排序
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5339854.html
Copyright © 2020-2023  润新知