How to Make TextView automatically scroll vertically in Android

Source: Internet
Author: User
Tags drawtext

In progressAndroidDuring application development, it is very easy to scroll horizontally or make a marquee,TextviewThe attributes are supported. As long as the settings are accurate, they will scroll and be relatively simple to develop. HoweverTextviewVertical scrolling is not supported, so you need to implement it by yourself. The vertical rolling schemes provided by many netizens are all the same. ScrollView is used for scrolling, but they are 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

123456 /** * @author xushilin *  * Vertical scrolling TextView Widget */public class VerticalScrollTextView extends TextView

2. Implement constructor:

123456789101112131415161718192021222324252627282930313233 public VerticalScrollTextView(Context context) {        super(context);        init();    }    public VerticalScrollTextView(Context context, AttributeSet attr) {        super(context, attr);        init();    }    public VerticalScrollTextView(Context context, AttributeSet attr, int i) {        super(context, attr, i);        init();    }    private void init() {        setFocusable(true);        // Here we mainly deal with the default value if no content is input.        if(list==null){            list=new ArrayList<Notice>();            Notice sen=new Notice(0,"No notification announcement now");            list.add(0, sen);        }               // Font size of common text and paint brush color settings        mPaint = new Paint();        mPaint.setAntiAlias(true);        mPaint.setTextSize(16);        mPaint.setColor(Color.BLACK);        mPaint.setTypeface(Typeface.SERIF);             // Specifies the font size of the highlighted text and the paint brush color.        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.

123456789101112131415161718192021222324252627282930 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

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.