1.自定义textview

public class ScrollTextView extends TextView {    // scrolling feature    private Scroller mSlr;    // milliseconds for a round of scrolling    private int mRndDuration = 100000;    // the X offset when paused    private int mXPaused = 0;    // whether it's being paused    private boolean mPaused = true;    /*    * constructor    */    public ScrollTextView(Context context) {        this(context, null);        // customize the TextView        setSingleLine();        setEllipsize(null);        setVisibility(INVISIBLE);    }    /*    * constructor    */    public ScrollTextView(Context context, AttributeSet attrs) {        this(context, attrs, android.R.attr.textViewStyle);        // customize the TextView        setSingleLine();        setEllipsize(null);        setVisibility(INVISIBLE);    }    /*    * constructor    */    public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        // customize the TextView        setSingleLine();        setEllipsize(null);        setVisibility(INVISIBLE);    }    /**     * begin to scroll the text from the original position     */    public void startScroll() {        // begin from the very right side        mXPaused = -1 * getWidth();        // assume it's paused        mPaused = true;        resumeScroll();    }    /**     * resume the scroll from the pausing point     */    public void resumeScroll() {        if (!mPaused)            return;        // Do not know why it would not scroll sometimes        // if setHorizontallyScrolling is called in constructor.        setHorizontallyScrolling(true);        // use LinearInterpolator for steady scrolling        mSlr = new Scroller(this.getContext(), new LinearInterpolator());        setScroller(mSlr);        int scrollingLen = calculateScrollingLen();        int distance = scrollingLen - (getWidth() + mXPaused);        int duration = (new Double(mRndDuration * distance * 1.00000                / scrollingLen)).intValue();        setVisibility(VISIBLE);        mSlr.startScroll(mXPaused, 0, distance, 0, duration);        invalidate();        mPaused = false;    }    /**     * calculate the scrolling length of the text in pixel     *     * @return the scrolling length in pixels     */    private int calculateScrollingLen() {        TextPaint tp = getPaint();        Rect rect = new Rect();        String strTxt = getText().toString();        tp.getTextBounds(strTxt, 0, strTxt.length(), rect);        int scrollingLen = rect.width() + getWidth();        rect = null;        return scrollingLen;    }    /**     * pause scrolling the text     */    public void pauseScroll() {        if (null == mSlr)            return;        if (mPaused)            return;        mPaused = true;        // abortAnimation sets the current X to be the final X,        // and sets isFinished to be true        // so current position shall be saved        mXPaused = mSlr.getCurrX();        mSlr.abortAnimation();    }    @Override     /*     * override the computeScroll to restart scrolling when finished so as that     * the text is scrolled forever     */    public void computeScroll() {        super.computeScroll();        if (null == mSlr) return;        if (mSlr.isFinished() && (!mPaused)) {            this.startScroll();        }    }    public int getRndDuration() {        return mRndDuration;    }    public void setRndDuration(int duration) {        this.mRndDuration = duration;    }    public boolean isPaused() {        return mPaused;    }}
2.布局文件
activity_main11
xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >            android:id="@+id/start"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="start"        android:text="走起" />            android:id="@+id/stop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="stop"        android:text="停止" />            android:id="@+id/startfor0"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="startFor0"        android:text="从头开始" />            android:id="@+id/test"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#339320"        android:singleLine="true"        android:textColor="#000000"        android:textSize="20dp" >    
3.mainActivity

public class MainActivity11 extends Activity implements View.OnClickListener {    private ScrollTextView test;    private Button start,stop,startfor0;    private List data;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main11);        test = (ScrollTextView) this.findViewById(R.id.test);        start = (Button) findViewById(R.id.start);        stop = (Button) findViewById(R.id.stop);        startfor0 = (Button) findViewById(R.id.startfor0);        start.setOnClickListener(this);        stop.setOnClickListener(this);        startfor0.setOnClickListener(this);        data = new ArrayList<>();        data.add("跑马灯1");        data.add("跑马灯2");        data.add("跑马灯3");        setAlwaysMarqueeTextViewData();    }    /**     * 设置最新咨询三条数据     */    private void setAlwaysMarqueeTextViewData(){        if(data!=null){//从集合里获取数据来显示在跑马灯上            test.setText(data.get(0)+";    "+data.get(1)+";    "+data.get(2));            test.startScroll();        }    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.start:                //开始                test.startScroll();                break;            case R.id.stop:                //停止                test.pauseScroll();                break;            case R.id.startfor0:                //从头开始                test.resumeScroll();                break;        }    }}

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. Android(安卓)recyclerview 支持网格布局的间隙平均分割
  3. listview使用ArrayAdapter显示文字
  4. Android(安卓)使用Room 生成不了数据库文件
  5. ch05 Android布局
  6. android.view.InflateException报错
  7. android listView性能优化
  8. (自定义)AlertDialog使用
  9. Android学习之SQLite数据库简单测试实例

随机推荐

  1. Android 桌面图标添加未读消息角标APP角
  2. android数据库 Android自带数据库SQLite
  3. Android SIP 网络通话
  4. Android Studio 添加 C、C++ 代码
  5. android启动过程及各个镜像间的关系
  6. android该系统的应用API选择演示版本
  7. Android真机调试不打印日志解决
  8. Android(安卓)8.0 Volte开关流程 HD图标
  9. Android(安卓)SDK无法更新
  10. Android 面试大全