天天看点

android 自定义横向文字跑马灯控件

。# android 自定义横向文字跑马灯控件

android TextView 控件可以直接设置文字横向滚动效果,类似跑马灯效果。但原生效果局限相比较大,比如无法控制滚动速度,无法监听滚动结束时的状态,无法切换暂停/开启等。由于项目需要就定义了一个,可以实现上述功能,如果需要其他功能也很方便做扩展。内容很简单直接上代码:

自定义view HorizontalScrollTextView

public class HorizontalScrollTextView extends TextView implements View.OnClickListener { 
  private float textLength = f;// 文本长度
  private float viewWidth = f;//文本控件的长度
  private float step = f;// 文本的横坐标
  private float y = f;// 文本的纵坐标
  public boolean isStarting = false;// 是否开始滚动
  private Paint paint = null;
  private String text = "";// 文本内容
  private OnScrollCompleteListener onScrollCompleteListener;//滚动结束监听

  public HorizontalScrollTextView(Context context) {
     super(context);
     initView();
  }

  public HorizontalScrollTextView(Context context, AttributeSet attrs) {
       super(context, attrs);
       initView();
    }

  public HorizontalScrollTextView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       initView();
  }

 public OnScrollCompleteListener getOnSrollCompleteListener() {
       return onScrollCompleteListener;
 } 

 public void setOnScrollCompleteListener(OnScrollCompleteListener  onScrollCompleteListener){
      this.onScrollCompleteListener = onScrollCompleteListener;
}

 private void initView() {
        setOnClickListener(this);
 }

 public void init(WindowManager windowManager) {
   paint = getPaint();
   //设置滚动字体颜色
   paint.setColor(Color.parseColor("#f41301"));
   text = getText().toString();
   textLength = paint.measureText(text);
   viewWidth = getWidth();
   if (viewWidth == ) {
     if (windowManager != null) {
        Display display = windowManager.getDefaultDisplay();
        viewWidth = display.getWidth();
       }
      }
        y = getTextSize() + getPaddingTop();
    }
    //开启滚动
  public void startScroll() {
     isStarting = true;
     invalidate();
  }
  //停止滚动
  public void stopScroll() {
     isStarting = false;
     invalidate();
  }

  @Override
  public void onDraw(Canvas canvas) {
       canvas.drawText(text,  - step, y, paint);
       if (!isStarting) {
           return;
        }
       step += ;// 2.0为文字的滚动速度
       //判断是否滚动结束
       if (step > textLength){
           step = ;
           onScrollCompleteListener.onScrollComplete();
       }
         invalidate();
   }

 //控制点击停止或者继续运行
  @Override
  public void onClick(View v) {
          if (isStarting)
            stopScroll();
          else
            startScroll();
   }

   public interface OnScrollCompleteListener {
         void onScrollComplete();
   }
}
           

然后直接在xml文件里引用,最后在代码中就可以控制使用了

horizontalScrollTextView.setText(“这是一段可以横向滚动的广告文本);
  horizontalScrollTextView.init(getActivity().getWindowManager());
  horizontalScrollTextView.startScroll();
           

设置结束监听

horizontalScrollTextView.setOnScrollCompleteListener(newHorizontalScrollTextView.OnScollCompleteListener(
        {
               @Override
               public void onScrollComplete() {
                  Log.e("zsw", "我跑完拉!");
              }
 });   
           

第一次写博客,功能很简单,不足之处多多包涵。