zoukankan      html  css  js  c++  java
  • android实现跑马灯效果

    第一步:新建一个新项目,MarqueeTextView
    首先为了观察到跑马灯效果,将要显示的文字极可能 写长。在strings.xml目录里面将

    <string name="hello_world">hello_world</string>

    改动为

    <string name="hello_world">我的代码非常长,真的非常长,不行你看看。实际上是骗你的,逗比,hiahia~~~~</string>

    默认情况下。显示文字会自己主动换行!为了实现跑马灯效果。首先要阻止其自己主动换行。通过使用singleLine属性来实现!
    android:singleLine=”true”
    假设其目的不过实现单行文字的跑马灯效果。能够只再通过三个语句来实现!
    android有个ellipsize属性。
    android:ellipsize=”marquee”
    android:focusable=”true”
    android:focusableInTouchMode=”true”

    通过上述代码能够实现单行文字的跑马灯效果。可是假设要实现多行文字的跑马灯效果,将上述代码反复书写,不能实现预想功能!

    由于在上述的focusable属性里面已经将焦点定义到第一行上!兴许的得不到focus!

    能够通过新建一个class类来实现!

    在src目录里面新建一个类命名为MarqueeText

    package com.example.marqueetextviewdemo1;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class MarqueeText extends TextView{
    
        public MarqueeText(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
    
        public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
        }
    
        public MarqueeText(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
        public boolean isFocused() {
            return true;
        }
    }

    然后再将main.xml文件中面的TextView所有改动为 包名+.+类名。
    此次改动是为了将所有以这样的方式定义的,都能获得focus。然后实现跑马灯的效果!

    显示的效果例如以下:
    这里写图片描写叙述

  • 相关阅读:
    Django 想要单独执行文件
    Django基础
    Bootstrap框架
    Font Awesome矢量图标框架
    js函数式编程——蹦床函数
    ie被hao.360劫持的解决方法
    函数式编程——惰性链
    你可能不知道的BFC在实际中的应用
    高度随宽度适应的响应式方案
    腾讯云播放器更新——TCplayer
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7263415.html
Copyright © 2011-2022 走看看