Android开发之TextView文字水平滚动效果实现
2014-10-09 11:31
991 查看
在网络上搜集了很多关于这方面的文章和实现方法,最终经过实践验证和部分调整,完全可用,包括不依赖焦点和选中TextView便可实现水平滚动效果。
现将主要代码罗列如下,希望对看到的人有帮助。
第一步:编写MarqueeText.java类,继承自TextView
package cn.superyouth.www.itools;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;
public class MarqueeText extends TextView {
/** 是否停止滚动 */
private boolean mStopMarquee;
private String mText;
private float mCoordinateX;
private float mTextWidth;
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setText(String text) {
this.mText = text;
mTextWidth = getPaint().measureText(mText);
if (mHandler.hasMessages(0))
mHandler.removeMessages(0);
mHandler.sendEmptyMessageDelayed(0, 2000);
}
@SuppressLint("NewApi")
@Override
protected void onAttachedToWindow() {
mStopMarquee = false;
if (!(mText == null || mText.isEmpty()))
mHandler.sendEmptyMessageDelayed(0, 2000);
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
mStopMarquee = true;
if (mHandler.hasMessages(0))
mHandler.removeMessages(0);
super.onDetachedFromWindow();
}
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!(mText == null || mText.isEmpty()))
canvas.drawText(mText, mCoordinateX, 30, getPaint());
}
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if (Math.abs(mCoordinateX) > (mTextWidth + 5)) {
mCoordinateX = 0;
invalidate();
if (!mStopMarquee) {
sendEmptyMessageDelayed(0,500);
}
} else {
mCoordinateX -= 1;
invalidate();
if (!mStopMarquee) {
sendEmptyMessageDelayed(0, 30);
}
}
break;
}
super.handleMessage(msg);
}
};
}
第二步:编写布局文件main.xml
第三步:编写SYIT_Index.java继承自Activity类
现将主要代码罗列如下,希望对看到的人有帮助。
第一步:编写MarqueeText.java类,继承自TextView
package cn.superyouth.www.itools;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;
public class MarqueeText extends TextView {
/** 是否停止滚动 */
private boolean mStopMarquee;
private String mText;
private float mCoordinateX;
private float mTextWidth;
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setText(String text) {
this.mText = text;
mTextWidth = getPaint().measureText(mText);
if (mHandler.hasMessages(0))
mHandler.removeMessages(0);
mHandler.sendEmptyMessageDelayed(0, 2000);
}
@SuppressLint("NewApi")
@Override
protected void onAttachedToWindow() {
mStopMarquee = false;
if (!(mText == null || mText.isEmpty()))
mHandler.sendEmptyMessageDelayed(0, 2000);
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
mStopMarquee = true;
if (mHandler.hasMessages(0))
mHandler.removeMessages(0);
super.onDetachedFromWindow();
}
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!(mText == null || mText.isEmpty()))
canvas.drawText(mText, mCoordinateX, 30, getPaint());
}
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if (Math.abs(mCoordinateX) > (mTextWidth + 5)) {
mCoordinateX = 0;
invalidate();
if (!mStopMarquee) {
sendEmptyMessageDelayed(0,500);
}
} else {
mCoordinateX -= 1;
invalidate();
if (!mStopMarquee) {
sendEmptyMessageDelayed(0, 30);
}
}
break;
}
super.handleMessage(msg);
}
};
}
第二步:编写布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/backsmall" android:orientation="vertical" > <cn.superyouth.www.itools.MarqueeText android:id="@+id/textMsg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="115dp" android:textColor="@android:color/black" android:lines="1" android:focusable="true" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever" android:ellipsize="marquee" /> </LinearLayout>
第三步:编写SYIT_Index.java继承自Activity类
package cn.superyouth.www; import java.io.IOException; import cn.superyouth.www.itools.MarqueeText; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.GridView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class SYIT_Index extends Activity { MarqueeText autoScrollTextView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); autoScrollTextView = (MarqueeText) findViewById(R.id.textMsg); autoScrollTextView.setTextSize(30); autoScrollTextView.setTextColor(Color.BLUE); autoScrollTextView.setText("暂无任何预警信息!"); // 点击预警提示信息 autoScrollTextView.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // 进入预警信息页面 Intent intent = new Intent(SYIT_Index.this, SYIT_Warning.class); startActivity(intent); } }); } }
相关文章推荐
- android开发(46) 使用 textview实现文字的阴影效果,浮雕效果
- Android TextView实现水平滚动显示效果
- Android中TextView实现文字跑马灯效果(滚动轮播)
- Android开发之实现水平滚动效果—RecyclerView
- 一分钟实现 Android textview 跑马灯文字滚动效果
- Android textview和listview实现水平自动滚动的走马灯效果
- Android之TextView实现文字过长时省略部分或者滚动显示
- TextView 实现走马观灯 水平滚动效果
- android组件TextView实现字体水平滚动
- android中TextView中如何设置水平滚动效果
- Android重写TextView实现文字跑马灯效果
- Android之TextView实现文字过长时省略部分或者滚动显示
- Android开发 TextView实现跑马灯效果
- Android之TextView实现文字过长时省略部分或者滚动显示
- Android----Android之TextView实现文字过长时省略部分或者滚动显示 (跑马灯)
- Android之TextView实现文字过长时省略部分或者滚动显示
- How:android开发中实现TextView垂直滚屏效果的方法
- Android之TextView实现文字过长时省略部分或者滚动显示