直播系统源码,图片一直滚动,循环滚动,竖向和横向

直播系统源码,图片一直滚动,循环滚动,竖向和横向实现的相关代码

竖向:

 

 

public class LoopScrollView extends View {
    private final int speed;
    private final Bitmap bitmap;
 
    private Rect clipBounds = new Rect();
    private float offset = 0;
 
    private boolean isStarted;
 
    public LoopScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 0, 0);
        try {
            speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);
            bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));
        } finally {
            ta.recycle();
        }
//        start();
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(bitmap.getWidth(), MeasureSpec.getSize(heightMeasureSpec));
    }
 
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvas == null) {
            return;
        }
 
        canvas.getClipBounds(clipBounds);
 
        float normalizedOffset = offset;
        float layerHeight = bitmap.getHeight();
 
        if (offset <= -layerHeight) {
            offset += (floor(abs(normalizedOffset) / layerHeight) * layerHeight);
        }
 
        float top = offset;
        while (top < clipBounds.height()) {
            canvas.drawBitmap(bitmap, 0, getBitmapTop(layerHeight, top), null);
            top += layerHeight;
        }
 
        if (isStarted) {
            offset -= speed;
            postInvalidateOnAnimation();
        }
    }
 
    private float getBitmapTop(float layerHeight, float top) {
        float bitmapTop = top;
        if (speed < 0) {
            bitmapTop = clipBounds.height() - layerHeight - top;
        }
        return bitmapTop;
    }
 
    /**
     * Start the animation
     */
    public void start() {
        if (!isStarted) {
            isStarted = true;
            postInvalidateOnAnimation();
        }
    }
 
    /**
     * Stop the animation
     */
    public void stop() {
        if (isStarted) {
            isStarted = false;
            invalidate();
        }
    }
}

横向:

 

 

public class LoopScrollView extends View {
    private final int speed;
    private final Bitmap bitmap;
 
    private Rect clipBounds = new Rect();
    private int offset = 0;
 
    private boolean isStarted;
 
    public LoopScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 0, 0);
        try {
            speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);
            bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));
        } finally {
            ta.recycle();
        }
        start();
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());
    }
 
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvas == null) {
            return;
        }
 
        canvas.getClipBounds(clipBounds);
 
        int normalizedOffset = offset;
        int layerWidth = bitmap.getWidth();
        if (offset < -layerWidth) {
            offset += (int) (floor(abs(normalizedOffset) / (float) layerWidth) * layerWidth);
        }
 
        int left = offset;
        while (left < clipBounds.width()) {
            canvas.drawBitmap(bitmap, getBitmapLeft(layerWidth, left), 0, null);
            left += layerWidth;
        }
 
        if (isStarted) {
            offset -= speed;
            postInvalidateOnAnimation();
        }
    }
 
    private float getBitmapLeft(int layerWidth, int left) {
        float bitmapLeft = left;
        if (speed < 0) {
            bitmapLeft = clipBounds.width() - layerWidth - left;
        }
        return bitmapLeft;
    }
 
    /**
     * Start the animation
     */
    public void start() {
        if (!isStarted) {
            isStarted = true;
            postInvalidateOnAnimation();
        }
    }
 
    /**
     * Stop the animation
     */
    public void stop() {
        if (isStarted) {
            isStarted = false;
            invalidate();
        }
    }
}

 

以上就是直播系统源码,图片一直滚动,循环滚动,竖向和横向实现的相关代码, 更多内容欢迎关注之后的文章

 

posted @ 2021-11-24 14:09  云豹科技-苏凌霄  阅读(162)  评论(0编辑  收藏  举报