Android 實現跑馬燈效果
阿新 • • 發佈:2018-12-06
這裡用到了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); } });