android 可以控制速度的跑马灯
来源:互联网 发布:网络被屏蔽怎么破解 编辑:程序博客网 时间:2024/05/22 03:01
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
<LinearLayout 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" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="start" android:text="走起" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stop" android:text="停止" /> <Button android:id="@+id/startfor0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startFor0" android:text="从头开始" /> <com.qiyuan.jindongshangcheng.view.ScrollTextView 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" > </com.qiyuan.jindongshangcheng.view.ScrollTextView></LinearLayout>3.mainActivity
public class MainActivity11 extends Activity implements View.OnClickListener { private ScrollTextView test; private Button start,stop,startfor0; private List<String> 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; } }}
0 0
- android 可以控制速度的跑马灯
- 第二十三节:独立按键控制跑马灯的速度。
- 【Android】Android开发可以手动进行控制的跑马灯效果,包括从左到右,以及从右到左,
- 【Android】Android开发可以手动进行控制的跑马灯效果,包括从左到右,以及从右到左,
- 自定义TextView跑马灯效果可控制启动/停止/速度
- 自定义TextView跑马灯效果可控制启动/停止/速度
- 自定义跑马灯,可控制速度与方向
- 横向竖向各种速度的跑马灯
- android textview自带跑马灯的控制
- 单片机控制跑马灯
- Android TextView的跑马灯(不获取焦点情况下也可以跑)
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android:TextView不获取焦点可以实现跑马灯的效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- android的跑马灯效果
- Android 跑马灯的方法
- android的textView跑马灯
- Android中的加密方法
- Java I/O流图示
- node.js开发错误——TypeError: req.flash is not a function
- JAVA实现的支付宝扫描二维码支付
- DIY:利用单片机自制的RGB拖尾流水灯,含电路图、源代码、演示视频、效果图
- android 可以控制速度的跑马灯
- 关于MATLAB循环打开2进制文件
- 常用排序算法的python实现和性能分析
- 域账号被锁定
- 加载更多功能的实现
- ROC和AUC介绍以及如何计算AUC
- 有缓冲文件操作的几个函数
- 2.1安装
- 关于编程严谨性-----------处理业务前状态判断