Android 实现跑马灯效果

版权声明:未经博主同意切勿转载 https://blog.csdn.net/qq_32136827/article/details/84839863

这里用到了HorizontalScrollView嵌套TextView,上xml布局:

<HorizontalScrollView
        android:id="@+id/horiSv"
        android:layout_width="match_parent"
        android:layout_height="70px"
        android:background="@color/green"
        android:scrollbars="none">

        <TextView
            android:id="@+id/noticeTv"
            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:gravity="center"
            android:singleLine="true"
            android:text=""
            android:textColor="@color/white"
            android:textSize="36sp" />
    </HorizontalScrollView>

java代码中的实现:

private HorizontalScrollView mNoticeScrollView;
private TextView mNoticeTv;
private TranslateAnimation mRigthToLeftAnim;
private final static float SCOLL_V = 0.2f;



mNoticeScrollView = (HorizontalScrollView) findViewById(R.id.horiSv);
mNoticeTv = (TextView) findViewById(R.id.noticeTv);
mNoticeTv.setFocusable(true);




String value = "<font color='white' size='24'>" + "收费标准:4小时/元" + "</font>";
        mNoticeTv.setText(Html.fromHtml(value));
        mNoticeTv.post(new Runnable() {
            @Override
            public void run() {
                mRigthToLeftAnim = new TranslateAnimation(mNoticeScrollView.getWidth(), -mNoticeTv.getWidth(), 0, 0);
                mRigthToLeftAnim.setRepeatCount(Animation.INFINITE);
                mRigthToLeftAnim.setInterpolator(new LinearInterpolator());
                mRigthToLeftAnim.setDuration((long) ((mNoticeScrollView.getWidth() + mNoticeTv.getWidth()) / SCOLL_V));
                mNoticeTv.startAnimation(mRigthToLeftAnim);
            }
        });

猜你喜欢

转载自blog.csdn.net/qq_32136827/article/details/84839863