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

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

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

自定义view HorizontalScrollTextView


public class HorizontalScrollTextView extends TextView implements View.OnClickListener { 
  private float textLength = 0f;// 文本长度
  private float viewWidth = 0f;//文本控件的长度
  private float step = 0f;// 文本的横坐标
  private float y = 0f;// 文本的纵坐标
  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 == 0) {
     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;// 2.0为文字的滚动速度
       //判断是否滚动结束
       if (step > textLength){
           step = 0;
           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", "我跑完拉!");
              }
 });   

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值