The text in textview in Android achieves the dynamic effect, with the flashlight effect and flashing effect

Source: Internet
Author: User

I encountered a headache when I was learning Android-how to make the text work out as a drive lamp. At first I wanted to find some information on the Internet, just like everyone else, however, after searching for a few hours on the Internet, I found that the results were very annoying. I did not answer a lot of questions, so I started my own research path, I am an android cainiao character, and I am very fond of it. I gradually learned how to implement the drive-by-lamp effect in my learning of examples of Android. Below is a piece of my own code. please correct me if there are any errors.

Package IRDC. scrollingtext; import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview; public class scrollingtext extends Activity
{
Public textview T1;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
T1 = (textview) findviewbyid (R. Id. T1 );
T1.settext ");
T1.settextsize (30 );
T1.sethorizontallyscrolling (true );
T1.setfocusable (true );
}
} In this program, I set the focus of T1 to true (that is, the focus is on T1), and my colleague set the text display of T1 to exceed the display area (t1.sethorizontallyscrolling (true) the purpose of setting this line is to prevent the program from automatically breaking the text into a single line.) Of course, these attributes can be defined in the XML file. Next let's take a look at the main. xml file. <? XML version = "1.0" encoding = "UTF-8"?>
<Absolutelayout
Android: Id = "@ + ID/widget35"
Android: Layout _; fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "@ drawable/Black"
Xmlns: Android = "http://schemas.android.com/apk/res/android"
>
<Textview
Android: Id = "@ + ID/T1"
Android: layout_width = "100px" // This is the width of the text display area. This value must be smaller than the text width. Otherwise, it will not work.
Android: layout_height = "wrap_content"
Android: text = "@ string/str_id"
Android: textcolor = "@ drawable/Green"
Android: layout_x = "61px"
Android: layout_y = "69px"
Android: scrollx = "2px"
Android: singleline = "true"
Android: ellipsize = "marquee"
Android: marqueerepeatlimit = "marquee_forever"
>
</Textview> <viewstub Android: layout_y = "221dip" Android: Layout _; wrap_content "Android: layout_x =" 103dip "Android: Id =" @ + ID/viewstub01 "Android: layout_height = "wrap_content"> </viewstub>
</Absolutelayout> you noticed that in textview, I added three blue fields. singleline indicates that the textview Chinese text is a single line of text. If you set sethorizontallyscrolling (true) in your program) here you can stop writing, and the next step is the key. ellipsize = "marquee" indicates that we set textview to a drive lamp, marqueerepeatlimit = "marquee_forever" indicates the number of times that the rolling effect of the drive lamp is repeated. You can enter a natural number. Now let's try compiling. PS: Preparation of Flashing TextMany games have flickering text. For example, when an application is opened, the first page is often a large game pictorial, then there is a flickering "any key to continue" or "touch the screen". How can this problem be solved? After thinking about it, I thought I could use a timer to change the text color every Ms. The Code is as follows. If there is a better way, I hope I can send an email to tell me. Thank you! Private Boolean change = false; textview touchscreen = (textview) findviewbyid (R. id. touchscreen); // obtain the textview object timer = new timer (); timer. schedule (task, 1,300); // The parameters are delay (after which), duration (Execution interval) timertask task = new timertask (){
Public void run (){
Runonuithread (New runnable (){
Public void run (){
If (Change ){
Change = false;
Touchscreen. settextcolor (color. Transparent); // This is transparent, = no text can be seen
} Else {
Change = true;
Touchscreen. settextcolor (color. Red );
}
}});
}
};

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.