android中跑马灯效果的实现

android中常见的跑马灯效果实现的代码以及思路分析

  • 有些童鞋想用TextView实现跑马灯的效果 , 代码如下:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="我是一个很长很长很长很长很长很长很长很长很长很长的跑马灯效果"/>

但是 , 这种方式会出现各种焦点问题 , 比如同一个 界面上出现两个跑马灯的控件 , 这时候就会导致后面的那个跑马灯控件效果无法实现 , 因为android默认一个界面上只有一个焦点 , 而一个跑马灯控件是需要一个焦点的 , 这就导致抢焦点问题 , 因此建议使用自定义的跑马灯控件.

  • 跑马灯效果的正确姿势

自定义控件代码:有三种情况需要注意
a.要告诉我们的系统 , 我这个跑马灯控件是一直有焦点的 , 这样才会有跑马灯的效果;
b.更改焦点的情况下 , 例如点击EditText控件需要焦点的时候 , 如果跑马灯控件有焦点才调用父类的改变焦点的方法;
c.弹出dialog的情况下 , 同情况b.

package com.lyx.marqueetextview.views;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by liyongxiang on 2017/4/30.
 */

public class MarqueeTextView extends TextView {

    public MarqueeTextView(Context context) {
        this(context, null);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * a.当前控件的焦点,第一次xml加载 的情况
     */
    @Override
    public boolean isFocused() {
        return true;//告诉我们的系统 ,我这里是一直有焦点的
    }

    //b.在更改焦点时,有别的控件申请焦点的情况下
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if (focused) {//有焦点
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
        }
    }

    //c.弹出对话框的情况下
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        if (hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus);
        }
    }
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.lyx.marqueetextview.MainActivity">

    <com.lyx.marqueetextview.views.MarqueeTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="我是一个很长很长很长很长很长很长很长很长很长的跑马灯效果"
        android:textSize="20sp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:hint="请输入内容"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="弹出对话框"/>
</LinearLayout>

效果图:
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值