资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android中怎么通过自定义View实现跑马灯效果

这篇文章给大家介绍android中怎么通过自定义View实现跑马灯效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

成都创新互联是一家专业提供木兰企业网站建设,专注与网站设计、网站建设、H5网站设计、小程序制作等业务。10年已为木兰众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

实现步骤:

1、自定义view的class:

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class MarqueenView extends View {

 /*
  * 1、自定义View的属性 2、在View的构造方法中获得我们自定义的属性 [ 3、重写onMesure ] 4、重写onDraw
  */

 /*
  * 文本
  */
 private String mTitleText;

 /**
  * 文本的颜色
  */
 private int mTitleTextColor;

 /**
  * 文本的大小
  */
 private int mTitleTextSize;

 /**
  * 绘制时控制文本绘制的范围
  */
 private Rect mBound, usualBound;

 // 画笔
 private Paint mPaint;

 private int spead = 15;

 private int length = 3;

 private int currentLength;

 @SuppressLint("HandlerLeak")
 private Handler handler = new Handler() {
  @Override
  public void handleMessage(android.os.Message msg) {
   if (mBound.width() <= getWidth()) {
    if (currentLength >= 120 + (getWidth() / length) * length) {
     currentLength = length;
    } else {
     currentLength = currentLength + length;
    }
   } else {
    if (currentLength >= (mBound.width() + 120)) {
     currentLength = length;
    } else {
     currentLength = currentLength + length;
    }
   }

   invalidate();
   handler.sendEmptyMessageDelayed(0, spead);

  };
 };

 public MarqueenView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
  currentLength = length;
 }

 public MarqueenView(Context context) {
  this(context, null);
 }

 public MarqueenView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);

  TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);

  int n = a.getIndexCount();
  for (int i = 0; i < n; i++) {
   int attr = a.getIndex(i);

   switch (attr) {
   case R.styleable.CustomTitleView_titleText:
    mTitleText = a.getString(attr);
    break;
   case R.styleable.CustomTitleView_titleTextColor:
    mTitleTextColor = a.getColor(attr, Color.BLACK);
    break;
   case R.styleable.CustomTitleView_titleTextSize:
    mTitleTextSize = a.getDimensionPixelSize(attr,
      (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
    break;
   }

  }
  a.recycle();

  mPaint = new Paint();
  mPaint.setTextSize(mTitleTextSize);

  mBound = new Rect();
  usualBound = new Rect();
  mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
  mPaint.getTextBounds("1234567890QqYy你好", 0, 16, usualBound);
 }


 /*
  * EXACTLY:一般是设置了明确的值或者是MATCH_PARENT AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
  * UNSPECIFIED:表示子布局想要多大就多大,很少使用
  */

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int widthSize = MeasureSpec.getSize(widthMeasureSpec);

  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  int width;
  int height;
  if (widthMode == MeasureSpec.EXACTLY) {
   width = widthSize;
  } else {
   mPaint.setTextSize(mTitleTextSize);
   mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
   float textWidth = mBound.width();// 字体宽度

   // 控件padding
   int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
   width = desired;
  }

  if (heightMode == MeasureSpec.EXACTLY) {
   height = heightSize;
  } else {
   mPaint.setTextSize(mTitleTextSize);
   mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
   float textHeight = mBound.height();
   int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
   height = desired;
  }
  // setMeasuredDimension((width / length) * length, height);
  setMeasuredDimension(width, height + usualBound.bottom);
 }

 @Override
 protected void onDraw(Canvas canvas) {


  mPaint.setColor(mTitleTextColor);
  if (mBound.width() <= getWidth()) {


   if (currentLength < mBound.width()) {
    canvas.drawText(mTitleText, getPaddingLeft() - currentLength, getHeight() - usualBound.bottom, mPaint);
   }

   if (currentLength >= 120) {
    canvas.drawText(mTitleText, getWidth() - currentLength + 120, getHeight() - usualBound.bottom, mPaint);
   }

  } else {


   if (currentLength < mBound.width()) {
    canvas.drawText(mTitleText, getPaddingLeft() - currentLength, getHeight() - usualBound.bottom, mPaint);
   }

   if (currentLength >= mBound.width() - getWidth() + 120) {
    canvas.drawText(mTitleText, 120 - currentLength + mBound.width(), getHeight() - usualBound.bottom, mPaint);
   }

  }

 }

 public static int getFontHeight(Integer textSize) {
  Paint paint = new Paint();
  if (textSize != null) {
   paint.setTextSize(textSize);
  }
  FontMetrics fm = paint.getFontMetrics();
  return (int) (fm.descent - fm.ascent);
 }

 public void startScroll() {
  handler.removeMessages(0);
  handler.sendEmptyMessage(0);
 }

 public void stopScroll() {
  handler.removeMessages(0);
  currentLength = 0;
  invalidate();
 }

 public void setScrollLength(int length) {
  this.length = length;
  currentLength = length;
 }

 public void setSpead(int spead) {
  this.spead = spead;
 }

 public void setText(String msg) {
  mTitleText = msg;
  mBound = new Rect();
  mPaint.getTextBounds(msg, 0, mTitleText.length(), mBound);
 }


}

2、在values的attrs.xml文件中需要加入自定义的属性,字体大小、颜色、和初始内容,这里有待改进




 
 
 

 
  
  
  
 

3、在layout的布局文件中引用此控件




 
 

4、上面准备好后,就可以在activity中设置跑马灯并开始滚动了

mv = (MarqueenView) findViewById(R.id.main_marquee_view);
mv.stopScroll();
mv.setText("滚动内容");// 设置显示内容
//mv.setSpead(15);//滚动频率,默认15毫秒一次,数值太小会影响效率
//mv.setScrollLength(3);//默认每次左移3px,数值太大会有停顿感,数值太小滚动会变慢
mv.startScroll();

//记得在Ondestroy中停止滚动,方便回收
@Override
 protected void onDestroy() {
  try {
   mv.stopScroll();
   mv = null;
  } catch (Throwable e) {
   e.printStackTrace();
  }
  super.onDestroy();
 }

关于android中怎么通过自定义View实现跑马灯效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


名称栏目:android中怎么通过自定义View实现跑马灯效果
URL标题:http://cdkjz.cn/article/jsdgdd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220