跑马灯(速度、显示文本)

实现一个跑马灯效果的程序:

一、定义一个类AutoScrollTextView,该类继承TextView并实现OnClickListener接口(目的是通过点击跑马灯滚动中的文本,可以实现停止和恢复滚动的功能)。

package org.caotao.lamp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class AutoScrollTextView extends TextView implements OnClickListener {
    public final static String TAG = AutoScrollTextView.class.getSimpleName();
  
    private float textLength = 0f;//文本长度
    private float viewWidth = 0f;//AutoScrollTextView控件的长度
    private float step = 0f;//文字的横坐标
    private float y = 0f;//文字的纵坐标
    private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量
    private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
    public boolean isStarting = false;//是否开始滚动
    private Paint paint = null;//绘图样式
    private String text = "";//文本内容

  
    public AutoScrollTextView(Context context) {
        super(context);
        initView();
    }
    public AutoScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }
  
  
    private void initView(){
        setOnClickListener(this);
    }
  
  
    public void init(WindowManager windowManager){
        paint = getPaint();
        paint.setColor(MainActivity.updateFontColor);
       
        text = getText().toString();
        textLength = paint.measureText(text); //measure()方法获取text的长度
        viewWidth = getWidth();
        if(viewWidth == 0){
            if(windowManager != null){
                Display display = windowManager.getDefaultDisplay();
                viewWidth = display.getWidth();
            }
        }
        step = viewWidth + textLength;
        temp_view_plus_text_length = viewWidth + textLength;
        temp_view_plus_two_text_length = viewWidth + textLength * 2;
        System.out.println(temp_view_plus_text_length);
        y = getTextSize() + getPaddingTop();
    }

  
    public void startScroll(){
        isStarting = true;
        invalidate();
    }
  
  
    public void stopScroll(){
        isStarting = false;
        invalidate();
    }
  
    //覆写TextView的onDraw()方法,实现文本滚动显示的效果
    @Override
    public void onDraw(Canvas canvas) {
     /**
      * drawText()方法:
      * 参数1:要显示的文本
      * 参数2:文本显示的x坐标,TextView的最左端的x坐标是0,最右端的x坐标是TextView.getWidth();
      * 参数3:文本显示的y坐标,TextView的顶端所在的y坐标。
      * 参数4:描绘文本的画笔
      */
        canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
        if(!isStarting){
            return;
        }
       
        step += MainActivity.updateSpeed;//速度设置:1-10
       
        if(step > temp_view_plus_two_text_length){
            step = textLength;
        }
        invalidate();
    }
   
    /**
     * 响应用户点击事件:点击则停止,再点击继续开始滚动
     */
    public void onClick(View v) {
        if(isStarting){
            stopScroll();
        }
        else{
            startScroll();
        }
    }
}

 


二、在auto.xml布局文件里进行如下布局:


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
        android:id="@+id/TextViewNotice"
     android:layout_height="50dp"
     android:layout_width="fill_parent"
     android:layout_marginTop="10dp"
     android:text="@string/defaultview"
     android:textColor="#000"
     android:inputType="text"
     android:background="#555"
      android:textSize="30dp"   />
   
          android:layout_width="fill_parent"
       android:layout_height="fill_parent">
      
            android:id="@+id/setSpeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="@string/setspeed" />
   
            android:id="@+id/speedSeekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_below="@id/setSpeed"
        android:max="10"
        android:progress="1" />

         android:id="@+id/contentText"
     android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:hint="@string/text"
        android:layout_below="@id/speedSeekBar" />
   

你可能感兴趣的:(跑马灯(速度、显示文本))