網上的TextView做跑馬燈,大多都是要用到焦點,而且字數要超出滾動區域寬度才能實現滾動,使用起來十分不方便。
這里實現一種真正可控的滾動
(1)不需要焦點
(2)任意字數
(3)滾動從滾動區域右邊出來,在左邊消失,再從右邊出來。
上代碼
1、布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="改變滾動內容1"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/btn1" android:text="改變滾動內容2"/> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/btn2" android:text="退出程序"/> <com.using.margindemo.MarqueeTextView android:id="@+id/tvScroll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40dp" > </com.using.margindemo.MarqueeTextView> </RelativeLayout>
2、滾動控件
package com.using.margindemo; 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 MarqueeTextView extends TextView { /** 是否停止滾動 */ private boolean mStopMarquee; private String mText;//文本內容 private float mCoordinateX = 1280;//當前滾動位置 private float mTextWidth;//文本寬度 private int mScrollWidth = 1280;//滾動區域寬度 private int speed = 1;//滾動速度 public float getCurrentPosition() { return mCoordinateX; } public void setCurrentPosition(float mCoordinateX) { this.mCoordinateX = mCoordinateX; } public int getScrollWidth() { return mScrollWidth; } public void setScrollWidth(int mScrollWidth) { this.mScrollWidth = mScrollWidth; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public MarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public void setText(String text) { this.mText = text; mTextWidth = getPaint().measureText(mText); //mTextWidth = 1280; if (mHandler.hasMessages(0)) mHandler.removeMessages(0); mHandler.sendEmptyMessageDelayed(0, 10); } @Override protected void onAttachedToWindow() { mStopMarquee = false; if (!isEmpty(mText)) mHandler.sendEmptyMessageDelayed(0, 2000); super.onAttachedToWindow(); } public static boolean isEmpty(String str) { return str == null || str.length() == 0; } @Override protected void onDetachedFromWindow() { mStopMarquee = true; if (mHandler.hasMessages(0)) mHandler.removeMessages(0); super.onDetachedFromWindow(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isEmpty(mText)) canvas.drawText(mText, mCoordinateX, 150, getPaint()); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: if (mCoordinateX < (-mTextWidth)) {//文字滾動完了,從滾動區域的右邊出來 mCoordinateX = mScrollWidth; invalidate(); if (!mStopMarquee) { sendEmptyMessageDelayed(0, 500); } } else { mCoordinateX -= speed; invalidate(); if (!mStopMarquee) { sendEmptyMessageDelayed(0, 30); } } break; } super.handleMessage(msg); } }; }
3、
package com.using.margindemo; import android.app.ActionBar; import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.MarginLayoutParams; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; public class MainActivity extends Activity { private ImageView image; private MarqueeTextView marqueeTextView; private Button button1; private Button button2; private Button button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題欄 setContentView(R.layout.activity_main); marqueeTextView = (MarqueeTextView)findViewById(R.id.tvScroll); MarginLayoutParams margin1 = new MarginLayoutParams( marqueeTextView.getLayoutParams()); margin1.setMargins(100, 200, 0, 0);//設置滾動區域位置:在左邊距400像素,頂邊距10像素的位置 RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(margin1); layoutParams1.height = 240;//設滾動區域高度 layoutParams1.width = 1000; //設置滾動區域寬度 marqueeTextView.setLayoutParams(layoutParams1); marqueeTextView.setScrollWidth(1000); marqueeTextView.setCurrentPosition(100 + 1000);//設置滾動信息從滾動區域的右邊出來 marqueeTextView.setSpeed(2); marqueeTextView.setText("這才是真正的跑馬燈效果!"); button1 = (Button)findViewById(R.id.btn1); button2 = (Button)findViewById(R.id.btn2); button3 = (Button)findViewById(R.id.btn3); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { marqueeTextView.setText("這才是真正的跑馬燈效果!"); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { marqueeTextView.setText("溫馨提示:掛號或繳費后,請到休息區等候,我們會盡快為您服務,請留意屏幕叫號信息,謝謝!"); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
