Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of Racing Lantern by Android

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report