Now in the project to do a question bank, because some mathematical symbols or chemical symbols, such as no way directly in front of the display, so the use of text-to-picture mixed;
The data returned in the background is directly in HTML format.
So I started to study how the controls are going to display HTML
First post the reference article, thanks for sharing!
1, this is only suitable for loading local images, or compatible version under 4.0
Android TextView display with HTML text two-------"TextView show local Pictures"
In this way, as long as the Baidu search Android TextView set HTML data, will find out a lot of similar, but feel this for the great God wrote the best!
If the HTML data, only text, use the following notation is OK,
If there is no picture in the HTML data, just the text, it is OK to set the String html = "<p> This is the test </p><span> data </span> "; TextView Mtvhello = (TextView) This.findviewbyid (R.id.tv_hello); Mtvhello.settext (html.fromhtml (Html));
If the HTML data contains the local picture path, you can refer to the link above, the big God of the two ways can be
It's mostly more of this load picture parameter html.imagegetter imagegetter = new Html.imagegetter () { @Override public drawable Getdrawable (String source) { //implementation method can refer to the above post, according to their own needs to implement return null; } }; Html. fromhtml when adding the above parameters Mtvhello.settext (html.fromhtml (html,imagegetter,null));
In fact, if your project is only compatible with less than 4.0, and not afraid of the card, the above post also has the implementation method (but this demand is not estimated now!). )
For a reason, 4.0 below is the main thread can request the network, but after 4.0 is not able to request the network in the main thread,
url = new URL (source); drawable = Drawable.createfromstream (Url.openstream (), "");
Above this writing will be in the main line loads download pictures, although 4.0 below can be used, but if the picture is large, the estimate is directly dead, so still do not use!
If it is a local image, you can use the two pictures in the post, according to their own needs to achieve the OK;
2, asynchronously load the HTML picture reference article (StackOverflow, sometimes can not access, find a way to yourself ...)
Android HTML Imagegetter as Asynctask
The above post perfectly solves the problem!
Posted code, save some friends can not access the post, not see the code
Import Android.graphics.canvas;import Android.graphics.drawable.bitmapdrawable;import android.graphics.drawable.drawable;/** * Created by Administrator on 2015/4/30. */public class Urldrawable extends bitmapdrawable{ //The drawable that's need to set, you could set the initial DRA Wing //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 (Drawa ble = null) { drawable.draw (canvas);}} }
Package Com.jddl.tvhtmldemo;import Android.content.context;import Android.graphics.drawable.drawable;import Android.os.asynctask;import Android.text.html;import Android.view.view;import Org.apache.http.HttpResponse;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Java.io.ioexception;import java.io.inputstream;import java.net.malformedurlexception;/** * Created by Administrator On 2015/4/30. */public class Urlimageparser implements Html.imagegetter {Context C; View container; @Override protected Object Clone () throws Clonenotsupportedexception {return super.clone (); }/** * Construct the urlimageparser which would execute asynctask and refresh the container * * @param t * @param c */public Urlimageparser (View T, Context c) {this.c = C; This.container = t; Public drawable getdrawable (String source) {urldrawable urldrawable = new urldrawable (); //Get the actual source imagegetterasynctask Asynctask = new Imagegetterasynctask (urldrawable); Asynctask.execute (source); Return reference to Urldrawable where I'll change with actual image from//the SRC tag return Urldrawa ble } 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) {//set the correct bound according to The result from an HTTP call Urldrawable.setbounds (0, 0, result.getintrinsicwidth (), Result.getintrinsicheight ()) ; Change the reference of the drawable to the result/ From the HTTP call urldrawable.drawable = result; Redraw the image by invalidating the container URLImageParser.this.container.invalidate (); }/** * Get the drawable from URL * * @param urlstring * @return * * Public drawable fetchdrawable (String urlstring) {try {InputStream is = fetch (urlstring); drawable drawable = Drawable.createfromstream (IS, "src"); Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); return drawable; } catch (Exception e) {return null; }} private InputStream fetch (String urlstring) throws Malformedurlexception, IOException {Defau Lthttpclient httpClient = new Defaulthttpclient (); HttpGet request = new HttpGet (urlstring); HttpResponse response = Httpclient.execute (request); Return response.getentity (). getcontent (); }} public Urlimageparser () {super (); }}
Since I have a selection of items in my project, and a single choice, so I used the TextView, RadioButton and CheckBox three kinds of controls,
The following code is used
public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {s Uper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView Mtvhello = (TextView) This.findviewbyid (R.id.tv_hello); String html = "<p> This is test </p><span> data </span> "; Mtvhello.setmovementmethod (Scrollingmovementmethod.getinstance ()); Urlimageparser urlimggetter = new Urlimageparser (Mtvhello, this); Mtvhello.settext (html.fromhtml (Html, urlimggetter, null)); RadioButton mrbhtml = (RadioButton) This.findviewbyid (r.id.rb_html); Urlimageparser URLIMGGETTERRB = new Urlimageparser (mrbhtml, this); Mrbhtml.settext (html.fromhtml (Html, URLIMGGETTERRB, null)); CheckBox mcbhtml = (checkbox) This.findviewbyid (r.id.cb_html); Urlimageparser URLIMGGETTERCB = new Urlimageparser (mcbhtml, this); Mrbhtml.settext(Html.fromhtml (Html, URLIMGGETTERCB, null)); }}
Simply say my understanding (little brother engaged in Android is not long, if there is a misunderstanding, please give us a lot of advice!) )
The Urlimageparser class implements the Html.imagegetter interface, and the notation is no different from the other.
In getdrawable first return a urldrawable, is an empty picture
Then use, asynchronously load the picture, and after the load is complete, refresh the picture and view
Thought seems very simple, but did not think how to achieve!
Thanks again for sharing!
Because most of them are written by TextView using the method,
But I think: because the RadioButton and the Checkbox are all TextView subclasses, it should also be used!
The final test proves that the idea is feasible, stick the running photo
Android Drip---Textview,radiobutton set HTML text, load network pictures