TextView Html solution for adding Images

Source: Internet
Author: User

I checked some of the online Html solutions for adding TextView with image tags, and found that many of them talk about the following:
1: rewrite ImageGetter to directly obtain network images (this is a common practice, but the main process will be blocked in the case of large images or poor Network)

ImageGetter imgGetter = new Html. imageGetter () {public Drawable getDrawable (String source) {Drawable drawable = null; URL url; try {url = new URL (source); drawable = Drawable. createFromStream (url. openStream (), ""); // obtain the network image} catch (Exception e) {return null;} drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); return drawable ;}};

And set TextView

TextView notice_content = (TextView) findViewById(R.id.notice_content);notice_content.setText(Html.fromHtml(getIntent().getStringExtra(CONTENT),imgGetter,null));

2: rewrite ImageGetter and load images asynchronously (this is a smooth method. Set a default image and load it asynchronously. If Drawable is used as an object to receive images, the mobile phone automatically scales the image, this is not what I want. What I need is a real-size image (the image is scaled in proportion to the resolution of the mobile phone ))

Import java. io. IOException; import java. io. inputStream; import java.net. malformedURLException; import org. apache. http. httpResponse; import org. apache. http. client. methods. httpGet; import org. apache. http. impl. client. defaultHttpClient; import android. content. context; import android. content. res. resources. notFoundException; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawabl E; import android. OS. asyncTask; import android. text. html. imageGetter; import android. util. displayMetrics; import android. view. display; import android. widget. textView; public class URLImageParser implements ImageGetter {Context c; TextView TV _image; private Drawable mdefadradrawable; public URLImageParser (TextView t, Context c) {this. TV _image = t; this. c = c; try {mdefadradrawable = c. getResources (). getDrawab Le (R. drawable. activity_detail_title_default); // Log. I ("-->", "execute");} catch (NotFoundException e) {mdefadradrawable = null; // Log. I ("-->", "execute 1"); e. printStackTrace () ;}@ Overridepublic Drawable getDrawable (String source) {// TODO Auto-generated method stubURLDrawable urlDrawable = new URLDrawable (); // main3. B. add (source); try {/** mdefadradrawable. setBounds (0, 0, 0 + * mdefadradrawable. getIntrinsicW Idth (), * mdefadradrawable. getIntrinsicHeight (); */urlDrawable. drawable = mdefadradrawable; URLImageParser. this. TV _image.invalidate ();} catch (Exception e) {e. printStackTrace ();}/** urlDrawable. setBounds (0, 0, 0 + mdefadradrawable. getIntrinsicWidth (), * mdefadradrawable. getIntrinsicHeight (); */ImageGetterAsyncTask asyncTask = new imagegetterasynctask(urldrawable;;asynctask.exe cute (source); return UrlDrawable;} public class ImageGetterAsyncTask extends AsyncTask <String, Void, Drawable> {URLDrawable urlDrawable; public ImageGetterAsyncTask (URLDrawable d) {this. urlDrawable = d ;}@ Overrideprotected void onPostExecute (Drawable result) {if (result! = Null) {urlDrawable. drawable = result; URLImageParser. this. TV _image.invalidate (); // Log. I ("-->", "execute 3") ;}@ Overrideprotected void onPreExecute () {urlDrawable. setBounds (0, 0, 0 + mdefadradrawable. getIntrinsicWidth (), 0 + mdefadradrawable. getIntrinsicHeight (); urlDrawable. drawable = mdefadradrawable; URLImageParser. this. TV _image.invalidate (); super. onPreExecute () ;}@ Overrideprotected Drawable doInBackground (String... params) {// TODO Auto-generated method stubString source = params [0]; // picture URLreturn fetchDrawable (source );} // obtain the URL's Drawable object public Drawable fetchDrawable (String urlString) {BitmapDrawable bitmap = null; Drawable drawable = null; try {InputStream is = fetch (urlString); bitmap = (BitmapDrawable) bitmapDrawable. createFromStream (is, "src"); drawable = bitmap; DisplayMetrics metrics = Constants. metrics; if (bitmap. getBitmap (). getWidth ()> metrics. widthPixels | bitmap. getBitmap (). getHeight ()> metrics. heightPixels) // perform proportional scaling program drawable. setBounds (0, 0, metrics. widthPixels, (int) (metrics. widthPixels * bitmap. getBitmap (). getHeight ()/bitmap. getBitmap (). getWidth (); elsedrawable. setBounds (0, 0, bitmap. getBitmap (). getWidth (), bitmap. getBitmap (). getHeight ();} catch (Exception e) {return null;} return drawable;} private InputStream fetch (String urlString) throws MalformedURLException, IOException {DefaultHttpClient httpClient = new condition (); httpGet request = new HttpGet (urlString); HttpResponse response = httpClient.exe cute (request); return response. getEntity (). getContent ();}}}

URLDrawable class

import android.graphics.Canvas;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;public class URLDrawable extends BitmapDrawable {protected Drawable drawable;@Overridepublic void draw(Canvas canvas) {    if (drawable != null) {   drawable.draw(canvas);  }}}

This is part of the solution. If you have a good solution, paste it and let me refer to the study! Thank you.

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.