How can the running horse lamp effect be achieved on Android?

One way to achieve the marquee effect in Android is by using the TextView widget and combining it with property animations or a Handler to create the scrolling text effect. Here is a simple implementation method:

  1. Add a TextView widget in the layout file and set its attribute to be scrollable.
<TextView
    android:id="@+id/marqueeTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:text="This is a marquee text view"
    android:textColor="@android:color/black"
    android:textSize="20sp" />
  1. In Java code, locate the TextView widget and apply a scrolling effect to it.
TextView marqueeTextView = findViewById(R.id.marqueeTextView);
marqueeTextView.setSelected(true);
  1. If you need to control scrolling speed or stop scrolling, you can achieve this by using property animations or a Handler.
// 使用属性动画控制滚动速度
ObjectAnimator animator = ObjectAnimator.ofFloat(marqueeTextView, "translationX", -100f, 1000f);
animator.setDuration(5000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start();

// 使用Handler控制滚动停止
Handler handler = new Handler();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        marqueeTextView.clearAnimation();
    }
};
handler.postDelayed(runnable, 10000); // 10秒后停止滚动

By following the above steps, you can achieve a simple marquee effect for a TextView. You can also customize and optimize it further based on your specific needs.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds