Hacks动画篇-Hack1 使用TextSwitcher和ImageSwitcher实现平滑过渡

TextSwitcher和ImageSwitcher

作者:李旺成

时间:2016年5月9日


需求说明

假设,现在要实现一个这样的效果,在 TextView 或者 ImageView 中循环浏览信息。有如下场景:

  • 通过向左和向右的导航按钮浏览日期列表
  • 在日期选择控件中改变日期
  • 倒计时时钟
  • 新闻纲要

(参考自:《Android 50 Hacks》)
要更改 TextView 和 ImageView 的内容非常简单,但是有个缺点,就是直接调用 TextView 或者 ImageView 提供的方法替换内容的时候没有什么视觉效果,显得很单调。

可以看到有不少的 App 在文本内容切换的时候添加了动画效果(如:支付宝)。Android 提供了 TextSwitcher 和 ImageSwitcher 分别替换 TextView 和 ImageView,来实现过渡过程的动画效果。

TextSwitcher 与 ImageSwitcher 简介

老规矩,学习一个控件先看看它的继承结构,这里之所以将 TextSwitcher 与 ImageSwitcher 放在一起介绍,是有原因的,下面会介绍。

继承结构

都是直接继承自 ViewSwitcher(所以就把这”两兄弟“放到一块介绍了),继承结构里面有一个很熟悉的,那就是 FrameLayout(好像哪里都有你...)。看到这里至少可以确认一点,ViewSwitcher 是个容器,从名称上推测,TextSwitcher 应该是 Text(文本)的容器,ImageSwitcher 应该是 Image(图片)的容器。

看看官方文档上是怎么说的

TextSwitcher类
ImageSwitcher类

不是我截错图了,ImageSwitcher 下就是没有简介,那只好看看它的父类了:

ViewSwitcher

介绍都非常简洁(甚至说简单),这里就不纠结了,反正离不了是个容器,直接用起来。

TextSwitcher 简单使用

先看效果:

TextSwitcher演示

1、在布局文件中使用
上面说了,就把它当成普通容器使用即可,示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下一个" />
    <TextSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

2、认识 Factory
文档中提到要给 ViewSwitcher 设置一个 Factory 用来创建 views。来看看这个 Factory 到底是什么:

/**
 * Creates views in a ViewSwitcher.
 */
public interface ViewFactory {
    /**
     * Creates a new {@link android.view.View} to be added in a
     * {@link android.widget.ViewSwitcher}.
     *
     * @return a {@link android.view.View}
     */
    View makeView();
}

它是 ViewSwitcher 内部的一个接口,里面只有一个方法,返回值为 View,那么很简单了,只要实现一个方法。

3、为 TextSwitcher 设置 Factory
类似于这种和设置监听有点像的情况,我一般有两种实现方式,一种是让 Activity 实现接口,另一种是使用匿名内部类,具体看情况。示例:

// public class TextSwitcherActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory

...

private void setFactory() {
    mSwitcher.setFactory(this);
    mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            TextView t = new TextView(TextSwitcherActivity.this);
            t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            t.setTextSize(36);
            return t;
        }
    });
}

// 实现接口中的方法
@Override
public View makeView() {
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    t.setTextSize(36);
    return t;
}

...

4、为 TextSwitcher 设置 动画

Animation in = AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);

mTextSwitcher.setInAnimation(this, android.R.anim.fade_in);
mTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);

很简单,没有什么要说的。

5、更新 TextSwitcher 的内容

private void updateCounter() {
    mSwitcher.setText(String.valueOf(mCounter));
}

private void onSwitchText() {
    mTextSwitcher.setText(TEXTS[mTextsPosition]);
}

对,就和 TextView 一样,调用 TextSwitcher 的 setText() 方法即可,每次调用该方法的时候都会使用你所指定的动画来切换内容。

ImageSwitcher 简单使用

先看效果:

ImageSwitcher演示

1、在布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#55000000"
        android:gravity="center_vertical"
        android:spacing="16dp" />
</RelativeLayout>

直接从 APIDemos 里面拷出来的代码,大家凑合着看吧。

2、设置 Factory
这和 TextSwitcher 没什么不同,不过要注意的是 ImageSwitcher 是用来为 Image 切换提供视觉效果的,也就是说,它里面要放 ImageView。

mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);

@Override
public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    return i;
}

3、设置动画

mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out));

4、切换图片

mSwitcher.setImageResource(mImageIds[position]);

对,ImageSwitcher 提供了和 ImageView 一样的方法,用来设置 Image。

小结

TextSwitcher 与 ImageSwitcher 的使用很简单,总结一下就是下面几步:

  1. 获取实例
    一般是先在布局文件中写好,然后通过 findViewById() 方法获取,当然也可以直接在代码中 new
  2. 为 ViewSwitcher 指定 Factory
    通过 viewSwitcher.setFactory() 方法设置,Factory 的实现是情况而定
  3. 设置切换动画
    setInAnimation():设置换入动画
    setOutAnimation():设置换出动画
  4. 切换内容
    TextSwitcher 调用 setText() 方法
    ImageSwitcher 调用 setImageXxx() 方法

好了关于 ViewSwitcher 的介绍到这里了,希望对你有用。

项目地址

AndroidHacks合集
动画篇

示例用到代码见:
TextSwitcherActivity.java
ImageSwitcherActivity.java
activity_textswitcher.xml
activity_image_switcher.xml

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,213评论 25 707
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,113评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,509评论 6 30
  • 11月的清晨 我找不到眼镜 推开窗子呼吸 能让我很精神 被慵懒推开屋门 她说你该吃早餐了。oh。你早就该起身! 1...
    做一只开不了口的猪阅读 165评论 0 0
  • 2017.5.17 文/满全 (3/100) 看似不重要的细节,往往决定了你的收入。千万别不当回事,学会了这些,在...
    王者甘阅读 336评论 1 1