android多个跑马灯6,【Android】实现走马灯并可设置速度

本文介绍了如何解决TextView走马灯在失去焦点时暂停的问题,通过创建自定义MarqueeView类,使其始终保持焦点。此外,还展示了如何利用反射技术自定义走马灯的滚动速度,从而实现更灵活的控制。

一、前言

使用TextView实现走马灯效果非常的简单,只需要在布局里添加一个如下的TextView;

android:id="@+id/marquee"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ellipsize="marquee"

android:marqueeRepeatLimit="marquee_forever"

android:singleLine="true"

android:focusable="true"

android:focusableInTouchMode="true" />

但是,当TextView失去焦点时(例如我们有两个走马灯,或者触发弹窗),走马灯会暂停(有兴趣的可以自己试一下),等到重新获取到焦点时,才继续;

二、实现自定义TextView

为了解决上述问题,我们可以自定义一个MarqueeView,继承自TextView,并让他一直有焦♂点;

package com.mory.MarqueeView;

import android.annotation.SuppressLint;

import android.content.Context;

import android.graphics.Rect;

import android.support.annotation.Nullable;

import android.support.v7.widget.AppCompatTextView;

import android.text.TextUtils;

import android.util.AttributeSet;

import android.util.Log;

import java.lang.reflect.Field;

public class MarqueeView extends AppCompatTextView {

private static final String TAG = "MarqueeView";

public MarqueeView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

init();

}

@Override

protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {

if (focused) {

super.onFocusChanged(true, direction, previouslyFocusedRect);

}

}

@Override

public void onWindowFocusChanged(boolean hasWindowFocus) {

if (hasWindowFocus) {

super.onWindowFocusChanged(true);

}

}

@Override

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

}

private void init() {

setSingleLine();

setEllipsize(TextUtils.TruncateAt.MARQUEE);

setMarqueeRepeatLimit(-1);

setFocusable(true);

setFocusableInTouchMode(true);

}

}

上述代码中做的操作很简单,只是把布局中配置的属性放到代码中完成;

三、使用自定义View

在我们的布局中,使用MarqueeView;

android:id="@+id/marquee"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center" />

当目前为止,已经可以正常使用的走马灯了;

但是,有的小朋友问了(无中生友):咱们怎么设置它的滚动速度呢

四、 利用反射自定义速度

我在网络上看到的实现方式都有些复杂,有着大量的代码和线程交互(例如io线程轮询通过handler通知ui线程更新),因此决定尝试使用反射方式,通过修改系统定义的速度来实现;

/**

* 利用反射 设置跑马灯的速度

* 在onLayout中调用即可

*

* @param newSpeed 新的速度

*/

@SuppressLint("PrivateApi")

private void setMarqueeSpeed(float newSpeed) {

try {

// 获取走马灯配置对象

Class tvClass = Class.forName("android.widget.TextView");

Field marqueeField = tvClass.getDeclaredField("mMarquee");

marqueeField.setAccessible(true);

Object marquee = marqueeField.get(this);

if (marquee == null) {

return;

}

// 设置新的速度

Class> marqueeClass = marquee.getClass();

/*

// 速度变量的名称可能与此示例的不相同 可自行打印查看

for (Field field : marqueeClass.getDeclaredFields()) {

Log.i(TAG, field.getName());

}

*/

// SDK中的是mPixelsPerMs,但我的开发机是下面的名称

Field speedField = marqueeClass.getDeclaredField("mPixelsPerSecond");

speedField.setAccessible(true);

float orgSpeed = (float) speedField.get(marquee);

// 这里设置了相对于原来的20倍

speedField.set(marquee, newSpeed);

Log.i(TAG, "setMarqueeSpeed: " + orgSpeed);

Log.i(TAG, "setMarqueeSpeed: " + newSpeed);

} catch (ClassNotFoundException | NoSuchFieldException e) {

Log.e(TAG, "setMarqueeSpeed: 设置跑马灯速度失败", e);

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

这里踩了一个坑,我的Compile SDK是28的,SDK源码中变量名叫mPixelsPerMs,我的开发机并不是这个(可能是版本不同,开发机是22);

如果你和我一样也是固定机型,不考虑适配的话,可以解开里面的多行注释,然后自己找找是哪个名称;

如果需要适配,可以试试,看看不同版本的SDK下变量名分别叫什么;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值