使用drawText方法不斷重繪文字。xml布局:<com.ycq.myview.MarqueeText android:id="@+id/test" android:layout_width="match_parent" android:layout_height="50dp" android:background="#339320" android:maxLines="1" android:textColor="#FF00FF" android:textSize="21sp"/>注意:需要設(shè)置為1行。可在XML中設(shè)置顏色,字號,不要設(shè)置文字內(nèi)容。Activity調(diào)用:test = (MarqueeText) this.findViewById(R.id.test);test.setMyContext("11111112225555");test.setL2r(true);test.setMySpeed(10);test.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { start(); }});setL2r設(shè)置方向,默認為從左向右;setMySpeed設(shè)置速度,默認為5,說明:若速度設(shè)置<0,則默認為1,若>15,則默認15.@OverridePRotected void onPause() { super.onPause(); stop();}@Overrideprotected void onResume() { super.onResume(); start();}@Overrideprotected void onDestroy() { super.onDestroy(); stop();}public void start() { test.startScroll();}public void stop() { test.stopScroll();}銷毀Activity,則不在執(zhí)行。具體實現(xiàn):package com.ycq.myview;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.TextView;/** * Created by yi on 2017/2/6. */public class MarqueeText extends TextView implements Runnable { private int currentScrollX = 0;// 當(dāng)前滾動的位置 private boolean isStop = false; private int textWidth; private boolean isMeasure = false; private String myContext = ""; private int vWidth; private int mySpeed = 5; private Boolean l2r = true; //getPaint()獲取系統(tǒng)畫筆 public MarqueeText(Context context) { super(context); } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isMeasure) {// 文字寬度只需獲取一次就可以了 textWidth = (int) getPaint().measureText(myContext); vWidth = getWidth(); isMeasure = true; } float baseline = getHeight() / 2 + getPaint().getTextSize() / 2 - getPaint().getFontMetrics().descent; canvas.drawText(myContext, currentScrollX, baseline, getPaint()); } @Override public void run() { if (!l2r) {//向左運動 currentScrollX -= mySpeed;// 滾動速度 if (currentScrollX < 0) { if (Math.abs(currentScrollX) >= textWidth) { currentScrollX = vWidth; } } } if (l2r) {//由左向右運動 currentScrollX += mySpeed;// 滾動速度 if (currentScrollX >= vWidth) { currentScrollX = -textWidth; } } invalidate(); postDelayed(this, 5); if (isStop) { return; } } // 開始滾動 public void startScroll() { isStop = false; this.removeCallbacks(this); post(this); } // 停止?jié)L動 public void stopScroll() { isStop = true; } public String getMyContext() { return myContext; } public void setMyContext(String myContext) { this.myContext = myContext; textWidth = (int) getPaint().measureText(myContext); } public int getMySpeed() { return mySpeed; } public void setMySpeed(int mySpeed) { this.mySpeed = mySpeed; if (mySpeed <= 0) { this.mySpeed = 1; } if (mySpeed >= 15) { this.mySpeed = 15; } } public Boolean getL2r() { return l2r; } public void setL2r(Boolean l2r) { this.l2r = l2r; }}float baseline = getHeight() / 2 + getPaint().getTextSize() / 2 - getPaint().getFontMetrics().descent; canvas.drawText(myContext, currentScrollX, baseline, getPaint());保證繪制的文字在控件的豎直中線位置。
新聞熱點
疑難解答