现在的位置: 首页 > 综合 > 正文

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

2017年10月12日 ⁄ 综合 ⁄ 共 11310字 ⁄ 字号 评论关闭

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

一、定义一个类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布局文件里进行如下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
   <org.caotao.lamp.AutoScrollTextView
     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"   />
   
   <RelativeLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
      
    <TextView
        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" />
   
    <SeekBar
        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" />

    <EditText
     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" />
    <Button
        android:id="@+id/contentSure"
        android:text="@string/sure"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/contentText"
        android:layout_below="@id/speedSeekBar" />
   
    <TextView
        android:id="@+id/setFontColor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"
        android:text="@string/setFondColor"
        android:layout_below="@id/contentText" />
   
    <Spinner
        android:id="@+id/fontColorSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:prompt="@string/fontcolor_prompt"
        android:layout_below="@id/setFontColor" />
   
    <TextView
        android:id="@+id/setBackgroundColor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"
        android:text="@string/setBackgroundColor"
        android:layout_below="@id/fontColorSpinner" />
   
    <Spinner
        android:id="@+id/backgroundColorSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:prompt="@string/backgroundColor_prompt"
        android:layout_below="@id/setBackgroundColor" />
   
      
   </RelativeLayout>

 

</LinearLayout>

 

三、主界面中的代码:

package org.caotao.lamp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 //声明一系列的布局控件
 private AutoScrollTextView autoScrollTextView = null;

 private TextView setSpeed = null;
 private SeekBar speedSeekBar = null;
 private Button contentBtn = null;
 private EditText contentText = null;
 private Spinner fontColorSpinner = null;
 private Spinner backgroundColorSpinner = null;

 public static float updateSpeed = 0f; //设置文字的滚动速度,数字越大,速度越快。
 public static String updateText = null;
 public static int updateFontColor = 0;
 public static int updateBackgroundColor = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //提示速度设置的TextView
        setSpeed = (TextView)findViewById(R.id.setSpeed);
        //设置速度的SeekBar进度条
        speedSeekBar = (SeekBar)findViewById(R.id.speedSeekBar);
        speedSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   
         public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
          updateSpeed = seekBar.getProgress();
          setSpeed.setText("速度" + updateSpeed);
   }
         
   public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
   }
   
   public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
   }
  });
       
        //根据ID获得设置显示文本的控件
        contentText = (EditText)findViewById(R.id.contentText);
        contentBtn = (Button)findViewById(R.id.contentSure);
        contentBtn.setOnClickListener(new contentBtnListener());
       
       
        //设置字体颜色的spinner
        fontColorSpinner = (Spinner)findViewById(R.id.fontColorSpinner);
        ArrayAdapter<CharSequence> fontColorAdapter = ArrayAdapter.createFromResource(
          this, R.array.fontcolor_array, android.R.layout.simple_spinner_item);
       
        fontColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fontColorSpinner.setAdapter(fontColorAdapter);
        fontColorSpinner.setOnItemSelectedListener(new fontColorListener());
       
        //设置背景颜色的spinner
        backgroundColorSpinner = (Spinner)findViewById(R.id.backgroundColorSpinner);
        ArrayAdapter<CharSequence> backgroundColorAdapter = ArrayAdapter.createFromResource(
          this, R.array.fontcolor_array, android.R.layout.simple_spinner_item);
       
        backgroundColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        backgroundColorSpinner.setAdapter(fontColorAdapter);
        backgroundColorSpinner.setOnItemSelectedListener(new backgroundColorListener());
       
        //根据ID获取自定义AutoScrollTextView类的对象
        autoScrollTextView = (AutoScrollTextView)findViewById(R.id.TextViewNotice);
        autoScrollTextView.init(getWindowManager());

        autoScrollTextView.startScroll();
       
    }
   
   
    class contentBtnListener implements OnClickListener{
     public void onClick(View v) {
      // TODO Auto-generated method stub
      updateText = contentText.getText().toString();
      autoScrollTextView.setText(updateText);
      autoScrollTextView.init(getWindowManager());
  }
    }
   
   
  //设置字体颜色的spinner的选项监听器
    class fontColorListener implements OnItemSelectedListener{
     
  public void onItemSelected(AdapterView<?> parent, View v, int pos,
    long id) {
   // TODO Auto-generated method stub
   System.out.println(id);
   switch((int)id) {
   case 0:
    updateFontColor = Color.BLACK;
    break;
   case 1:
    updateFontColor = Color.GREEN;
    break;
   case 2:
    updateFontColor = Color.RED;
    break;
   case 3:
    updateFontColor = Color.YELLOW;
    break;
   case 4:
    updateFontColor = Color.GRAY;
    break;
   case 5:
    updateFontColor = Color.MAGENTA;
    break;
   case 6:
    updateFontColor = Color.BLUE;
    break;
   default:break;
   }
   //重新初始化autoScrollTextView对象,更新画笔Paint的颜色
      autoScrollTextView.init(getWindowManager());
  }

  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub
  }
    }
   
    //设置背景颜色的spinner的选项监听器
    class backgroundColorListener implements OnItemSelectedListener{

  public void onItemSelected(AdapterView<?> parent, View v, int pos,
    long id) {
   // TODO Auto-generated method stub
   System.out.println(id);
   switch((int)id){
   case 0:
    updateBackgroundColor = Color.BLACK; break;
   case 1:
    updateBackgroundColor = Color.GREEN; break;
   case 2:
    updateBackgroundColor = Color.RED; break;
   case 3:
    updateBackgroundColor = Color.YELLOW; break;
   case 4:
    updateBackgroundColor = Color.GRAY; break;
   case 5:
    updateBackgroundColor = Color.MAGENTA; break;
   case 6:
    updateBackgroundColor = Color.BLUE; break;
   default:break;
   }
   //重新初始化autoScrollTextView对象,更新画笔Paint的颜色
   autoScrollTextView.setBackgroundColor(updateBackgroundColor);
      autoScrollTextView.init(getWindowManager());
  }

  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub
  }
    }
}

 

抱歉!评论已关闭.