一个Layout多个跑马灯的解决方案

效果:

之前在网上找了很多关于TextView的跑马灯效果实现的例子,实现起来都存在一些问题,例如一种是完全重画一个跑马灯,还有就是只设置TextView的相关属性使其具有跑马灯的效果,总的来说这两种方法都是可行的,但是都有其不足之处,第一种太复杂,实现起来比较麻烦,第二种呢,它只能在TextView获得焦点的时候才有跑马灯的效果,这样有时候并不能达到我们所要求的效果。我通过网上的一些例子自己在做了一些改动,就实现了现在不用获取焦点也能“跑”起来的效果。

首先,写一个类,让其继承自TextView:

 
public class MarqueeText extends TextView {
    public MarqueeText(Context con) {
        super(con);
    }

    public MarqueeText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

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

然后再将我们已经写好的这个控件(MarqueeText)放到布局文件中,例如main.xml:

 
 
<com.example.MarqueeText  
        android:id="@+id/AMTV1"  
        android:layout_width="400dip"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="80dip"  
        android:textSize="25sp"  
        android:textColor="@android:color/black"  
        android:lines="1"  
        android:focusable="true"  
        android:focusableInTouchMode="true"  
        android:scrollHorizontally="true"  
        android:marqueeRepeatLimit="marquee_forever"  
        android:ellipsize="marquee"  
        android:background="#2FFFFFFF"  
        android:text="这才是真正的文字跑马灯效果,文字移动速度,文字移动方向,文字移动的样式,动画等等……"  
        />  

本身是可以不持有焦点的,只要是在selected,所有也可以不自定义控件,直接在代码中textView.set...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值