Android EditText loads HTML content (content includes network images)

Source: Internet
Author: User

Android EditText loads HTML content (content includes network images)

Html. fromHtml can be used to load HTML content. fromHtml has three parameters to set. The first parameter is the html content to be displayed, and the second parameter is the focus. ImageGetter is used to process image loading, third TagHandler can be set to null. Next, let's take a look at ImageGetter. Many Methods on the Internet directly reference local images, which are synchronized, for example:

private ImageGetter imageGetter = new ImageGetter() {@Overridepublic 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;}}};
The code above is a method used in my project to reference local images, rewrite imagegetter, and then use ContentResolver to read and convert the image to Bitmap, and then display it. However, most of the time, we need to reference network images, so this method won't work. I found a lot of information. If I used an Asynchronous Method to load images in it, it would show a square point. Then the problem arises, how should we load network images?

First, let's create a URLDrawable to inherit BitmapDrawable and override the draw method. What is the purpose of this? This allows you to display the initial image, that is, the loaded image, when loading the image.

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);        }    }}

Next, rewrite ImageGetter.

URLImageParser inherits ImageGetter

Source code

URLImageParser. java

Public class URLImageParser implements ImageGetter {Context c; EditText container;/***** construct URLImageParser and execute AsyncTask, refresh the container * @ 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 (); // obtain the actual source ImageGetterAsyncTask asyncTask = new vertex (urlDrawable ); asyncTask.exe cute (source); // return reference URLDrawable I will change from src with the actual image mark return urlDrawable;} public class ImageGetterAsyncTask extends AsyncTask
 
  
{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) {// set the correct binding according to the HTTP call result Log. d ("height", "" + result. getIntrinsicHeight (); Log. d ("width", "" + result. getIntrinsicWidth (); urlDrawable. setBounds (0, 0, 0 + result. getIntrinsicWidth (), 0 + result. getIntrinsicHeight (); // you can call urlDrawable from HTTP to change the extracted reference results. drawable = result; // draw the image container URLImageParser. this. container. invalidate (); // For ICS URLImageParser. this. container. setHeight (URLImageParser. this. container. getHeight () + result. getIntrinsicHeight (); // Pre ICS URLImageParser. this. container. setEllipsize (null);}/*** get the 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 {expose httpClient = new DefaultHttpClient (); httpGet request = new HttpGet (urlString); HttpResponse response = httpClient.exe cute (request); return response. getEntity (). getContent ();}}}
 

The comments in the Code are also very clear and clear. You don't need to repeat them here. The most important thing is to rewrite onPostExecute. This method is used to refresh the UI after loading, the drawable must be re-painted before it can be displayed in EditText without overlapping the position of the text. Is it very simple?



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.