When developing Android applications, it is easy to scroll horizontally or to make a marquee. The attributes of textview are supported. As long as the settings are accurate, textview will scroll, making development easier, however, textview does not support vertical scrolling, so vertical scrolling needs to be implemented by yourself. Many users provide the same vertical rolling scheme. scrollview is used for scrolling, but not perfect, it is awkward to do. A netizen gave a clear rolling idea of the lyrics, which could solve the problem fundamentally. Therefore, the scrolling I implemented was implemented on the basis of the netizen and encapsulated a view, view inherits from textview. First, let's look at the implementation results:
The key points for achieving the effect shown in the figure are:
1. Override the ondrow method to calculate the rolling distance each time.
2. Calculate the Y axis of the view to make the current display highlighted.
3. Regularly refresh the view so that the interface keeps refreshing and scrolling.
4. Implement the data structure and pass the data to the view.
Let's take a look at the main code:
1. Create a class to inherit textview
/*** @ Author xushilin *** vertical scrolling textview widget */public class verticalscrolltextview extends textview
2. Implement constructor:
Public verticalscrolltextview (context) {super (context); Init ();} public verticalscrolltextview (context, attributeset ATTR) {super (context, ATTR); Init ();} public verticalscrolltextview (context, attributeset ATTR, int I) {super (context, ATTR, I); Init () ;}private void Init () {setfocusable (true ); // here we mainly deal with the default value if (list = NULL) {list = new arraylist <notice> (); notice Sen = new notice (0, "No notification announcement"); list. add (0, SEN);} // font size of the common text, and set the paint color mpaint = new paint (); mpaint. setantialias (true); mpaint. settextsize (16); mpaint. setcolor (color. black); mpaint. settypeface (typeface. serif); // The font size of the highlighted text and the paint color settings mpathpaint = new paint (); mpathpaint. setantialias (true); mpathpaint. setcolor (color. red); mpathpaint. settextsize (16); mpathpaint. settypeface (typeface. sans_serif );}
3. Write the ondraw method, calculate the line spacing of the text, and draw the common text and highlighted text in this method.
protected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(0xEFeffff);Paint p = mPaint;Paint p2 = mPathPaint;p.setTextAlign(Paint.Align.CENTER);if (index == -1)return;p2.setTextAlign(Paint.Align.CENTER);canvas.drawText(list.get(index).getName(), mX, middleY, p2);float tempY = middleY;for (int i = index - 1; i >= 0; i--) {tempY = tempY - DY;if (tempY < 0) {break;}canvas.drawText(list.get(i).getName(), mX, tempY, p);}tempY = middleY;for (int i = index + 1; i < list.size(); i++) {tempY = tempY + DY;if (tempY > mY) {break;}canvas.drawText(list.get(i).getName(), mX, tempY, p);}}
4. Calculate the Y axis value and update the index
protected void onSizeChanged(int w, int h, int ow, int oh) {super.onSizeChanged(w, h, ow, oh);mX = w * 0.5f; mY = h;middleY = h * 0.5f;}private long updateIndex(int index) {if (index == -1)return -1;this.index=index;return index;}
5. Update the view regularly and expose the interface to the client program for calling.
public void updateUI(){new Thread(new updateThread()).start();}class updateThread implements Runnable {long time = 1000;int i=0;public void run() {while (true) {long sleeptime = updateIndex(i);time += sleeptime;mHandler.post(mUpdateResults);if (sleeptime == -1)return;try {Thread.sleep(time);i++;if(i==getList().size())i=0;} catch (InterruptedException e) {e.printStackTrace();}}}}Handler mHandler = new Handler();Runnable mUpdateResults = new Runnable() {public void run() {invalidate(); }};
6. Call in XML layout file:
<?xml version="1.0" encoding="utf-8"?> <!-- Demonstrates scrolling with a ScrollView. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <com.demo.xsl.text.SampleView android:id="@+id/sampleView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/selector" /> </LinearLayout>
7. Call in Java code to pass data:
Package COM. demo. XSL. text; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. OS. handler; public class verticalscrolltextactivity extends activity {sampleview msampleview; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); msampleview = (sampleview) findviewbyid (R. I D. sampleview1); List lst = new arraylist <sentence> (); For (INT I = 0; I <30; I ++) {if (I % 2 = 0) {sentence Sen = new sentence (I, I + ", the top three winners of the Ballon d'or, Cristiano Ronaldo, Harvey shortlisted"); lst. add (I, SEN);} else {sentence Sen = new sentence (I, I + ", the bull wants to use the three major players for Warcraft ???? "); Lst. Add (I, SEN) ;}// pass data to the view msampleview. setlist (LST); // update viewmsampleview. updateui ();}}
Click to download source code