My Android Advanced tour------> Android Display image method in TextView

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:24px"><span style="white-space:pre"></span>Interview questions: please tell me which methods the Android SDK supports to display Rich text information (different colors, sizes, and text messages containing images), and briefly describe the implementation method.</span></p></p><p><p><span style="font-size:24px"><span style="white-space:pre"></span>Answer:<span style="font-size:24px">The Android SDK supports the following ways to display Rich text information <span style="font-size:24px">. </span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="white-space:pre"></span>1. Use the TextView component to display Rich Text Information. Rich Text tags can be used in TextView components to display Rich Text information, which is similar to HTML tags, but is simpler than HTML tags and supports a limited number of ways to display rich Text. For example, The <font> label is used to set the font and color,<b> used to set BOLD. The text that contains these tags cannot be used directly as the parameter value of the Textview.settext method, but first use the Html.fromhtml method to convert the text to a charsequnce object, and then the object as <span style="font-size:24px"> The parameter value of the Textview.settext method. </span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="white-space:pre"></span>2. Use the WebView component to display the HTML Page.</span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="white-space:pre"></span>3. Inherit the view class or its subclasses, and override the OnDraw method to draw the text or image directly in the Method.</span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><br></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="white-space:pre"></span>The above 3 methods all support the graphics and text blending Effect. The 1th method, however, is to implement the Imagegetter interface when displaying an image (using the Tag) and return the Drawable object that encapsulates the image resource through the imagegetter.getdrawable method.</span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="white-space:pre"></span>Displaying images in a TextView component can also use Imagespan objects, Imagespan objects are used to encapsulate bitmap objects, and spannablestring objects are encapsulated by Imagespan objects, and finally Displays the image on the <span style="font-size:24px">TextView component as the parameter value of the Textview.settext method for the spannablestring Object. </span></span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><br></span></span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)">==================================================================================================</span><br></span></span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><strong>Android display Image method in TextView</strong><br></span></span></p></p><p><p> <span style="font-size:24px"><span style="color:rgb (51,51,51); line-height:26px; text-align:center; Background-color:rgb (254,254,242) "><span style=" color:rgb (51,51,51); line-height:26px "><span style=" font-family : ' Comic Sans MS '; font-size:18px; White-space:pre "> </span> <strong> <span style=" Font-family:simsun "> method one: override Textview's OnDraw method, It's not too good to control the position of the space occupied by the font after displaying the Picture. This is generally recommended if the font is overlapping on the Picture. Time relationship, This does not pay the Source. </span> </strong> </span> <strong> <span style="font-family:simsun"> <br> </span> </strong> </span> </span> </p></p><p><p> <span style="font-size:24px"><span style="color:rgb (51,51,51); line-height:26px; text-align:center; Background-color:rgb (254,254,242) "><span style=" color:rgb (51,51,51); line-height:26px "> <strong> <span style=" Font-family:simsun "><span style=" white-space:pre "> </span> method two: display images in TextView components using tags </span> </strong> </span> </span> </span> <span style="color:rgb (51,51,51); line-height:26px; text-align:center; font-size:24px; Background-color:rgb (254,254,242) "><span style=" font-family:simsun "> </span> </span> </p></p><pre name="code" class="java"><pre name="code" class="java">@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.oyp); TextView mytextview= (TextView) Findviewbyid (r.id.mytextview); String html = ""; imagegetter imggetter = new Imagegetter () {@Overridepubl IC drawable getdrawable (String source) {int id = integer.parseint (source); Load Image resource Drawable d = getresources (). getdrawable (id);//set to display the size of the image (shown in the original Size) d.setbounds (0, 0, d.getintrinsicwidth (), D.getintrinsicheight ()); return d;}; Charsequence charsequence = html.fromhtml (Html, imggetter, null); mytextview.settext (charsequence); myTextView.append ("Ouyangpeng http://blog.csdn.net/ouyang_peng");}</pre></pre><br><br><p><p><span style="font-family:SimSun; font-size:24px"><strong><span style="color:rgb(51,51,51); line-height:26px">Note the following sentence:</span></strong></span></p></p><p><p><span style="font-family:SimSun; font-size:24px"><strong><span style="color:rgb(51,51,51); line-height:26px"><span style="white-space:pre"></span>String html = "";</span></strong></span></p></p><p><p><span style="font-family:SimSun; font-size:24px"><strong><span style="color:rgb(51,51,51); line-height:26px"><span style="white-space:pre"></span>IMG src = After "there is also a" number in addition to ". If these two ' numbers are removed, it becomes a string html = ""; will be error, because SRC is directly equal to a number, is not resolved.</span><br></strong></span></p></p><br><br><p><p></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="color:rgb(51,51,51); font-family:SimSun; font-size:23.9999980926514px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><strong><span style="white-space:pre"></span>Method Three: Use the Imagespan object to display the image in the TextView component</strong></span></span></span></span></span></span></span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.oyp); TextView mytextview= (TextView) Findviewbyid (r.id.mytextview);//get resource Image Bitmapbitmap bitmap= based on resource ID Bitmapfactory.decoderesource (getresources (), r.drawable.icon);//create Imagespan objects from bitmap objects Imagespan imagespan=new Imagespan (this,bitmap);//create a Spannablestring object to insert an image encapsulated with a Imagespan object spannablestring spannablestring=new Spannablestring ("icon");//replace Iconspannablestring.setspan with Imagespan object (imagespan, 0, 4, spannable.span_exclusive_ EXCLUSIVE);//display the image on the TextView component Mytextview.settext (spannablestring); mytextview.append ("Ouyangpeng http:/ Blog.csdn.net/ouyang_peng ");}</pre></pre><br><br><p><p></p></p><p style="text-align:center"><p style="text-align:center"><span style="font-family:SimSun; color:#333333"><span style="font-size:23.9999980926514px; line-height:26px; background-color:rgb(254,254,242)"><strong><br></strong></span></span></p></p><p style="text-align:left"><p style="text-align:left"><span style="font-family:SimSun; color:#333333"><span style="font-size:23.9999980926514px; line-height:26px; background-color:rgb(254,254,242)"><strong>PS: can refer to the following links to deepen understanding</strong></span></span></p></p><p style="text-align:left"><p style="text-align:left"><span style="font-family:SimSun; color:#333333"><span style="font-size:23.9999980926514px; line-height:26px; background-color:rgb(254,254,242)"><strong>My Android Advanced tour------> android Add a background color to the text displayed in the TextView component<br></strong></span></span></p></p><p style="text-align:left"><p style="text-align:left"><span style="font-family:SimSun; color:#333333"><span style="font-size:23.9999980926514px; line-height:26px; background-color:rgb(254,254,242)">(address: Http://blog.csdn.net/ouyang_peng/article/details/46918379)</span></span></p></p><p style="text-align:left"><p style="text-align:left"><span style="font-family:SimSun; color:#333333"><span style="font-size:23.9999980926514px; line-height:26px; background-color:rgb(254,254,242)"><strong>http://blog.csdn.net/yanzi1225627/article/details/24590029<br></strong></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="color:rgb(51,51,51); font-family:SimSun; font-size:23.9999980926514px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><strong>Http://www.linuxidc.com/Linux/2012-05/59803.htm</strong></span></span></span></span></span></span></span></p></p><p><p> <span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px">< Span style= "font-size:24px" ><span style="color:rgb (51,51,51); font-family:kaiti_gb2312; font-size:18px; line-height:26px; text-align:center; Background-color:rgb (254,254,242) "><span style=" Color:rgb (51,51,51); font-family:simsun; font-size:23.9999980926514px; line-height:26px; text-align:center; Background-color:rgb (254,254,242) "> <strong> <br> </strong> </span> </span> </span> </span> </span> </span> </p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="color:rgb(51,51,51); font-family:SimSun; font-size:23.9999980926514px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="font-weight:bold"></span></span></span></span></span></span></span></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><span style="font-size:18px">==================================================================================================</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><strong><span style="font-size:18px">Ouyangpeng welcome reprint, sharing with people is the source of progress!</span></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><strong><span style="font-size:18px">Reprint please keep the original address <span style="color:rgb(153,51,153)">: Http://blog.csdn.net/ouyang_peng</span></span></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><span style="font-family:KaiTi_GB2312"><span style="font-size:18px">==================================================================================================</span></span></p></p><span style="font-size:12px"><span style="font-size:12px">  </span></span><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><br></span></span></span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><br></span></span></span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><br></span></span></span></span></span></span></p></p><p><p><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="font-size:24px"><span style="color:rgb(51,51,51); font-family:KaiTi_GB2312; font-size:18px; line-height:26px; text-align:center; background-color:rgb(254,254,242)"><br></span></span></span></span></span></span></p></p><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>My Android Advanced tour------> Android Display image method in TextView</p></p></span>

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.