Android跑马灯两种方式
来源:互联网 发布:耳机音频测试软件 编辑:程序博客网 时间:2024/05/22 00:10
第一中方式: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积分");
0 0
- Android跑马灯两种方式
- Android跑马灯效果实现的三种方式
- Android三种方式实现TextView跑马灯效果
- Android跑马灯实现的三种方式
- Android三种方式实现TextView跑马灯效果
- Android TextView实现跑马灯效果的两种方式
- android开发TextView自动滚动,跑马灯显示方式
- Android 跑马灯效果
- android跑马灯效果
- android跑马灯
- android跑马灯效果
- android 跑马灯
- android跑马灯文字效果
- Android 跑马灯
- android跑马灯实现
- Android跑马灯效果
- android 跑马灯实现
- Android 跑马灯效果
- Android wifi无线调试App新玩法ADB WIFI
- 链接中的符号
- iOS 修改项目的桌面名称
- android 双service和双进程
- Table边框使用总结
- Android跑马灯两种方式
- 偏好设置 SharedPreferences
- 浅谈数论(三)水仙花数
- 对于linux下system()函数的深度理解(转载)
- xml-rpc c++ 测试代码
- linux系统中查看己设置iptables规则
- Android学QQ空间相册浏览类型横向滑动效果显示多图片MyHorizontalScrollView
- java 错误代码回馈到前台
- 一步一步写二叉查找树