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

前端 未结 11 876
长发绾君心
长发绾君心 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条回答
  • 2020-12-16 06:51

    Here was my SOLUTION

    To make the long text inside textview not be cut by parent view or by screen, I have done two things.

    First, let textview inside a scroolview like below code

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/marquee_text"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:maxLines="1"
            android:textColor="@android:color/black"
            android:textSize="30sp"/>
    </ScrollView>
    

    Then, I measure my text size then refine the textview param by doing this.

        marqueeText.setText("my long text");
        Paint textPaint = marqueeText.getPaint();
        String text = marqueeText.getText().toString();//get text
        int width = Math.round(textPaint.measureText(text));//measure the text size
        ViewGroup.LayoutParams params =  marqueeText.getLayoutParams();
        params.width = width;
        marqueeText.setLayoutParams(params); //refine
    
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
        int screenWidth = displaymetrics.widthPixels;
    
        //this is optional. do not scroll if text is shorter than screen width
        //remove this won't effect the scroll
        if (width <= screenWidth) {
            //All text can fit in screen.
            return;
        }
        //set the animation
        TranslateAnimation slide = new TranslateAnimation(0, -width, 0, 0);
        slide.setDuration(20000);
        slide.setRepeatCount(Animation.INFINITE);
        slide.setRepeatMode(Animation.RESTART);
        slide.setInterpolator(new LinearInterpolator());
        marqueeText.startAnimation(slide);
    

    I hope this solution which took me half a day to research can help others who might meet the same problem like me.

    0 讨论(0)
  • 2020-12-16 06:56

    Just add this to your textview

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:textSize="30dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Your_Text" />
    
    0 讨论(0)
  • 2020-12-16 06:57

    I think you can use translate animation. Something like this

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromXDelta="100"
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toXDelta="-100" />
    

    And add to your textview like this

    textview.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_animation));
    

    Hope it can help you.

    0 讨论(0)
  • 2020-12-16 06:58

    Use this simple way with ellipsize and marquee options using @rajath answer

    <TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-12-16 07:01

    Add this Animation file:

    <translate
        android:duration="7000"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:repeatCount="10"
        android:repeatMode="restart"
        android:toYDelta="-100%p" />
    

    /*Put your text view inside scroll*/
            <ScrollView
                android:layout_width="@dimen/dp_size_220"
                android:layout_height="@dimen/dp_size_16"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/iv_myra_notification"
                app:layout_constraintRight_toLeftOf="@+id/iv_one_way"
                app:layout_constraintTop_toTopOf="parent">
                <TextView
                    android:id="@+id/marquee_text"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="left"
                    android:text="@{itemData.notification.message}"
                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    tools:text="bfnfkjnvlen jknjkgnojeng"/>
            </ScrollView>
    
    0 讨论(0)
  • 2020-12-16 07:04

    try this code it will help you out Shri n HERO

    <?xml version="1.0" encoding="utf-8"?>
    <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" />
    
    </translate>
    
    0 讨论(0)
提交回复
热议问题