AndroidTextview跑马灯横向滚动

原理:自定义textview

源码:http://download.csdn.net/detail/lm_zp/9517135

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"
    tools:context=".MainActivity" >

    <com.bwie.androidtest.AutoText   
        android:id="@+id/autoTxt"    
        android:layout_width="match_parent"    
        android:layout_height="wrap_content"    
        android:background="#ffffff"    
        android:textColor="#00ff00"    
        android:textSize="35sp" />  


</RelativeLayout>
自定义textview

AutoText.java

package com.bwie.androidtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

public class AutoText extends TextView {

	private int width, height;
	private Paint paintText;
	private float posx, posy;
	private float speed = 0.0f;
	private String text = "hello haha";
	private float textWidth = 0;
	private float moveDistance = 0.0f;
	private boolean isStarting = false;

	public AutoText(Context context) {
		super(context);

	}

	public AutoText(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	private void initView() {
		paintText = new Paint();
		//字体大小
		paintText.setTextSize(50.0f);
		//字体颜色
		paintText.setColor(Color.BLACK);
		paintText.setTypeface(Typeface.DEFAULT_BOLD);
		paintText.setAntiAlias(true);
		text = getText().toString();
		textWidth = paintText.measureText(text);
		Log.e("msg", "textWidth= " + textWidth);
		this.speed = textWidth;
		moveDistance = textWidth * 2 + width;
	}

	public void initDisplayMetrics(WindowManager windowManager) {
		/* 取得屏幕分辨率大小 */
		DisplayMetrics dm = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(dm);
		this.width = dm.widthPixels;
		this.height = dm.heightPixels;

		initView();
		this.posx = width + textWidth;
		FontMetrics fm = paintText.getFontMetrics();
		float baseline = fm.descent - fm.ascent;
		//字体显示高度
		this.posy = height / 2 - baseline;
	}

	public void startScroll() {
		isStarting = true;
		invalidate();
	}

	public void stopScroll() {
		isStarting = false;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// super.onDraw(canvas);
		canvas.drawText(text, posx - speed, posy, paintText);
		if (!isStarting) {
			return;
		}
		speed += 2.0f;
		if (speed > moveDistance)
			speed = textWidth;
		invalidate();
	}

}

MainActivity.java

package com.bwie.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils.TruncateAt;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	String str11 = "促进青年教师全面发展,\n引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + "\n"
			+ "促进青年教师全面发展,\n引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + "\n"
			+ "  djsdnh   kshdfjks     \n\r\t ";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		scroll3();
	}

	void scroll3() {
		AutoText auto = (AutoText) findViewById(R.id.autoTxt);
		auto.setText(str11);
		//传递屏幕宽度
		auto.initDisplayMetrics(getWindowManager());
		//开始
		auto.startScroll();
	}

}

AndroidTextview跑马灯横向滚动_第1张图片

你可能感兴趣的:(AndroidTextview跑马灯横向滚动)