android 文字滚动,全屏字幕滚动APP实现

android 一款文字滚动App

  • 下载app体验

https://download.csdn.net/download/qq_38355313/88063389
新增自定义文字大小和自定义滚动速度

  • 先上效果图

android 文字滚动,全屏字幕滚动APP实现_第1张图片
android 文字滚动,全屏字幕滚动APP实现_第2张图片
android 文字滚动,全屏字幕滚动APP实现_第3张图片
android 文字滚动,全屏字幕滚动APP实现_第4张图片

核心就是自定义文字滚动的类

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.example.baselibrary.CommonParameters;
import com.example.baselibrary.utils.SPUtil;


/**
 * @Author MUZI102
 * @Date 2022/2/717:58
 * @Version 1.0
 * 

*

* 所有属性 尽量用Builder 来控制 */ public class ScrollTextView extends View { public static final int SCROLLDIRECTION_LEFT_TO_RIGHT = 0; public static final int SCROLLDIRECTION_RIGHT_TO_LEFT = 1; public static final int SCROLLDIRECTION_NO_MOVE = 2; public static final int TEXT_SIZE_SMALL = 3; public static final int TEXT_SIZE_MEDIUM = 4; public static final int TEXT_SIZE_LAGRE = 5; public static final int TEXT_SIZE_EXTRA_LARGE = 55; public static final int SCROLL_SPEED_SLOW = 6; public static final int SCROLL_SPEED_MEDIUM = 7; public static final int SCROLL_SPEED_FAST = 8; public static final int SCROLL_SPEED_PARTICULARLY_FAST = 9; public static final int SCROLL_SELECT_WHITE = 10; //#FFFFFF public static final int SCROLL_SELECT_BLACK = 11;//#000000 public static final int SCROLL_SELECT_RED = 12;//红色 public static final int SCROLL_SELECT_FUCHISA = 13;//红色 public static final int SCROLL_SELECT_LAWNGREEN = 14;//红色 public static final int SCROLL_SELECT_YELLOW = 16;//红色 public static final int SCROLL_SELECT_LIME = 17;//红色 public static final int SCROLL_SELECT_BLUE = 18;//红色 private Paint mPaint; private int width; private int higth; private float stringWidth; private float currentTextX; private float currentTextY; private String drawString = ""; private Builder mBuilder; private static final int CYCLE_INDEX = 100; private boolean isScroll = true; private int currentSpeed = 0; private int scrollDirection = SCROLLDIRECTION_LEFT_TO_RIGHT; private boolean isStart = false; private Context context; public Builder getBuilder() { if (mBuilder == null) { mBuilder = new Builder(); } return mBuilder; } public void setBuilder(Builder mBuilder) { this.mBuilder = mBuilder; measureTextlLocation(); SPUtil.getInstance(context).setBean(CommonParameters.SP_KEY_SCROLLTEXTVIEW_BUILDER, mBuilder); } public ScrollTextView(Context context) { super(context); initPaint(); this.context = context; } public ScrollTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initPaint(); this.context = context; } public ScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(); this.context = context; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onFinishInflate() { super.onFinishInflate(); } /** * 初始化画笔设置 */ private void initPaint() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setAntiAlias(true); } private void measureTextlLocation() { width = getMeasuredWidth(); higth = getMeasuredHeight(); if (mBuilder.getTextSize() == TEXT_SIZE_SMALL) { mPaint.setTextSize(context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.sp_100)); } else if (mBuilder.getTextSize() == TEXT_SIZE_MEDIUM) { mPaint.setTextSize(context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.sp_130)); } else if (mBuilder.getTextSize() == TEXT_SIZE_LAGRE) { mPaint.setTextSize(context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.sp_160)); } else if (mBuilder.getTextSize() == TEXT_SIZE_EXTRA_LARGE) { mPaint.setTextSize(context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.sp_200)); } else { mPaint.setTextSize(context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.sp_250)); } if (mBuilder.getScrollSpeed() == SCROLL_SPEED_SLOW) { currentSpeed = context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.dp_5); } else if (mBuilder.getScrollSpeed() == SCROLL_SPEED_MEDIUM) { currentSpeed = context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.dp_7); } else if (mBuilder.getScrollSpeed() == SCROLL_SPEED_FAST) { currentSpeed = context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.dp_9); } else if (mBuilder.getScrollSpeed() == SCROLL_SPEED_PARTICULARLY_FAST) { currentSpeed = context.getResources().getDimensionPixelSize(com.example.resourcelibrary.R.dimen.dp_11); } else { currentSpeed = 10; } mPaint.setColor(Color.parseColor(getColor(mBuilder.getTextColor()))); this.isScroll = mBuilder.isScroll(); this.scrollDirection = mBuilder.getScrollDirection(); StringBuilder StringBuilder = new StringBuilder(); for (int i = 0; i < CYCLE_INDEX; i++) { StringBuilder.append(mBuilder.getMsg()).append(" "); } if (mBuilder.getScrollDirection() == SCROLLDIRECTION_LEFT_TO_RIGHT) { //左往右 //文字反转 drawString = StringBuilder.reverse().toString(); //文字的y轴坐标 Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); stringWidth = mPaint.measureText(drawString); currentTextY = getMeasuredHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2; currentTextX = -stringWidth; } else if (mBuilder.getScrollDirection() == SCROLLDIRECTION_RIGHT_TO_LEFT) { //右往左 drawString = StringBuilder.toString(); //文字的y轴坐标 Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); stringWidth = mPaint.measureText(drawString); currentTextY = getMeasuredHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2; currentTextX = width; } else { //禁止不动 drawString = mBuilder.getMsg(); //文字的y轴坐标 Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); currentTextY = getMeasuredHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2; currentTextX = getWidth() / 2 - mPaint.measureText(drawString) / 2; } invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isStart) { if (mBuilder != null) { if (!isScroll) { //不滚动,则居中显示 canvas.drawText(drawString, currentTextX, currentTextY, mPaint); } else { //滚动 canvas.drawText(drawString, currentTextX, currentTextY, mPaint); if (scrollDirection == SCROLLDIRECTION_LEFT_TO_RIGHT) { //左往右 currentTextX = currentTextX + currentSpeed; if (currentTextX >= 0) { measureTextlLocation(); return; } } else if (scrollDirection == SCROLLDIRECTION_RIGHT_TO_LEFT) { //右往左 currentTextX = currentTextX - currentSpeed; if (0 > currentTextX + stringWidth) { measureTextlLocation(); return; } } invalidate(); } } } } public boolean isStart() { return isStart; } public void setStart(boolean start) { isStart = start; if (isStart) { invalidate(); } } public static class Builder { private String msg = "欢迎使用字幕滚动app!"; private int textSize = ScrollTextView.TEXT_SIZE_MEDIUM; private int scrollDirection = ScrollTextView.SCROLLDIRECTION_RIGHT_TO_LEFT; private int textColor = SCROLL_SELECT_WHITE; private int scrollSpeed = ScrollTextView.SCROLL_SPEED_MEDIUM; private boolean isScroll = true; private int bgColor = SCROLL_SELECT_BLACK; public Builder(String msg, int textSize, @ColorInt int textColor, int scrollDirection, int scrollSpeed, boolean isScroll, int bgColor) { this.msg = msg; this.textSize = textSize; this.textColor = textColor; this.scrollDirection = scrollDirection; this.scrollSpeed = scrollSpeed; this.isScroll = isScroll; this.bgColor = bgColor; } public Builder() { } public Builder(Builder builder) { this.msg = builder.getMsg(); this.textSize = builder.getTextSize(); this.textColor = builder.getTextColor(); this.scrollDirection = builder.getScrollDirection(); this.scrollSpeed = builder.getScrollSpeed(); this.isScroll = builder.isScroll(); this.bgColor = builder.getBgColor(); } public String getMsg() { return msg; } public Builder setMsg(String msg) { this.msg = msg; return this; } public int getTextSize() { return textSize; } public Builder setTextSize(int textSize) { this.textSize = textSize; return this; } public int getTextColor() { return textColor; } public Builder setTextColor(int textColor) { this.textColor = textColor; return this; } public int getScrollDirection() { return scrollDirection; } public Builder setScrollDirection(int scrollDirection) { this.scrollDirection = scrollDirection; return this; } public int getScrollSpeed() { return scrollSpeed; } public Builder setScrollSpeed(int scrollSpeed) { this.scrollSpeed = scrollSpeed; return this; } public boolean isScroll() { return isScroll; } public int getBgColor() { return bgColor; } public void setBgColor(int bgColor) { this.bgColor = bgColor; } public Builder setScroll(boolean scroll) { isScroll = scroll; return this; } @Override public String toString() { return "Builder{" + "msg='" + msg + '\'' + ", textSize=" + textSize + ", scrollDirection=" + scrollDirection + ", textColor=" + textColor + ", scrollSpeed=" + scrollSpeed + ", isScroll=" + isScroll + ", bgColor=" + bgColor + '}'; } } /** * 设置背景颜色 * * @param type */ public void setBgColor(int type) { if (onBgColorChange != null) { onBgColorChange.onBgColorChange(type); } mBuilder.setBgColor(type); SPUtil.getInstance(context).setData(CommonParameters.SP_KEY_SCROLLTEXTVIEW_BUILDER, mBuilder); } public void setOnBgColorChange(OnBgColorChange onBgColorChange) { this.onBgColorChange = onBgColorChange; } private OnBgColorChange onBgColorChange; public interface OnBgColorChange { void onBgColorChange(int bg); } public String getColor(int type) { if (type == SCROLL_SELECT_WHITE) { return "#ffffff"; } else if (type == SCROLL_SELECT_BLACK) { return "#000000"; } else if (type == SCROLL_SELECT_RED) { return "#ff0000"; } else if (type == SCROLL_SELECT_FUCHISA) { return "#ff00ff"; } else if (type == SCROLL_SELECT_LAWNGREEN) { return "#7cfc00"; } else if (type == SCROLL_SELECT_YELLOW) { return "#ffff00"; } else if (type == SCROLL_SELECT_LIME) { return "#00ff00"; } else if (type == SCROLL_SELECT_BLUE) { return "#0000ff"; } else { return "#ffffff"; } } }

主要属性是通过内部类Builder来控制

    public static class Builder {
        private String msg = "欢迎使用字幕滚动app!";  //需要滚动的文字内容
        private int textSize = ScrollTextView.TEXT_SIZE_MEDIUM; //文字大小
        private int scrollDirection = ScrollTextView.SCROLLDIRECTION_RIGHT_TO_LEFT; //文字滚动的方向
        private int textColor = SCROLL_SELECT_WHITE; //滚动文字的颜色
        private int scrollSpeed = ScrollTextView.SCROLL_SPEED_MEDIUM; //滚动速度
        private boolean isScroll = true; //是否滚动,false则居中显示
        private int bgColor = SCROLL_SELECT_BLACK; //背景颜色

你可能感兴趣的:(android,java,android,studio)