In many weather or news applications, we can see the effects of subtitles rolling. The simplest implementation is the effect of running a horse lamp, which can be achieved with the Attributes provided by the system. you need to use custom controls to implement complicated operations. for example, let textview implement vertical scrolling. here I want to talk about the vertical scroll subtitle effect, and the content is not only text, but also images or other elements. if you don't talk nonsense, go directly to the following:
First, let's take a look at the core implementation:
My current practice is to rewrite scrollview and provide several important external methods:
Isscrolled () method to determine whether the current status is rolling
Setscrolled (Boolean flag) sets the scroll Switch
Setperiod (long period) sets the time from the start to the end of the scroll.
Setspeed (long speed) sets the scroll speed
Note the following:
1. Because it isScheduled operationSo you need to process the corresponding lifecycle of the activity: when the interface is invisible, SetSetscrolled (true)Turn on the scroll switch, from visible to invisible,Setscrolled (false)
Turn off the switch
2. You can callSetperiod (long period)AndSetspeed (long speed)Control the scroll speed
3. Because it isScrollviewImplemented. The content placed in the middle is the same as that in scrollview. You can not only set text, but also addImageAnd other elements to implement a complex UI
4.Text MixingAt present, I haven't done any detail on this demo yet. The most important part is text processing. We need to consider Chinese and English, full-width half-width, font size, paragraph processing, and computation of the corresponding character width for formatting.
Processing of images and other resources is relatively simple, mainly Processing Resolution and computing width and height
I will explain these parts in detail later.
This demo is temporarily written by me. The UI and text-and-text hybrid sorting, including the specific scrolling part, are relatively simple to process. You can expand it based on this example and make the desired results as needed:
Demo: http://download.csdn.net/detail/t12x3456/5875157
The corresponding code is as follows:
The first is custom View:
Package COM. tony. autoscroll; import android. content. context; import android. OS. handler; import android. util. attributeset; import android. util. log; import android. view. motionevent; import android. widget. scrollview;/*** @ author Tony **/public class autoscrollview extends scrollview {private final handler = new handler (); private long duration = 50; private Boolean isscrolled = false; private int C Urrentindex = 0; private long period = 1000; private int currenty =-1; private doublex; private Double Y; private int type =-1; /*** @ Param context */Public autoscrollview (context) {This (context, null );} /*** @ Param context * @ Param attrs */Public autoscrollview (context, attributeset attrs) {This (context, attrs, 0 );} /*** @ Param context * @ Param attrs * @ Param defstyle */public Utoscrollview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle);} public Boolean ontouchevent (motionevent event) {int action = event. getaction (); Switch (Action) {Case motionevent. action_down: x = event. getx (); y = event. gety (); If (type = 0) {setscrolled (false);} break; Case motionevent. action_move: Double Movey = event. gety ()-y; double movex = event. getx ()-x; If (Movey> 20 | Movey <-20) & (movex <50 | movex>-50) & getparent ()! = NULL) {getparent (). requestdisallowintercepttouchevent (true);} break; Case motionevent. action_up: If (type = 0) {currentindex = getscrolly (); setscrolled (true) ;}break; default: break;} return Super. ontouchevent (event) ;}@ override public Boolean onintercepttouchevent (motionevent p_event) {return true ;} /*** determine whether the current status is rolling ** @ return the isscrolled */Public Boolean isscrolled () {return isscrol Led;}/*** enable or disable the auto-scroll function ** @ Param isscrolled true: Enable, false: Disable */Public void setscrolled (Boolean isscrolled) {This. isscrolled = isscrolled; autoscroll ();}/*** get the pause time from the current scroll to the end. Unit: millisecond ** @ return the period */public long getperiod () {return period;}/*** sets the pause time from the current scroll to the end. Unit: millisecond ** @ Param period * The period to set */Public void setperiod (long period) {This. period = period;}/*** get the current rolling speed, Unit: milliseconds. The smaller the value, the faster the speed. ** @ Return the speed */public long getspeed () {return duration;}/*** sets the current rolling speed in milliseconds. The smaller the value, the faster the speed. ** @ Param speed * The duration to set */Public void setspeed (long speed) {This. duration = speed;} public void settype (INT type) {This. type = type;} private void autoscroll () {handler. postdelayed (New runnable () {@ override public void run () {Boolean flag = isscrolled; If (FLAG) {If (currenty = getscrolly () {try {thread. sleep (period);} catch (interruptedexception e) {e. printstacktrace () ;}currentindex = 0; scrollto (0, 0); handler. postdelayed (this, period);} else {currenty = getscrolly (); handler. postdelayed (this, duration); currentindex ++; scrollto (0, currentindex * 1) ;}} else {// currentindex = 0; // scrollto (0, 0) ;}}, duration );}}
Mainactivity:
package com.tony.autoscroll;import com.example.testautoscroll.R;import android.os.Bundle;import android.app.Activity;/** * link: blog.csdn.net/t12x3456 * @author Tony * */public class MainActivity extends Activity {private AutoScrollView scrollView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);scrollView = (AutoScrollView) findViewById(R.id.auto_scrollview);}@Overrideprotected void onStart() {// TODO Auto-generated method stubif(!scrollView.isScrolled()){scrollView.setScrolled(true);}super.onStart();}@Overrideprotected void onStop() {// TODO Auto-generated method stubif(scrollView.isScrolled()){scrollView.setScrolled(false);}super.onStop();}}
If any reprint is available, please declare the Source: Shi zhisha:
Http://blog.csdn.net/t12x3456