Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of Racing Lantern by Android

Shulou Source: shulou.com Published: 2022-06-01 00:32:05 10月02日 Update

This article introduces the relevant knowledge of "how to achieve the racing lantern effect in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The first kind: relatively simple, but with strong limitations, it seems that you can only run from right to left, and there is a requirement: the length of the font must be greater than the length of the control, otherwise there will be no effect, the important code is the dark part, the specific code is at the end of the article.

Introduction of important codes

1. Android:ellipsize= "marquee" is set as the display mode of racing lantern.

2. Android:focusable= "true" gets focus

3. Android:focusableInTouchMode= "true" can gain focus by touching

4. Android:marqueeRepeatLimit= "marquee_forever" sets the number of times to repeat

5. Android:singleLine= "true" single-line display

Second: the use of timers to change the value of padding to play the effect of racing lanterns, in all directions, there is no first requirement, the main role is the setPadding method, remember to turn off the timer at the end!

1. Layout file

two。 The code that implements the function

Again, remember to turn off the timer!

The specific code is as follows:

The first method:

The second method:

Layout file:

Java file:

Public class MainActivity extends Activity {private TextView mytext; private Handler handler; private Runnable runnable; private int theroll=0; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); initView (); marquee () / / remember to call method} private void initView () {/ / instantiate mytext = (TextView) findViewById (R.id.mytext);} / / scrolling specific code private void marquee () {handler=new Handler (); runnable=new Runnable () {@ Override public void run () {if (theroll > 80) {theroll=-80 } theroll=theroll+2; / / you can control the scrolling direction by setting the following four parameters mytext.setPadding (theroll,0,0,0); handler.postDelayed (this,100); / / adjustable speed}}; handler.postDelayed (runnable,0);} @ Override protected void onDestroy () {super.onDestroy () / / in the end, remember to turn off the timer handler.removeCallbacks (runnable);}} "how to achieve the racing lantern effect in Android" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Code effects racing lanterns timers methods files importance function content layout article direction more focus knowledge length practicality learning and then again Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Apple Shulou Technology Microsoft