`
rayln
  • 浏览: 424753 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Textswitcher控件

 
阅读更多
转载:http://www.cnblogs.com/webabcd/archive/2010/01/27/1657171.html

Textswitcher控件可以使文件改变时增加动画效果, 比如淡入淡出.
如图:


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button android:id="@+id/btnChange" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="改变文字" />

    <!--
        TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果)
    -->
    <TextSwitcher android:id="@+id/textSwitcher"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />

</LinearLayout>



package com.textswitcher;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class TextswitcherActivity extends Activity implements ViewSwitcher.ViewFactory {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);

        setTitle("TextSwithcer");

        final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
        // 指定转换器的 ViewSwitcher.ViewFactory
        switcher.setFactory(this);
        
        // 设置淡入和淡出的动画效果
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        switcher.setInAnimation(in);
        switcher.setOutAnimation(out);

        // 单击一次按钮改变一次文字
        Button btnChange = (Button) this.findViewById(R.id.btnChange);
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switcher.setText(String.valueOf(new Random().nextInt()));
            }
        });
    }

    // 重写 ViewSwitcher.ViewFactory 的 makeView(),返回一个 View
    @Override
    public View makeView() {
        TextView textView = new TextView(this);
        textView.setTextSize(36);
        return textView;
    }
}

分享到:
评论

相关推荐

    Android TextSwitcher(文字交换器)的使用

    1. 在布局文件中添加TextSwitcher控件: ```xml &lt;androidx.core.widget.TextSwitcher android:id="@+id/textSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; ``` 2. 在...

    Android-仿照Google在卡片上滚动显示最新评论的TextSwitcher

    本篇文章将深入探讨如何在Android中实现这样一个功能,主要使用TextSwitcher控件。 TextSwitcher是Android SDK提供的一种特殊的View,它主要用于在两个文本之间平滑地切换。这个控件特别适合用于动态更新内容,例如...

    TextSwitcher:仿京东、淘宝滚动小广播,实现文字轮播滚动

    要实现“仿京东、淘宝滚动小广播”的功能,首先需要在布局文件中添加TextSwitcher控件,并设置相应的属性,如宽度、高度、字体样式等。接下来,我们需要创建一个包含多条文本信息的列表,并设置一个定时器(Timer)...

    Android的三种动画图形编程

    在“演示Android中的文字和图片特效控件的使用”资源中,讲是的TextSwitcher控件、Gallery控件和ImageSwitcher控件的特效实现。而这个资源演示的是android的动画编程,本示例针对图片和ListView控件实现动画。本人...

    Android TextSwitcher实现文字上下翻牌效果(铜板街)

    在布局文件中,我们需要添加一个TextSwitcher控件: ```xml &lt;android.widget.TextSwitcher android:id="@+id/tv_notice" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; ``` ...

    Android TextSwitcher文本切换器和ViewFlipper使用详解

    1. **获取TextSwitcher实例**:通过`findViewById()`方法找到XML布局文件中定义的TextSwitcher控件。 ```java TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher); ``` 2. **设置ViewFactory...

    TextSwitcher垂直滚动文字广告demo

    在Android开发中,TextSwitcher是一个非常实用的控件,常用于实现文本的动态切换效果,比如我们在京东、淘宝的搜索框中看到的那种滚动小广播。这个“TextSwitcher垂直滚动文字广告demo”是一个示例项目,展示了如何...

    ViewSwitcher实例ImageSwitcher和TextSwitcher免费demo

    在Android开发中,ViewSwitcher是一个非常实用的控件,它属于ViewAnimator家族的一部分,用于在两个或更多视图之间进行切换。这个实例是关于如何使用ViewSwitcher的子类ImageSwitcher和TextSwitcher来实现动态更换...

    图片轮播 文字轮播 ImageSwitcher TextSwitcher

    ImageSwitcher和TextSwitcher是Android SDK提供的一对控件,专门用于实现这样的动态切换效果。下面我们将深入探讨这两个控件的工作原理、使用方法以及实际应用中的注意事项。 首先,ImageSwitcher是用于在两个...

    Android 中文 API (17) —— TextSwitcher

    在Android开发过程中,`TextSwitcher`类作为一个特殊的控件,被设计用于动态地改变界面上显示的文本,并支持动画效果,从而使得用户界面更加生动有趣。`TextSwitcher`继承自`ViewSwitcher`类,它仅接受`TextView`...

    智能家居系统 文本切换TextSwitcher.doc

    "文本切换 TextSwitcher"是 Android 平台上一种用于实现动态文本切换效果的控件,常被用于智能家居应用的界面设计中,以提供更丰富的用户反馈和信息展示。 1. **功能特性** TextSwitcher 主要用于在界面上动态地...

    Android开发实现自动切换文字TextSwitcher功能示例

    在Android开发中,TextSwitcher是一个非常实用的控件,它是ViewSwitcher的子类,专为在界面上实现文本内容自动切换而设计。通过TextSwitcher,开发者可以在多个文本字符串间平滑地进行切换,通常用于显示动态更新或...

    ViewSwitcher,ImageSwitcher,TextSwitcher的使用

    在Android开发中,ViewSwitcher、ImageSwitcher和TextSwitcher是三种用于切换视图的控件,它们属于ViewFlipper家族,常用于实现动画效果的视图切换。这三个组件在用户界面设计中扮演着重要角色,特别是在创建动态、...

    Android控件操作-二十四章经 - v1.01

    第六章“切换类TextSwitcher和ImageSwitcher”则介绍了用于动态切换文本和图像的控件,适用于制作简单的动画效果或信息展示。 第七章“下拉框Spinner控件”讲述了Spinner的使用,包括数据绑定、监听选择变化,使得...

    android学习文档--控件2.pptx

    - **TextSwitcher**:用于快速切换文本内容的控件。 - **TabHost**、**TabWidget**、**TabActivity**:这三个控件共同组成 Tab 布局,其中 `TabHost` 作为容器,`TabWidget` 用于显示 Tab 按钮,而 `TabActivity` 则...

    自定义TextView实现垂直自动轮播

    本示例中,我们探讨的是如何自定义一个TextView来实现垂直自动轮播功能,这个控件名为`ScrollTextView`,它继承了`TextSwitcher`。`TextSwitcher`是一个用于在两个视图之间切换文本的视图,通常用于动画效果,而在...

    Android控件及布局的使用

    ### Android控件及布局的使用知识点详解 #### 第一章:Android基础 ##### 1.1 Android开发环境搭建 - **安装JDK (Java Development Kit)**:Java是Android开发的基础语言,因此首先需要安装JDK。可以从Oracle官网...

    Android 文字跑马灯文字水平自动滚动控件及效果演示.rar

    Android 文字跑马灯文字水平自动滚动控件及效果演示,文字左右移动特效,文字滚动速度可调、文本颜色也可以自定义,可以用手触屏来控制是否停止文字滚动,控制点击停止或者继续运行,在开始滚动前,对文字样式做以下...

    Android应用源码之4.TextView控件学习.zip

    TextView支持多种动画效果,如淡入淡出、平移等,可以使用`TextSwitcher`或自定义动画实现。 9. **自定义字体** 可以通过加载本地字体文件或在线字体库,自定义TextView的字体。首先将字体文件放入res/font目录,...

Global site tag (gtag.js) - Google Analytics