Android 实现跑马灯效果

Android 实现跑马灯效果

Android中实现跑马灯效果有多种方式,本篇简单介绍下:

1: TextView属性实现

java 复制代码
    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="#77000000"
        android:padding="5dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="这是textview的跑马灯效果"
        android:id="@+id/tv1"
        />

这里需要注意下:

  1. 需要限制textview的宽度,不能设置为wrap_content
  2. 启动跑马灯效果需要获取焦点requestFocus().

2: 代码实现

java 复制代码
tv2.setSingleLine();
tv2.setHorizontallyScrolling(true);
tv2.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv2.setMarqueeRepeatLimit(-1);
tv2.setFocusable(true);
tv2.setFocusableInTouchMode(true);
tv2.requestFocus();

3: 自定义 view实现

这里可以使用动画的效果实现.

java 复制代码
package com.test.marquee;

import android.animation.ValueAnimator;
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 android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

public class MarqueeView extends View {
    private String text;
    private Paint paint;
    private float textWidth;
    private float textX;
    private float viewWidth;
    private ValueAnimator animator;

    public MarqueeView(Context context) {
        super(context);
        init();
    }

    public MarqueeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MarqueeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        text = "This is a marquee";
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(50);
        paint.setColor(Color.BLACK);
        textWidth = paint.measureText(text);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        textX = viewWidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(text, textX, getHeight() / 2, paint);
    }

    public void startMarquee() {
        animator= ValueAnimator.ofFloat(viewWidth, -textWidth);
        animator.setDuration(5000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(animation -> {
            textX = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();
    }

    public void stopMarquee() {
        // 停止动画
        if (animator!=null) animator.cancel();
    }
}

4: 实现竖直效果的跑马灯

java 复制代码
package com.test.marquee;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class VerticalMarqueeTextView extends AppCompatTextView {
    private float offsetY;

    public VerticalMarqueeTextView(Context context) {
        super(context);
        init();
    }

    public VerticalMarqueeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setSingleLine();
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setHorizontallyScrolling(true);
        setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(0, offsetY);
        super.onDraw(canvas);
    }

    @Override
    public boolean isFocused() {
        return true;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        post(new Runnable() {
            @Override
            public void run() {
                offsetY -= 1;
                if (offsetY <= -getHeight()) {
                    offsetY = 0;
                }
                invalidate();
                postDelayed(this, 20);
            }
        });
    }
}
相关推荐
随遇丿而安1 小时前
第11周:Activity 跳转与传值 + 跳转优化
android
私人珍藏库2 小时前
[Android] BBLL 开源第三方B哩电视TV端
android·app·生活·工具·多功能
杉氧5 小时前
跨平台资源管理:一套代码如何搞定 Android、iOS 和 Web 的图片与多语言?
android·架构·android jetpack
安卓修改大师6 小时前
安卓修改大师实战:从反编译到定制的完整APK修改指南
android
柚鸥ASO优化7 小时前
安卓APP推广的“降本增效”密码:守好商店内,打好商店外
android·aso优化
我是一颗柠檬8 小时前
【Java项目技术亮点】EXPLAIN深度分析与慢查询治理
android·java·开发语言
Android-Flutter8 小时前
android compose shadow 阴影 使用
android·kotlin·compose
帅次8 小时前
Android 高级工程师面试:Java 多线程与并发 近1年高频追问 22 题
android·java·面试
2501_943782358 小时前
【共创季稿事节】摩斯电码转换器:编码表与双向转换的实现
android·华为·鸿蒙·鸿蒙系统
STCNXPARM8 小时前
Android selinux详解
android·selinux