Android跑马灯两种方式
2016-07-13 14:25
399 查看
第一中方式:TextView实现
第二种实现效果,自定义控件
<TextView android:id="@+id/text_sign_message" android:layout_width="match_parent" android:layout_height="31dp" android:background="@color/sign_notice" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:gravity="center_vertical" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:text="签到奖励规则变更通知:连续第1、2、3、4、5、6、7....." android:textColor="@color/sign_notice_text" android:textSize="@dimen/normal_13"/>
android:focusable="true" android:focusableInTouchMode="true"获取焦点,没有焦点跑马灯效果不能实现
<pre name="code" class="html">android:marqueeRepeatLimit="marquee_forever"无限循环
第二种实现效果,自定义控件
public class MarqueeTextView extends TextView{ /** * 是否停止滚动 */ private boolean mStopMarquee; private String mText;//文本内容 private float mTextWidth;//文本宽度 private int speed = 1;//滚动速度 private float mCoordinateX = 0;//当前滚动位置 public MarqueeTextView(Context context) { super(context); } public MarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public int getSpeed() { return speed; } /** * 设置速度 * * @return */ public void setSpeed(int speed) { this.speed = speed; } public void setText(String text) { this.mText = text; mTextWidth = getPaint().measureText(mText); if (mHandler.hasMessages(0)) mHandler.removeMessages(0); mHandler.sendEmptyMessageDelayed(0, 0); } @Override protected void onAttachedToWindow() { mStopMarquee = false; if (!isEmpty(mText)) mHandler.sendEmptyMessageDelayed(0, 0); super.onAttachedToWindow(); } @Override protected void onDetachedFromWindow() { mStopMarquee = true; if (mHandler.hasMessages(0)) mHandler.removeMessages(0); super.onDetachedFromWindow(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isEmpty(mText)) canvas.drawText(mText, mCoordinateX, dp2px(20), getPaint()); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: if (mCoordinateX < (-mTextWidth)) {//文字滚动完了,从滚动区域的右边出来 mCoordinateX = getWidth(); invalidate(); if (!mStopMarquee) { sendEmptyMessageDelayed(0, 30); } } else { mCoordinateX -= speed; invalidate(); if (!mStopMarquee) { sendEmptyMessageDelayed(0, 30); } } break; } super.handleMessage(msg); } }; public static boolean isEmpty(String str) { return str == null || str.length() == 0; } public int dp2px(int dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, this.getResources().getDisplayMetrics()); } }使用
<MarqueeTextView android:id="@+id/text_sign_message" android:layout_width="match_parent" android:layout_height="31dp" android:background="@color/sign_notice" android:gravity="center_vertical" android:singleLine="true" android:textColor="@color/sign_notice_text" android:textSize="@dimen/normal_13"/>
mSignMessage_text = (MarqueeTextView) mSignIntehralHead.findViewById(R.id.text_sign_message); mSignMessage_text.setText("签到奖励规则变更通知:连续第1、2、3、4、5、6、7天签到,分别送20、25、30、35、40、45、50积分,连续签到超过7天后,每天固定送50积分");
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories