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
- Android开发中跑马灯效果的实现
- android中跑马灯效果的实现
- TextView中跑马灯效果的实现
- Android开发textview中跑马灯效果
- iOS中跑马灯效果小结
- TextView中跑马灯效果获取focus焦点
- Android中跑马灯 maxLines与singleLine
- 解决复杂布局中跑马灯不能跑的问题
- android 文字太长跑马灯获不到焦点无法显示效果的一种解决方法
- android在armv4t中跑。
- android在armv4t中跑
- Android玄铁剑之TextView之跑跑马灯
- Android手机中跑Ubuntu系统
- Android自定义的控件在eclipse中提示空指针但是在程序中跑起来没问题
- Android下蒙板效果的实现
- Android透明效果的实现
- android的半透明效果实现
- Android透明效果的实现
- 【四月英语——沉默期】
- 深入浅出Spring(二) AOP详解
- 用Charles Proxy测试Facebook、Twitter的方法
- SpringBoot非官方教程 | 第十九篇: 验证表单信息
- HBase shell小结
- android中跑马灯效果的实现
- 门面模式
- Bootstrap实现响应式导航栏效果
- js算法之最常用的排序
- java中讲讲File的用法,举例?
- Android 控件多次点击处理方法
- 自执行匿名函数剖析
- MySQL中出现关于no data
- 深入理解C++对象模型之Data Member存取成本