跑马灯实现的三种方式
来源:互联网 发布:怎么做seo推广 编辑:程序博客网 时间:2024/05/13 23:28
实现跑马灯的三种方式:分别是直接用控件、自定义一个HorizontalScrollView和自定义一个TextView。
一、直接使用控件:
优点:使用简单
缺点:可扩展性差
使用:直接在XML文件中添加如下代码即可:
<TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:text="春花秋月何时了,往事知多少。问君能有几多愁,恰似一江春水向东流。" android:textColor="#0000ff" android:textSize="80sp" />
二、自定义一个HorizontalScrollView
利用水平滑动的ScrollView来实现。
使用:首先自定义一个HorizontalScrollView控件,然后在XML文件中使用即可(注意在XML中使用自定义控件需要控件的全包名)。
1.自定义的控件代码如下:
public class HorizontalScorllTextView extends HorizontalScrollView implements Runnable{int currentScrollX = 0;// 当前滚动的位置TextView tv;public HorizontalScorllTextView(Context context) {super(context);initView(context);}public HorizontalScorllTextView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}public HorizontalScorllTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}void initView(Context context){View v = LayoutInflater.from(context).inflate(R.layout.scroll_layout, null);tv = (TextView)v.findViewById(R.id.tv_video_name);this.addView(v);}public void setText(String text){tv.setText(text);startScroll();}private void startScroll(){this.removeCallbacks(this); post(this);}@Overridepublic void run() {currentScrollX ++;// 滚动速度 scrollTo(currentScrollX, 0); if (currentScrollX >= tv.getWidth()) { scrollTo(0, 0); currentScrollX = 0; } postDelayed(this, 50);}}2.在XML中使用时如下:
<com.example.marqueedemo.utils.MarqueeTextView android:id="@+id/marqueeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:text="" android:textColor="#0f0" android:textSize="80dp" />
使用:首先自定义一个TextView控件,然后在XML文件中使用即可(注意在XML中使用自定义控件需要控件的全包名)。
1.自定义的控件代码如下:
public class MarqueeTextViewNew extends TextView implements Runnable { private int screenWidth = 80; private int currentScrollX = -screenWidth;// 当前滚动的位置 private boolean isStop = false; private int textWidth; private boolean isMeasure = false; public MarqueeTextViewNew(Context context) { super(context); // TODO Auto-generated constructor stub } public MarqueeTextViewNew(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeTextViewNew(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (!isMeasure) {// 文字宽度只需获取一次就可以了 getTextWidth(); isMeasure = true; } } /** * 获取文字宽度 */ private void getTextWidth() { Paint paint = this.getPaint(); String str = this.getText().toString(); textWidth = (int) paint.measureText(str); } @Override public void run() {// currentScrollX -= 1;// 滚动速度,方向:从左向右 currentScrollX += 1;// 滚动速度 方向:从右向左 scrollTo(currentScrollX, 0); if (isStop) { return; } if (currentScrollX > textWidth) { currentScrollX = -screenWidth; }// if (getScrollX() <= -(this.getWidth())) {// scrollTo(textWidth, 0);// currentScrollX = textWidth;// } postDelayed(this, 5); } // 开始滚动 public void startScroll() { isStop = false; this.removeCallbacks(this); post(this); } // 停止滚动 public void stopScroll() { isStop = true; } // 从头开始滚动 public void startFor0() { currentScrollX = 0; startScroll(); } @Override public void setText(CharSequence text, BufferType type) { // TODO Auto-generated method stub super.setText(text, type); startScroll(); } @Override public void destroyDrawingCache() { // TODO Auto-generated method stub super.destroyDrawingCache(); }}
2.在XML中的使用如下:
<com.example.marqueedemo.utils.MarqueeTextViewNew android:id="@+id/marqueeTextViewNew" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="80sp" android:singleLine="true" android:ellipsize="marquee" android:textColor="@android:color/holo_red_light" android:text="春花秋月何时了,往事知多少。问君能有几多愁,恰似一江春水向东流。" android:layout_centerHorizontal="true" />
粗略整理,不足之处还望大家指正~~~
0 0
- 跑马灯实现的三种方式
- Android跑马灯效果实现的三种方式
- Android跑马灯实现的三种方式
- 跑马灯效果实现方式三种
- Android三种方式实现TextView跑马灯效果
- Android三种方式实现TextView跑马灯效果
- TextView常用的几种方式:跑马灯(三)
- 跑马灯(字幕水平滚动)的两种实现方式
- (二)跑马灯的两种实现方式
- Android TextView实现跑马灯效果的两种方式
- 跑马灯的实现
- 跑马灯的实现
- Android 跑马灯效果实现的两种方式,解决和viewpager的冲突问题
- 安卓开发文字跑马灯效果的两种实现方式
- Android学习笔记(实现跑马灯效果的两种方式 )
- 跑马灯效果的两种实现
- TextView跑马灯的两种实现
- TextView跑马灯的实现
- webkit webApp 开发技术要点总结
- Oracle:Authid Current_User的使用(2)
- Redis复制与可扩展集群搭建
- python 命令行如何查看与更换当前路径?
- Logstash中grok filter example例子
- 跑马灯实现的三种方式
- 数据库事务的四大特性以及事务的隔离级别
- Git常用命令总结
- redis持久化,主从及数据备份
- JDBC中的Statement和PreparedStatement的区别
- LeetCode-104. Maximum Depth of Binary Tree
- Java 计算两个日期相差年数字、月数、天数及时分秒
- 开通博客啦!我的第一个博客
- poj 1753 Flip Game (状压DP)@