Android TextView implements image/Text mixing. androidtextview

Source: Internet
Author: User

Android TextView implements image/Text mixing. androidtextview

TextView has been used for graphic loading in the game strategy. However, when you see the content in NetEase news, you can click an image to adjust it to a new Activity. It also feels like Textview, but I don't know how to do it. I think it can be realized by dynamically loading Textview and ImageView layout, but it is very complicated when it is large. So no method is found. Use this method tomorrow to see if it can be implemented.

Using TextView to achieve this effect, text mixing of pictures, text fonts of different colors, making phone calls and hanging browsers, etc.

The Code is as follows:

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MyTextView = (TextView) this. findViewById (R. id. img_iv );
// Create a SpannableString object
SpannableString sp = new SpannableString ("this sentence contains a Baidu hyperlink, highlighted, or in italic .");
// Set the hyperlink
Sp. setSpan (new URLSpan ("http://www.baidu.com"), 5, 7, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Set highlight style 1
Sp. setSpan (new BackgroundColorSpan (Color. RED), 17, 19, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
// Set the highlighted style (sp. setSpan (new ForegroundColorSpan (Color. YELLOW), Spannable. SPAN_EXCLUSIVE_INCLUSIVE );
// Set italics
Sp. setSpan (new StyleSpan (android. graphics. Typeface. BOLD_ITALIC), 27, 29, Spannable. SPAN_EXCLUSIVE_INCLUSIVE );
// Call
Sp. setSpan (new URLSpan ("tel: 4155551212"), 2, 5, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// The image is displayed in textview.
Drawable d = getResources (). getDrawable (R. drawable. ic_launcher );
D. setBounds (0, 0, d. getIntrinsicWidth (), d. getIntrinsicHeight ());
ImageSpan span = new ImageSpan (d, ImageSpan. ALIGN_BASELINE );
Sp. setSpan (span, 0, 1, Spannable. SPAN_INCLUSIVE_EXCLUSIVE );

// Set the SpannableString object to TextView.
MyTextView. setText (sp );
// Set TextView to clickable
MyTextView. setMovementMethod (LinkMovementMethod. getInstance ());
}



To solve this problem, Android gradually displays the text in TextView one by one,

If the number of words is small, you can consider the custom control to inherit the textView, override the TextView (Context context, AttributeSet attris) method, and customize the style. Get the property value of the custom style in the constructor and start a thread to send messages to the View at intervals and capture the sub-string for display. The code can be as follows:
Public class MyTextView extends TextView {
Int textIndex = 0;

String text;

String subText;

ShowTextThread showTextThread;

Public MyTextView (Context context ){
Super (context );
Text = "";

}

Public MyTextView (Context context, AttributeSet attrs ){
Super (context, attrs );
TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. xxxx );

Text = array. getString (R. styleable. xxxx_xxxx );

ShowTextThread = new ShowTextView ();

ShowTextThread. start ();

}

Class ShowTextThread extends Thread {

Public void run (){

While (textIndex! = Text. length ){

SubText = text. subString (0, textIndex );

PostIndvalidate ();
TextIndex ++;

}

}
}

Protected void onDraw (){

SetText (subText );

Super. onDraw ();

}
}

If you have any questions, please ask. I hope it will help you! If the requirements are met, please adopt it. It is not easy to write so many words!

In android, how does one display text in TextView with a smiley face icon?

TextView has the attributes drawableTop drawableBottom drawableLeft drawableRight.

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.