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。然后实现跑马灯的效果!

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

  • 相关阅读:
    hihoCoder #1176 : 欧拉路·一 (简单)
    228 Summary Ranges 汇总区间
    227 Basic Calculator II 基本计算器II
    226 Invert Binary Tree 翻转二叉树
    225 Implement Stack using Queues 队列实现栈
    224 Basic Calculator 基本计算器
    223 Rectangle Area 矩形面积
    222 Count Complete Tree Nodes 完全二叉树的节点个数
    221 Maximal Square 最大正方形
    220 Contains Duplicate III 存在重复 III
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7263415.html
Copyright © 2011-2022 走看看