作者:程序員小冰,GitHub主頁:https://github.com/QQ986945193
新浪微博:http://weibo.com/mcxiaobing
首先給大家看一下我們今天這個最終實現的效果圖:
這個主要是用到的自定義TextView,然後裏面開一個線程進行,進行滾動效果。
我這裏寫了兩個自定義TextView,分別是從左到右,以及從右到左。利用滾動
的偏移量,進行設置滾動的位置。然後利用一個變量,進行區分是否是滾動狀態。
我這裏給大家看一下代碼吧,只給大家看一下其中一個自定義View的。
package davidmarqueecontrolproject.qq986945193.davidmarqueecontrolproject;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* @author :程序員小冰
* @新浪微博 :http://weibo.com/mcxiaobing
* @GitHub: https://github.com/QQ986945193
* @CSDN博客: http://blog.csdn.net/qq_21376985
* @碼雲OsChina :http://git.oschina.net/MCXIAOBING
* <p/>
* 從左到右 滾動自定義TextView
*/
public class AutoText extends TextView implements Runnable {
private int currentScrollX;// 當前滾動的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
public AutoText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public AutoText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isMeasure) {// 文字寬度只需獲取一次就可以了
getTextWidth();
isMeasure = true;
}
}
/**
* 獲取文字寬度
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
// 重寫setText 在setText的時候重新計算text的寬度
@Override
public void setText(CharSequence text, BufferType type) {
// TODO Auto-generated method stub
super.setText(text, type);
this.isMeasure = false;
}
@Override
public void run() {
currentScrollX -= 2;// 滾動速度
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (getScrollX() <= -(this.getWidth())) {
scrollTo(textWidth, 0);
currentScrollX = textWidth;
// return;
}
postDelayed(this, 5);
}
// 開始滾動
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止滾動
public void stopScroll() {
isStop = true;
// textWidth=currentScrollX; //隨時停止
}
// 從頭開始滾動
public void startFor0() {
currentScrollX = 0;
startScroll();
}
}
佈局我就不寫了,直接引用這個view就行了。然後java類中看一下吧,其實
也是直接調用它的方法。
package davidmarqueecontrolproject.qq986945193.davidmarqueecontrolproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import davidmarqueecontrolproject.qq986945193.davidmarqueecontrolproject.MarqueeText;
import davidmarqueecontrolproject.qq986945193.davidmarqueecontrolproject.R;
/**
* @author :程序員小冰
* @新浪微博 :http://weibo.com/mcxiaobing
* @GitHub: https://github.com/QQ986945193
* @CSDN博客: http://blog.csdn.net/qq_21376985
* @碼雲OsChina :http://git.oschina.net/MCXIAOBING
*/
public class MainActivity extends Activity {
private MarqueeText test;
private AutoText auto_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (MarqueeText) findViewById(R.id.test);
auto_text = (AutoText) findViewById(R.id.auto_text);
}
public void start(View v) {
test.startScroll();
// auto_text.startScroll();
}
public void stop(View v) {
test.stopScroll();
// auto_text.stopScroll();
}
public void startFor0(View v) {
test.startFor0();
// auto_text.startFor0();
}
}
好了。到此結束吧,不懂得請留言或者私信。源代碼需要的可以去
(AndroidStudio版)github下載地址:
https://github.com/QQ986945193/DavidMarqueeControlProject