How to animate the textview (very very long text )scroll automatically horizontally

前端 未结 11 877
长发绾君心
长发绾君心 2020-12-16 06:30

I am not interested in Marquee because, in Marquee you can not control the speed of marquee. I have tried to animate the textview but Parent view clips the text at the end e

相关标签:
11条回答
  • I am sure this will definitely solve the problem of the large audience out there.

    Q: Auto-scroll a single line long text message(either using hard_coding or from string.xml) horizontally & infinitely at a reasonable speed but using marquee(try it once at least). No clipping

    Step 1: In activity_main.xml file:

    <TextView
        android:text="either hard coding or from string.xml"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"  
        android:background="@color/colorPrimary"
        android:textSize="18sp"
        android:marqueeRepeatLimit="marquee_forever"
        android:textColor="@android:color/background_light" />
    

    Step 2: In main_activity java file public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        TextView textView = (TextView) findViewById(R.id.textView2);
        textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        textView.setSelected(true);
        textView.setSingleLine(true);
        textView.setText("Oxfam says 8 men as rich as half the world. | Govt may set threshold for probe into deposits. | At least 32 dead after Turkish plane hits village.");}}
    

    //one can remove the last line line if he has already feed the long input

    0 讨论(0)
  • 2020-12-16 07:08

    Can try out this. This is a solution using TranslateAnimation for creating an auto scrolling text (horizontal scroll, from Right to Left) (Tested on Android 8)

    Class: AnimationAutoTextScroller.java

    /**
     * A Class for automatically scrolling text horizontally from Right to Left 
     * using TranslateAnimation so that the scrolling speed can be controlled -Suresh Kodoor
     */
    
    public class AnimationAutoTextScroller {
    
        Animation animator;
        TextView  scrollingTextView;
        int       duration = 50000;  // default value
    
        public AnimationAutoTextScroller(TextView tv, float screenwidth) {
    
            this.scrollingTextView = tv;
            this.animator = new TranslateAnimation(
                    Animation.ABSOLUTE, screenwidth,
                    Animation.RELATIVE_TO_SELF, -1f,
                    Animation.RELATIVE_TO_SELF, 0f,
                    Animation.RELATIVE_TO_SELF, 0f
            );
            this.animator.setInterpolator(new LinearInterpolator());
            this.animator.setDuration(this.duration);
            this.animator.setFillAfter(true);
            this.animator.setRepeatMode(Animation.RESTART);
            this.animator.setRepeatCount(Animation.INFINITE);
    
            // setAnimationListener();
        }
    
        public void setDuration(int duration) {
            this.duration = duration;
        }
    
        public void setScrollingText(String text) {
            this.scrollingTextView.setText(text);
        }
    
        public void start() {
            this.scrollingTextView.setSelected(true);
            this.scrollingTextView.startAnimation(this.animator);
        }
    
        public void setAnimationListener() {
    
            animator.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                }
                public void onAnimationEnd(Animation animation) {
                    // This callback function can be used to perform any task at the end of the Animation
                }
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }
    }
    

    Layout XML: (keep the TextView under a HorizontalScrollView)

     <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            app:layout_constraintBottom_toTopOf="@+id/hguide3"
            app:layout_constraintEnd_toStartOf="@+id/vguide2"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toEndOf="@+id/vguide1"
            app:layout_constraintTop_toBottomOf="@+id/hguide2">
    
        <TextView
            android:id="@+id/translateanimatortextviewscroller"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="0dp"
            android:layout_marginEnd="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginStart="0dp"
            android:layout_marginTop="0dp"
            android:text=""
            android:singleLine="true"
            android:focusable="true"
            android:scrollHorizontally="true"
            android:background="#000000ff"
            android:textColor="#ff0000"
            android:textSize="55dp"
            android:textStyle="bold"
            android:typeface="sans" />
    
          </HorizontalScrollView>
    

    Activity:

    TextView scrollertextview = findViewById(R.id.translateanimatortextviewscroller);
    textscroller = new AnimationAutoTextScroller(scrollertextview, screenwidth);
    textscroller.setScrollingText(scrollertext);
    textscroller.setDuration(60000);
    textscroller.start();
    
    0 讨论(0)
  • 2020-12-16 07:09
    <translate>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXDelta="1500"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:toXDelta="-1250" />
    

    0 讨论(0)
  • 2020-12-16 07:10

    Its really frustrating,... But the answer is simple, Use Edittext instead of TextView, and wrap it in horizontalscrollview At the same time, setfocusable: false

       <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:scrollbars="none">
    
            <EditText
                android:id="@+id/post_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:focusable="false"
                android:outlineAmbientShadowColor="@color/colorPrimaryDark"
                android:shadowColor="#000000"
                android:shadowDx="1.5"
                android:shadowDy="1.3"
                android:shadowRadius="1.6"
                android:singleLine="true"
                android:textAlignment="center"
                android:textColor="@color/colorPrimary"
                android:textSize="20dp" />
        </HorizontalScrollView>
    

    Thanks to Hein, add the animation code

    final EditText textView =  view.findViewById(R.id.post_message);
            textView.startAnimation((Animation) AnimationUtils.loadAnimation(context,R.anim.horizontal_animation));
            String message="LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLong Text.";
            textView.setText(message);
    
    0 讨论(0)
  • 2020-12-16 07:11

    This is what worked for me. Place your textview inside a scroll view and then perform TranslateAnimation on the scrollview's child, my case its the LinearLayout. I am actually adding multiple views dynamically inside this linear layout.

    <ScrollView android:id="@+id/textScrollView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/textLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </LinearLayout>
    </ScrollView>
    
    TranslateAnimation slide = new TranslateAnimation(0, 0, height, -textLayout.getHeight());
        slide.setDuration(movementSpeed);
        slide.setRepeatCount(-1);
        slide.setRepeatMode(Animation.RESTART);
        slide.setInterpolator(new LinearInterpolator());
    
    textLayout.startAnimation(slide);
    
    height --> The point start scrolling up (in my case device height (device bottom))
    movementSpeed --> scrolling speed
    
    0 讨论(0)
提交回复
热议问题