android中跑马灯效果的实现

来源:互联网 发布:日本为何没人篡位知乎 编辑:程序博客网 时间:2024/04/29 10:52

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