自定义TextView跑马灯效果实例教程(转)
Android自带的跑马灯效果不太好控制,不能控制速度,不能即时停止和启动,而且还受焦点的影响蛋疼不已。由于项目需求需要用的可控制性高的跑马灯效果,所以自己写了一个自定义的TextView,可控制启动,停止,和速度。
注意:在布局文件引用本view时,paddingLeft,paddingRigh都必须为0dp,需要增加这两个属性的,大家可以自行修改代码。
android:ellipsize="marquee" android:singleLine="true" 这两个属性也要加上。
效果图:
1 public class MarqueeText extends TextView implements Runnable { 2 private int currentScrollX;// 当前滚动的位置 3 private boolean isStop = false; 4 private int textWidth; 5 private boolean isMeasure = false; 6 7 public MarqueeText(Context context) { 8 super(context); 9 // TODO Auto-generated constructor stub 10 } 11 12 public MarqueeText(Context context, AttributeSet attrs) { 13 super(context, attrs); 14 } 15 16 public MarqueeText(Context context, AttributeSet attrs, int defStyle) { 17 super(context, attrs, defStyle); 18 } 19 20 @Override 21 protected void onDraw(Canvas canvas) { 22 // TODO Auto-generated method stub 23 super.onDraw(canvas); 24 if (!isMeasure) {// 文字宽度只需获取一次就可以了 25 getTextWidth(); 26 isMeasure = true; 27 } 28 } 29 30 /** 31 * 获取文字宽度 32 */ 33 private void getTextWidth() { 34 Paint paint = this.getPaint(); 35 String str = this.getText().toString(); 36 textWidth = (int) paint.measureText(str); 37 } 38 39 @Override 40 public void run() { 41 currentScrollX -= 2;// 滚动速度 42 scrollTo(currentScrollX, 0); 43 if (isStop) { 44 return; 45 } 46 if (getScrollX() <= -(this.getWidth())) { 47 scrollTo(textWidth, 0); 48 currentScrollX = textWidth; 49 // return; 50 } 51 postDelayed(this, 5); 52 } 53 54 // 开始滚动 55 public void startScroll() { 56 isStop = false; 57 this.removeCallbacks(this); 58 post(this); 59 } 60 61 // 停止滚动 62 public void stopScroll() { 63 isStop = true; 64 } 65 66 // 从头开始滚动 67 public void startFor0() { 68 currentScrollX = 0; 69 startScroll(); 70 } 71 }
//布局文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/start" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:onClick="start" 12 android:text="走起" /> 13 14 <Button 15 android:id="@+id/stop" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:onClick="stop" 19 android:text="停止" /> 20 21 <Button 22 android:id="@+id/startfor0" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:onClick="startFor0" 26 android:text="从头开始" /> 27 28 <simtice.demo.marqueetext.MarqueeText 29 android:id="@+id/test" 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:background="#339320" 33 android:ellipsize="marquee" 34 android:singleLine="true" 35 android:text="这才是真正的文字跑马灯效果这才是真正的字跑马灯效果这才是真正的" 36 android:textColor="#000000" 37 android:textSize="20dp" > 38 </simtice.demo.marqueetext.MarqueeText> 39 40 </LinearLayout>
MainActivity:
public class MainActivity extends Activity { private MarqueeText test; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test = (MarqueeText) this.findViewById(R.id.test); } public void start(View v) { test.startScroll(); } public void stop(View v) { test.stopScroll(); } public void startFor0(View v){ test.startFor0(); } }
正在学习Android Developing