SoFunction
Updated on 2025-03-08

Android custom marquee effect

The previous requirement was to use FlipperView to achieve up and down flip effect, but I found that the data was a bit long, which would cause three pieces of data to be put away on a screen. Later, it was changed to a marquee, but the marquee with only text TextView has it, but what should I do if I ask for a small icon behind the text?

(1).:

public class HomeFragment extends BaseFragment {
  private MarqueeScroll mMarqueeScroll;
  private int[] name_tv = {.name_tv1, .name_tv2, .name_tv3, .name_tv4, .name_tv5, .name_tv6};
  private TextView[] name_tvs = new TextView[6];
  private int[] name_iv = {.name_iv1, .name_iv2, .name_iv3, .name_iv4, .name_iv5, .name_iv6};
  private ImageView[] name_ivs = new ImageView[6];
  private ArrayList<HomeFlipperBean> mFlipperList = new ArrayList<HomeFlipperBean>();

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = (.fragment_homenews, null);
    mMarqueeScroll = (MarqueeScroll) ();
    for (int i = 0; i < name_tv.length; i++) {
      name_tvs[i] = (TextView) (name_tv[i]);
      name_ivs[i] = (ImageView) (name_iv[i]);
    }
    ();

    // Analysis here    ResponseBaseBean<ArrayList<HomeFlipperBean>> responsBaseBean = (mFlipperData,
        new TypeReference<ResponseBaseBean<ArrayList<HomeFlipperBean>>>() {
        });
    mFlipperList = ();

    if (mFlipperList != null && () > 0) {
      // ForegroundColorSpan is the foreground color of text, BackgroundColorSpan is the background color of text      ForegroundColorSpan span1 = new ForegroundColorSpan(0xFF535353);
      ForegroundColorSpan span2 = new ForegroundColorSpan(0xFF346699);

      for (int i = 0; i < name_tvs.length; i++) {
        HomeFlipperBean bean = (i);
        name_tvs[i].setText(() + " " + ());
        if ("up".equals(())) {
          name_ivs[i].setBackgroundResource(.in_img3);
        } else {
          name_ivs[i].setBackgroundResource(.in_img4);
        }
        int length = ().length();
        SpannableStringBuilder builder = new SpannableStringBuilder(name_tvs[i].getText().toString());
        (span1, 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        (span2, length + 1, name_tvs[i].getText().toString().length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        name_tvs[i].setText(builder);
      }
    }
  }
}

(2).fragment_homenews.xml: Layout file reference control

<
   android:
   android:layout_width="match_parent"
   android:layout_height="@dimen/kx_nonet_h" >

   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="#E1F4FF"
     android:orientation="horizontal" >

     <TextView
        android:
        style="@style/filpper_text_style" />

     <ImageView
        android:
        style="@style/filpper_image_style" />

     <TextView
        android:
        style="@style/filpper_text_style" />

     <ImageView
        android:
        style="@style/filpper_image_style" />

     <TextView
        android:
        style="@style/filpper_text_style" />

     <ImageView
        android:
        style="@style/filpper_image_style" />

     <TextView
        android:
        style="@style/filpper_text_style" />

     <ImageView
        android:
        style="@style/filpper_image_style" />

     <TextView
        android:
        style="@style/filpper_text_style" />

     <ImageView
        android:
        style="@style/filpper_image_style" />

     <TextView
        android:
        style="@style/filpper_text_style" />

     <ImageView
        android:
        style="@style/filpper_image_style" />
   </LinearLayout>
</>

(3).: Customize HorizontalScrollView

public class MarqueeScroll extends HorizontalScrollView implements Runnable {
  private View inner;
  private Bitmap bitmap = null;

  /**
    * Scroll step length
    */
  private int step = 1;
  private int x;
  private int width;
  private int pWidth;
  private int pHeight;

  public MarqueeScroll(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundColor(0xFFE1F4FF);
  }

  @Override
  protected void onFinishInflate() {

    if (getChildCount() == 1) {
      inner = getChildAt(0);
    }

  }

  @Override
  protected void onDetachedFromWindow() {

    ();
    (this);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    if (getWidth() == 0) {
       lp = getLayoutParams();
       = pWidth;
       = pHeight;
      setLayoutParams(lp);
    }
    if (bitmap == null &amp;&amp; inner != null) {
      width = ();
      bitmap = (width, (), Config.RGB_565);
      Canvas canvas1 = new Canvas(bitmap);
      (canvas1);
      pWidth = getWidth();
      pHeight = getHeight();
      if (inner != null) {
        removeViewInLayout(inner);
        inner = null;
      }

      run();
    }

    if (bitmap != null) {

      int nowX = x;
      nowX -= step;
      (bitmap, nowX, 0, null);

      if (nowX &lt; 0) {

        (bitmap, width + nowX /* + space */, 0, null);
      }
      if (nowX &lt;= -width) {
        nowX = 0;

      }
      x = nowX;
    }
    (canvas);
  }

  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

      (msg);
    }

  };

  @Override
  public void run() {

    invalidate();
    (this, 1);
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.