水平滚动的文字

可以自由拖拽的水平滚动的文字,按下后暂停两秒钟继续滚动

package com.ds.uilib.scrollview;

/**
 * Created by dapeng on 2017/6/14.
 */

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.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.ds.uilib.R;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CustomScrollBar extends SurfaceView implements
        SurfaceHolder.Callback {

    private final String TAG = "CustomScrollBar";

    private SurfaceHolder holder;
    private Paint paint = null;// 画笔
    private boolean bStop = false; // 停止滚动

    private boolean clickEnable = false; // 可以点击
    private boolean isHorizontal = true; // 水平|垂直
    private int speed = 1; // 滚动速度
    private String text = "";// 文本内容
    private float textSize = 15f; // 字号
    private int textColor = Color.BLACK; // 文字颜色
    private int times = Integer.MAX_VALUE; // 滚动次数

    private int viewWidth = 0;// 控件的长度
    private int viewHeight = 0; // 控件的高度
    private float textWidth = 0f;// 水平滚动时的文本长度
    private float textHeight = 0f; // 垂直滚动时的文本高度

    private float textX = 0f;// 文字的横坐标
    private float textY = 0f;// 文字的纵坐标
    private float viewWidth_plus_textLength = 0.0f;// 显示总长度
    private int time = 0; // 已滚动次数

    private ScheduledExecutorService scheduledExecutorService; // 执行滚动线程
    private boolean isMoveLeft;
    private boolean isUp;
    private float moveX;
    private float upX;
    private float textXDown;
    private float downX;
    private float donwTextX;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            bStop=false;
        }
    };

    public CustomScrollBar(Context context) {
        super(context);
    }

    public CustomScrollBar(Context context, AttributeSet attrs) {
        //---------1
        super(context, attrs);
        holder = this.getHolder();
        holder.addCallback(this);
        paint = new Paint();

        //获取布局文件中的值
        TypedArray arr = getContext().obtainStyledAttributes(attrs,
                R.styleable.CustomScrollBar);
        clickEnable = arr.getBoolean(R.styleable.CustomScrollBar_clickEnable,
                clickEnable);
        isHorizontal = arr.getBoolean(R.styleable.CustomScrollBar_isHorizontal,
                isHorizontal);
        speed = arr.getInteger(R.styleable.CustomScrollBar_speed, speed);
        text = arr.getString(R.styleable.CustomScrollBar_text3);
        textColor = arr.getColor(R.styleable.CustomScrollBar_textColor3, textColor);
        textSize = arr.getDimension(R.styleable.CustomScrollBar_textSize3, textSize);
        times = arr.getInteger(R.styleable.CustomScrollBar_times, times);

        time = times;
        paint.setColor(textColor);
        paint.setTextSize(textSize);

        /*
         * 下面两行代码配合draw()方法中的canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
         * 将画布填充为透明
         */
        setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);

        setFocusable(true); // 设置焦点
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //----------2
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);//得到控件的宽度
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);//得到控件的高度
        if (isHorizontal) { // 水平滚动
            textWidth = paint.measureText(text);// 获取text的长度
            viewWidth_plus_textLength = viewWidth + textWidth;
            textY = (viewHeight - getFontHeight(textSize)) / 2
                    + getPaddingTop() - getPaddingBottom();
        } else { // 垂直滚动
            textHeight = getFontHeight(textSize) * text.length();// 获取text的长度
            viewWidth_plus_textLength = viewHeight + textHeight;
            textX = (viewWidth - textSize) / 2 + getPaddingLeft()
                    - getPaddingRight();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        //----------4
        Log.d(TAG, "surfaceChanged: ");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //-----------3
        bStop = false;
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleAtFixedRate(new ScrollThread(), 1000,
                10, TimeUnit.MILLISECONDS);
        Log.d(TAG, "surfaceCreated: ");

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        bStop = true;
        scheduledExecutorService.shutdown();
        Log.d(TAG, "surfaceDestroyed: ");
    }

    // 获取字体高度
    private int getFontHeight(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);
    }

    private void setTimes(int times) {
        if (times <= 0) {
            this.times = Integer.MAX_VALUE;
        } else {
            this.times = times;
            time = times;
        }
    }

    public void setText(String text) {
        this.text = text;
    }

    private void setSpeed(int speed) {
        if (speed > 10 || speed < 0) {
            throw new IllegalArgumentException("Speed was invalid integer, it must between 0 and 10");
        } else {
            this.speed = speed;
        }
    }

    /**
     * 当屏幕被触摸时调用
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!clickEnable) {
            return true;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                bStop = true;
//                if (!bStop && time == 0) {
//                    time = times;
//                }
                downX = event.getX();
                donwTextX =viewWidth- textX;
                break;
            case MotionEvent.ACTION_MOVE:

                Canvas canvas = holder.lockCanvas();
                canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 通过清屏把画布填充为透明
                isMoveLeft = true;
                moveX = event.getRawX();
                canvas.drawText(text, moveX+viewWidth-textX-downX, textY, paint);

                postInvalidate();
                holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
                break;
            case MotionEvent.ACTION_UP:
                isUp = true;
                upX=event.getRawX();
                textX = donwTextX - downX+upX;
                textX = viewWidth -textX;
                if(textX<0){
                    textX=0;
                }
                isMoveLeft = false;
//                bStop = false;
                handler.sendEmptyMessageDelayed(0, 2000);
                break;
        }
        return true;
    }

    private synchronized void draw(float X, float Y) {
        Canvas canvas = holder.lockCanvas();//获取画布
        canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 通过清屏把画布填充为透明
        if (isHorizontal) { // 水平滚动
            canvas.drawText(text, X, Y, paint);
        }
//        else { // 垂直滚动
//            for (int i = 0; i < text.length(); i++) {
//                canvas.drawText(text.charAt(i) + "", X, Y + (i + 1)
//                        * getFontHeight(textSize), paint);
//            }
//        }
        holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
    }

    class ScrollThread implements Runnable {

        @Override
        public void run() {

            while (!bStop) {
                if (isHorizontal) {
                        draw(viewWidth - textX, textY);
                         Log.i("machao",textX+"");
                        textX += speed;// 速度设置:1-10

                    if (textX > viewWidth_plus_textLength) {
                            textX = 0;
                            --time;
                    }

                    //垂直不做处理
//                else {
//                    draw(textX, viewHeight - textY);
//                    textY += speed;
//                    if (textY > viewWidth_plus_textLength) {
//                        textY = 0;
//                        --time;
//                    }
//                }
                    if (time <= 0) {
//                        bStop = true;
                    }
                }
            }
        }
    }
}

属性文件中配置

  
        
        
        
        
        
        
        
    

代码中使用


 

参考文章https://blog.csdn.net/u012604745/article/details/73277245

你可能感兴趣的:(水平滚动的文字)