textswitcher

Android TextSwitcher and ImageSwitcher are classes used to add animations to texts and images as they are being displayed on the screen to make them visually appealing. In this tutorial we’ll implement each of these.

Android TextSwitcherImageSwitcher是用于在文本和图像在屏幕上显示时向其添加动画的类,以使它们具有视觉吸引力。 在本教程中,我们将实现所有这些。

Android TextSwitcher和ImageSwitcher概述 (Android TextSwitcher and ImageSwitcher Overview)

The TextSwitcher and ImageSwitcher provide us a simple way to add animated transitions. These classes are used for a smooth transition animation in android view. Some usages of these are:

TextSwitcherImageSwitcher为我们提供了一种添加动画过渡的简单方法。 这些类用于android视图中的平滑过渡动画。 这些的一些用法是:

  • Navigating through a list of dates with Left and Right buttons使用向左和向右按钮浏览日期列表
  • Changing numbers in a date picker在日期选择器中更改数字
  • Countdown timer clock倒数计时器时钟
  • Smooth transition for a news headlines slider新闻标题滑块的平滑过渡

Android TextSwitcher (Android TextSwitcher)

A TextSwitcher is a specialised form of ViewSwitcher that contains only children of type TextView. TextSwitcher is used to animate a TextView when the text changes. Two types of animations are required to switch between texts:

一个TextSwitcherViewSwitcher的一种特殊形式,它包含唯一类型的TextView的孩子。 当文本更改时,TextSwitcher用于为TextView设置动画。 在文本之间切换需要两种动画:

  • In Animation: with which Text come in the Screen在动画中 :屏幕上出现的文本
  • Out Animation: with which Text goes out from the Screen动画 :文字从屏幕上消失

Android ImageSwitcher (Android ImageSwitcher)

As you might have noticed in the previous tutorials containing ImageViews that the loading and rendering of images was abrupt and not smooth. Here ImageSwitcher comes to the rescue since it supports some form of transitions from one image to another. The implementation is given below.

正如您在包含ImageViews的以前的教程中可能已经注意到的那样,图像的加载和渲染是突然且不平滑的。 ImageSwitcher之所以能够解决这个问题,是因为它支持某种形式的从一个图像到另一个图像的过渡。 下面给出了实现。

imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {public View makeView() {ImageView myView = new ImageView(getApplicationContext());return myView;}
}

The only thing left now is to add the Animation as given in the below snippet:

现在剩下的唯一一件事就是添加以下片段中给出的Animation:

Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_up);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);

In short the following steps need to be implemented to use these classes:

简而言之,需要执行以下步骤才能使用这些类:

  • Get the view reference using findViewById() method使用findViewById()方法获取视图引用
  • Set a factory using switcher.setFactory()使用switcher.setFactory()设置工厂
  • Set an in-animation using switcher.setInAnimation()使用switcher.setInAnimation()设置动画
  • Set an out-animation using switcher.setOutAnimation()使用switcher.setOutAnimation()设置动画
  • setFactory() is used to create new viewssetFactory()用于创建新视图
  • setText() on TextSwitcher works as follows:
    • It first removes the old text by using setOutAnimation()
    • It inserts the new text using setInAnimation()

    TextSwitcher上的setText()的工作方式如下:

    • 它首先使用setOutAnimation()删除旧文本。
    • 它使用setInAnimation()插入新文本

项目结构 (Project Structure)

码 (Code)

The layout for the MainActivity contains an ImageSwitcher, TextSwitcher and a Button in a RelativeLayout as shown below.

MainActivity的布局在RelativeLayout中包含一个ImageSwitcher,TextSwitcher和一个按钮,如下所示。

main.xml

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ImageSwitcherandroid:id="@+id/imageSwitcher"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentLeft="true" ></ImageSwitcher><TextSwitcherandroid:id="@+id/textSwitcher"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:padding="10dp" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_marginTop="10dp"android:onClick="onSwitch"android:text="Next Image >>" /></RelativeLayout>

The MainActivity consists of the Text an Image Switchers with their views created using setFactory(). The animations used are built in animations present in the android sdk. The application consists of three TextViews and ImageViews which are switched in cyclic order on each button click. The MainActivity.java is given below :

MainActivity包括“文本和图像切换器”,以及使用setFactory()创建的视图。 所使用的动画是内置在android sdk中的动画中的。 该应用程序由三个TextView和ImageView组成,它们在每次单击按钮时都以循环顺序切换。 MainActivity.java如下所示:

package com.journaldev.switchers;import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;public class MainActivity extends Activity {private static final String[] TEXTS = { "Background", "XP", "Sky" };private static final int[] IMAGES = { R.drawable.background, R.drawable.sample,R.drawable.sample_2 };private int mPosition = 0;private TextSwitcher mTextSwitcher;private ImageSwitcher mImageSwitcher;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {TextView textView = new TextView(MainActivity.this);textView.setTextSize(18);textView.setGravity(Gravity.CENTER);return textView;}});mTextSwitcher.setInAnimation(this, android.R.anim.fade_in);mTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);mImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(MainActivity.this);return imageView;}});mImageSwitcher.setInAnimation(this, android.R.anim.slide_in_left);mImageSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);onSwitch(null);}public void onSwitch(View view) {mTextSwitcher.setText(TEXTS[mPosition]);mImageSwitcher.setBackgroundResource(IMAGES[mPosition]);mPosition = (mPosition + 1) % TEXTS.length;}
}

The onSwitch() method is invoked on button click. Below is our application in execution.

单击按钮时将调用onSwitch()方法。 以下是我们正在执行的应用程序。

This brings an end to this tutorial. You can download the final Android Switchers Project from the link given below.

本教程到此结束。 您可以从下面给出的链接下载最终的Android Switchers项目

Download Android Switchers Example Project下载Android切换器示例项目

翻译自: https://www.journaldev.com/9555/android-textswitcher-and-imageswitcher-example-tutorial

textswitcher

textswitcher_Android TextSwitcher和ImageSwitcher示例教程相关推荐

  1. JQuery图表插件Highcharts示例教程

    JQuery图表插件Highcharts示例教程,先上图,大伙Show一下效果:先上三个图,分别是曲线.柱状.扇形. 图表中的数据纯属于DEMO的测试数据,没有实际用意.下面讲下大致的实现步骤 第一步 ...

  2. jquery饼状图插件的指引线_JQuery图表插件Highcharts示例教程

    JQuery图表插件Highcharts示例教程,先上图,大伙Show一下效果:先上三个图,分别是曲线.柱状.扇形. 图表中的数据纯属于DEMO的测试数据,没有实际用意.下面讲下大致的实现步骤 第一步 ...

  3. java 设计模式 示例_Java中的状态设计模式–示例教程

    java 设计模式 示例 状态模式是行为设计模式之一 . 当对象根据其内部状态更改其行为时,将使用状态设计模式. 如果必须根据对象的状态更改其行为,则可以在对象中使用状态变量,并使用if-else条件 ...

  4. memento模式_Java中的Memento设计模式-示例教程

    memento模式 记忆模式是行为设计模式之一 . 当我们要保存对象的状态以便以后可以恢复时,可以使用Memento设计模式. 使用Memento模式以这种方式实现该目的,即无法在对象外部访问对象的已 ...

  5. java 设计模式 示例_Java中的访问者设计模式–示例教程

    java 设计模式 示例 访客模式是行为设计模式之一 . 当我们必须对一组相似类型的对象执行操作时,将使用访问者模式. 借助访问者模式,我们可以将操作逻辑从对象移动到另一个类. 例如,假设有一个购物车 ...

  6. java 观察者模式示例_Java中的观察者设计模式-示例教程

    java 观察者模式示例 观察者模式是行为设计模式之一 . 当您对对象的状态感兴趣并希望在发生任何更改时得到通知时,观察者设计模式很有用. 在观察者模式中,监视另一个对象状态的对象称为Observer ...

  7. java 设计模式 示例_Java中的中介器设计模式-示例教程

    java 设计模式 示例 中介者模式是行为设计模式之一 ,因此它处理对象的行为. 中介器设计模式用于在系统中不同对象之间提供集中式通信介质. 根据GoF,中介者模式意图是: 通过封装不同对象集相互交互 ...

  8. java 设计模式 示例_Java中的策略设计模式-示例教程

    java 设计模式 示例 策略模式是行为设计模式之一 . 当我们对一个特定的任务有多种算法并且客户决定在运行时使用的实际实现时,将使用策略模式. 策略模式也称为策略模式 . 我们定义了多种算法,并让客 ...

  9. java设计模式迭代器模式_Java中的迭代器设计模式–示例教程

    java设计模式迭代器模式 迭代器模式是一种行为模式,它用于提供遍历一组对象的标准方式. Iterator模式在Java Collection Framework中得到了广泛使用,其中Iterator ...

最新文章

  1. java可以入侵电脑系统吗_如何通过tomcat入侵远程计算机系统
  2. Dapr 客户端 搭配 WebApiClientCore 玩耍服务调用
  3. python3 beautifulsoup 模块详解_关于beautifulsoup模块的详细介绍
  4. 2020年中国美好消费趋势报告
  5. 团队协作之文档管理-ShowDoc本地化安装使用
  6. java垃圾回收 分代_Java-垃圾回收机制-通用的分代垃圾回收机制
  7. SAP License:CO常见问题
  8. 5G网络出现打乱传统网络范式,AI驱动+区块链能搞出啥新花样?
  9. 最大似然估计_机器学习最大似然估计
  10. 第 22 章 Node.js 安装
  11. onenote使用python开发_如何充分利用 OneNote,发挥它的全部价值和潜力?
  12. 【前端】弹出框提交表单
  13. 现在Web前端培训,哪个机构比较好?
  14. 双拼输入法软件测试,为什么推荐你使用双拼输入法?
  15. 习题9-1 时间换算(15 分)
  16. Fedora14 基于Qt的UDP传输文字聊天小软件实现 (Qt查询本地Ip、Qt本地时间显示、传输中文汉字实现、Qt的textedit自动滚屏实现、给QPushButton设键盘快捷实现)---续上
  17. PHP 计算个人所得税(两种方式)
  18. revo uninstaller pro 长期试用的正确姿势!!
  19. 图表美化设置圆角——《超级处理器》应用
  20. 数据类型、变量、字符串(工匠工坊第二课)

热门文章

  1. [转载] Python开发系列课程(16) - 进程和线程
  2. CodeSmith使用存档
  3. [NOIp提高组2014]解方程
  4. 【linux之bash】
  5. 局域网电脑Sql2008 R2无法连接到localhost 解决方案
  6. js function定义函数的4种方法
  7. Java的重写equals但不重写hashCode方法的影响
  8. WebResource.axd引起的问题
  9. (十六)K-Means聚类
  10. 在CMakeLists.txt文件中包含Eigen