Android Realization of Text Scroll Play Effect Code

  • 2021-12-05 07:15:52
  • OfStack

During development, we will encounter text too long, TextView can not be fully displayed, but do not want to text newline display, which sometimes affects the appearance. At this time, we need to scroll the text so that users can see all the text.

Without saying much, go directly to the code:


import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class MarqueTextView extends TextView {
 public MarqueTextView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }
 public MarqueTextView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public MarqueTextView(Context context) {
 super(context);
 }
 @Override
 public boolean isFocused() {
 //true Indicates getting focus 
 return true;
 }
}

Usage:


<com.xxx.MarqueTextView
 android:singleLine="true"
 android:marqueeRepeatLimit="marquee_forever"
 android:ellipsize="marquee"/>

ps: Let's look at the implementation of android horizontal scrolling text

Realization of android Transverse Scroll Text


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

public class MarqueeTextView extends TextView {
 /**
  *  Text length 
  */
 private float textLength = 0f;
 /**
  *  Scroll bar length 
  */
 private float viewWidth = 0f;
 /**
  *  Text x Shaft   Coordinates of 
  */
 private float tx = 0f;
 /**
  *  Text Y Coordinates of the axis 
  */
 private float ty = 0f;
 /**
  *  Current length of text 
  */
 private float temp_tx1 = 0.0f;
 /**
  *  The length of the current transformation of the text 
  */
 private float temp_tx2 = 0x0f;
 /**
  *  Text scroll switch 
  */
 private boolean isStarting = false;
 /**
  *  Brush object 
  */
 private Paint paint = null;
 /**
  *  Text displayed 
  */
 private String text = "";
 /**
  *  Text scrolling speed 
  **/
 private float sudu;

 public MarqueeTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 /**
  *  Initialize the automatic scroll bar , Every time the text content is changed, it needs to be reinitialized 1 Times 
  *
  * @param windowManager  Get the screen 
  * @param text    What to display 
  * @param su    The speed at which text scrolls 
  */
 public void initScrollTextView(WindowManager windowManager, String text, float su) {
  //  Get the brush , Object of the parent class textPaint
  paint = this.getPaint();
  //  Get the text 
  this.text = text;
  this.sudu = su;
  textLength = paint.measureText(text);//  Gets the current text string length 
  viewWidth = this.getWidth();//  Get Width return mRight - mLeft;
  if (viewWidth == 0) {
   //  Gets the properties of the current screen 
   Display display = windowManager.getDefaultDisplay();
   viewWidth = display.getWidth();//  Get screen width  viewWidth  Is the starting position of scrolling and needs to be modified 
   //  You can start here again 
  }
  tx = textLength;
  temp_tx1 = viewWidth + textLength;
  temp_tx2 = viewWidth + textLength * 2;//  Define yourself, how much the text changes 
  // //  Size of text + Distance from the top 
  ty = this.getTextSize() + this.getPaddingTop();
 }

 /**
  *  Start scrolling 
  */
 public void starScroll() {
  //  Start scrolling 
  isStarting = true;
  this.invalidate();//  Refresh the screen 
 }

 /**
  *  Stop method , Stop scrolling 
  */
 public void stopScroll() {
  //  Stop scrolling 
  isStarting = false;
  this.invalidate();//  Refresh the screen 
 }

 /**
  *  Rewrite onDraw Method 
  */
 @Override
 protected void onDraw(Canvas canvas) {
  if (isStarting) {
   // A-Alpha Transparency /R-Read Red /g-Green Green /b-Blue Blue 
   //paint.setARGB(255, 200, 200, 200);
   canvas.drawText(text, temp_tx1 - tx, ty, paint);
   tx += sudu;
   //  When text scrolls to the far left of the screen 
   if (tx > temp_tx2) {
    //  Set the text to the rightmost start 
    tx = textLength;
   }
   this.invalidate();//  Refresh the screen 
  }
  super.onDraw(canvas);
 }
}

Use:


MarqueeTextView marqueeTextView = contentView.findViewById(R.id.five_text__view);
marqueeTextView.initScrollTextView(getWindowManager(), " Scroll text ", 1);
marqueeTextView.setText("");
marqueeTextView.starScroll();

Related articles: