Android text view textview to achieve the running lantern effect

Time:2021-7-21

This example for you to share the Android text view textview effect of the specific code, for your reference, the specific content is as follows

MainActivity

package com.example.junior;
 
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
 
public class MarqueeActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_ marquee; //  Declare a text view object
    private boolean isPaused = false; //  Does the running lantern text pause scrolling
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marquee);
        //Get TV from the layout file_ Text view of Marquee
        tv_marquee = findViewById(R.id.tv_marquee);
        //For TV_ Marquee set click listener
        tv_marquee.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_ Marquee) {// click text view TV_ marquee
            isPaused = !isPaused;
            if (isPaused) {
                tv_ marquee.setFocusable(false); //  No focus allowed
                tv_ marquee.setFocusableInTouchMode(false); //  It is not allowed to get focus when touching
            } else {
                tv_ marquee.setFocusable(true); //  Allow focus
                tv_ marquee.setFocusableInTouchMode(true); //  Allows you to get focus when you touch it
                tv_ marquee.requestFocus(); //  Force to get the focus, let the lantern roll
            }
        }
    }
}

 layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <!--  This is a normal text view -- >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        Android: text = "running lantern effect, click pause, and then click resume" / >
 
    <!--  This is the text view of running lantern scrolling. If the ellipsize property is set to true, the text scrolls from right to left -- >
    <TextView
        android:id="@+id/tv_marquee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColor="#000000"
        android:textSize="17sp"
        Android: text = "news: red warning, super typhoon moranti is about to land, please close the doors and windows, prepare enough food and grass, and prepare for flood control and disaster relief!" / >
</LinearLayout>

result

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]