自定义TextView实现跑马灯效果

mainActivity如下:

package c.x;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
    private Button mStartbButton;
    private Button mPauseButton;
    private Button mRestartbButton;
    private MarqueeText mMarqueeText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
   private void init(){
	   mMarqueeText=(MarqueeText) findViewById(R.id.marqueeText);
	   mStartbButton=(Button) findViewById(R.id.start_Button);
	   mStartbButton.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
       		mMarqueeText.startScroll();	
		}
	   });
	   mPauseButton=(Button) findViewById(R.id.pause_Button);
	   mPauseButton.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			mMarqueeText.pauseScroll();
			
		}
	   });
	  mRestartbButton=(Button) findViewById(R.id.restart_Button);
	  mRestartbButton.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
			mMarqueeText.restartScroll();
		}
	  });
   }
  
}


MarqueeText如下:

package c.x;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
/**
 * 需求描述:
 * TextView跑马灯的实现
 * 
 * 实现原理:
 * 通过不断的scrollTo()显示View中不同坐标处的内容
 * 
 *遇到的问题
 *1 注意scrollTo(int x, int y)
 *  移动View中内容,比如此处TextView中的文字.
 *  x the x position to scroll to
 *  y the y position to scroll to
 *  
 *2 scrollTo(int x, int y)的坐标问题
 *  参考资料:
 *  http://www.open-open.com/lib/view/open1328834050046.html
 *  个人理解:
 *  该坐标为View中的内容的坐标.该坐标的起始点虽然在内容的左上角
 *  但是Y轴的正向是竖直向上的,X轴水平向右.
 *  该理解待于进一步验证.
 *  
 */
public class MarqueeText extends TextView implements Runnable {
	private int contentWidth=0;
	private int scrollToX=0;
	private boolean isStop=false;
	private boolean isRun=true;
	private boolean isMeasureContentWidth=false;
	public MarqueeText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (!isMeasureContentWidth) {
			//获取文字长度
			Paint paint=this.getPaint();
			String content=this.getText().toString();
			contentWidth=(int) paint.measureText(content);
			isMeasureContentWidth=true;
		}
	}
	public void run() {
		if (isRun) {
			if (scrollToX>=contentWidth) {
				//重新开始
				scrollToX=-150;
			}
	        scrollTo(scrollToX, 0);
	        scrollToX=scrollToX+5;
	        postDelayed(this, 150);
		}
		
	}

	// 点击开始,执行线程
	public void startScroll() {
		post(this);
	}

	// 点击暂停
	public void pauseScroll() {
		isRun=false;
	}

	// 点击重新开始
	public void restartScroll() {
		isRun=true;
		scrollToX=0;
		startScroll();
	}

}


main.xml如下:

<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" >

    <LinearLayout
        android:id="@+id/linerLayout"
        android:layout_width="match_parent"
        android:layout_height="80dip"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/start_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="开始" />

            <Button
                android:id="@+id/pause_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="暂停" />

            <Button
                android:id="@+id/restart_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="重新开始" />
        </RelativeLayout>
    </LinearLayout>
    <c.x.MarqueeText
        android:id="@+id/marqueeText"
        android:layout_width="200dip"
        android:layout_height="100dip"
        android:layout_below="@id/linerLayout"
        android:text="波什29分4篮板2抢断4盖帽,雷-阿伦15分"
        android:textColor="@android:color/black"
        android:background="@android:color/white"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:gravity="center_vertical"
    />
</RelativeLayout>


 

上一篇:一次设计高可用网站的实战经验


下一篇:第六章:nginx实现动静分离