8

我对 Marquee 不感兴趣,因为在 Marquee 中您无法控制 Marquee 的速度。我试图为 textview 设置动画,但即使包含 textviews 的所有父布局和视图组都设置了两个标志 clipchildren=false,clipToPadding=false,父视图也会在末尾剪辑文本。

我错过了什么还是有更好的解决方法?

xml看起来像

<TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="66dp"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#585858"
        android:textSize="32sp" >
    </TextView>

和代码片段看起来像

TextView textView2 = (TextView)findViewById( R.id.textview1 );    
textView2.startAnimation((Animation)AnimationUtils.loadAnimation(this, R.anim.translate));
4

11 回答 11

6

我认为您可以使用翻译动画。像这样的东西

<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" />

并像这样添加到您的文本视图中

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

希望它可以帮助你。

于 2013-09-16T11:38:52.893 回答
6

我相信这肯定会解决大量观众的问题。

问:以合理的速度水平和无限地自动滚动单行长文本消息(使用 hard_coding 或来自 string.xml),但使用选框(至少尝试一次)。无剪裁

第 1 步:在 activity_main.xml 文件中:

<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" />

第 2 步:在 main_activity java 文件中 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.");}}

//如果他已经提供了长输入,则可以删除最后一行

于 2017-07-08T08:02:40.773 回答
5

只需将其添加到您的文本视图中

<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" />
于 2016-09-16T10:22:53.073 回答
3

这是我的解决方案

为了使 textview 中的长文本不会被父视图或屏幕剪切,我做了两件事。

首先,让 textview 在 scoolview 中,如下面的代码

<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>

然后,我测量我的文本大小,然后通过这样做来优化 textview 参数。

    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);

我希望这个花了我半天时间研究的解决方案可以帮助其他可能遇到像我一样问题的人。

于 2016-10-20T14:05:26.507 回答
2

可以试试这个。这是使用 TranslateAnimation 创建自动滚动文本(水平滚动,从右到左)的解决方案(在 Android 8 上测试)

类: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) {
            }
        });
    }
}

布局 XML:(将 TextView 保留在 Horizo​​ntalScrollView 下)

 <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>

活动:

TextView scrollertextview = findViewById(R.id.translateanimatortextviewscroller);
textscroller = new AnimationAutoTextScroller(scrollertextview, screenwidth);
textscroller.setScrollingText(scrollertext);
textscroller.setDuration(60000);
textscroller.start();
于 2019-08-01T14:03:57.327 回答
1

添加这个动画文件:

<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>
于 2019-09-19T09:15:54.597 回答
0

这对我有用。将您的文本视图放在滚动视图中,然后在滚动视图的子视图上执行TranslateAnimation,我的情况是 LinearLayout。我实际上是在这个线性布局中动态添加多个视图。

<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
于 2016-06-04T10:04:47.283 回答
0

真是郁闷啊,...不过答案很简单,用Edittext代替TextView,并用horizo​​ntalscrollview包裹起来,同时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>

感谢Hein,添加动画代码

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);
于 2020-08-21T10:50:24.747 回答
0

试试这个代码,它会帮助你解决 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>
于 2015-09-18T07:56:43.610 回答
0

使用@rajath答案的椭圆大小和选取框选项使用这种简单的方法

<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"/>
于 2018-05-18T13:13:27.937 回答
-1
<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" />

于 2015-09-18T08:05:51.177 回答