Android零基础入门第55节:ImageSwitcher和TextSwitcher使用

简介: 原文:Android零基础入门第55节:ImageSwitcher和TextSwitcher使用    上一期我们了解了ViewAnimator组件和ViewSwitcher组件的使用,你都掌握了吗?本期一起来学习ViewSwitcher的两个子组件ImageSwitcher和TextSwitcher。
原文: Android零基础入门第55节:ImageSwitcher和TextSwitcher使用

    上一期我们了解了ViewAnimator组件和ViewSwitcher组件的使用,你都掌握了吗?本期一起来学习ViewSwitcher的两个子组件ImageSwitcher和TextSwitcher。

 

 

一、ImageSwitcher

 

    ImageSwitcher和ImageSwitcher继承了 ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果。ImageSwitcher继承了 ViewSwitcher,并重写了 ViewSwitcher 的 showNext()、showPrevious()方法,因此 ImageSwitcher 使用起来更加简单。

    使用 ImageSwitcher 只要如下两步即可。

  • 为 ImageSwitcher 提供一个 ViewFactory,该 ViewFactory 生成的 View 组件必须是 ImageView。

  • 需要切换图片时,只要调用 ImageSwitcher 的 setImageDrawable(Drawable drawable)、 setImageResource(int resid)和 setImageURI(Uri uri)方法更换图片即可。

    接下来通过一个简单的示例程序来学习ImageSwitcher 的使用。

    继续使用WidgetSample工程的advancedviewsample模块,首先准备5张图片放在drawable目录下,然后在app/main/res/layout/目录下创建imageswitcher_layout.xml文件,在其中填充如下代码片段:

<?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="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right"/>
</RelativeLayout>

    上面界面布局文件中的粗体字代码定义了一个ImageSwitcher,并通过android:inAnimation 和android:outAnimation指定了图片切换时的动画效果。

    ImageSwitcher的使用一个最重要的地方就是需要为它指定一个ViewFactory,也就是定义它是如何把内容显示出来的,一般做法为在使用ImageSwitcher的该类中实现ViewFactory接口并覆盖对应的makeView方法。新建ImageSwitcherActivity.java文件,加载上面新建的布局文件,具体代码如下:

package com.jinyu.cqkxzsxy.android.advancedviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

/**
 * @创建者 鑫鱻
 * @描述 Android零基础入门到精通系列教程,欢迎关注微信公众号ShareExpert
 */
public class ImageSwitcherActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory {
    private ImageSwitcher mImageSwitcher = null;
    private int[] mImageIds = {
            R.drawable.image_01, R.drawable.image_02, R.drawable.image_03,
            R.drawable.image_04, R.drawable.image_05
    };
    private int mIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageswitcher_layout);

        // 获取界面组件
        mImageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);

        // 为他它指定一个ViewFactory,也就是定义它是如何把内容显示出来的,
        // 实现ViewFactory接口并覆盖对应的makeView方法。
        mImageSwitcher.setFactory(this);
        // 添加动画效果
        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

        // 为ImageSwitcher绑定监听事件
        mImageSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIndex ++;
                if(mIndex >= mImageIds.length){
                    mIndex = 0;
                }
                mImageSwitcher.setImageResource(mImageIds[mIndex]);
            }
        });

        mImageSwitcher.setImageResource(mImageIds[0]);
    }

    @Override
    public View makeView() {
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
        // 设置填充方式
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return imageView;
    }
}

    运行程序,点击ImageSwitcher时可以看到下图所示界面图片切换的效果。

 

 

二、TextSwitcher

 

    TextSwitcher继承了 ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换 View组件时使用动画效果。与ImageSwitcher相似的是,使用TextSwitcher也需要设置一个 ViewFactory。与 ImageSwitcher 不同的是,TextSwitcher 所需的 ViewFactory 的 makeView()方法必须返回一个TextView组件。

    TextSwitcher与TextView的功能有点相似,它们都可用于显示文本内容,区别在于TextSwitcher的效果更炫,它可以指定文本切换时的动画效果。

    接下来通过一个简单的示例程序来学习TextSwitcher的使用。

    继续使用WidgetSample工程的advancedviewsample模块,在app/main/res/layout/目录下创建textwitcher_layout.xml文件,在其中填充如下代码片段:

<?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" >
    <!-- 定义一个TextSwitcher,并指定了文本切换时的动画效果 -->
    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right" />
</LinearLayout>

    上面的界面布局文件中定义了一个TextSwitcher,并指定了文本切换时的动画效果。

    接下来Activity只要为TextSwitcher设置ViewFactory,该TextSwitcher即可正常工作。新建TextSwitcherActivity.java文件,加载上面新建的布局文件,具体代码如下:

package com.jinyu.cqkxzsxy.android.advancedviewsample;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

/**
 * @创建者 鑫鱻
 * @描述 Android零基础入门到精通系列教程,欢迎关注微信公众号ShareExpert
 */
public class TextSwitcherActivity extends AppCompatActivity {
    private TextSwitcher mTextSwitcher = null;
    private String[] mContents = {
            "你好", "HelloWorld", "Good!!!", "TextSwitcher", "你会了吗?"
    };
    private int mIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textwitcher_layout);

        mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
        mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView tv = new TextView(TextSwitcherActivity.this);
                tv.setTextSize(40);
                tv.setTextColor(Color.MAGENTA);
                return tv;
            }
        });

        mTextSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTextSwitcher.setText(mContents[mIndex++ % mContents.length]);
            }
        });

        mTextSwitcher.setText(mContents[0]);
    }
}

    上面的粗体字代码重写了 ViewFactory的makeView() 方法,该方法返回了一个TextView,这样即可让 TextSwitcher正常工作。当程序要切换TextSwitcher显示文本时,调用TextSwitcher的setText()方法修改文本即可。

    运行程序,点击TextSwitcher将会切换显示的文本,同时会出现动画效果,如下图所示。

    至此,关于ImageSwitcher和TextSwitcher组件学习完毕,如果还有不清楚的地方建议回头再多做练习。

 

    今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

   此文章版权为微信公众号分享达人秀(ShareExpert)——鑫鱻所有,若需转载请联系作者授权,特此声明!

 

往期总结分享:

Android零基础入门第1节:Android的前世今生

Android零基础入门第2节:Android 系统架构和应用组件那些事

Android零基础入门第3节:带你一起来聊一聊Android开发环境

Android零基础入门第4节:正确安装和配置JDK, 高富帅养成第一招

Android零基础入门第5节:善用ADT Bundle, 轻松邂逅女神

Android零基础入门第6节:配置优化SDK Manager, 正式约会女神

Android零基础入门第7节:搞定Android模拟器,开启甜蜜之旅

Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点

Android零基础入门第9节:Android应用实战,不懂代码也可以开发

Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌

Android零基础入门第13节:Android Studio配置优化,打造开发利器

Android零基础入门第14节:使用高速Genymotion,跨入火箭时代

Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航

Android零基础入门第16节:Android用户界面开发概述

Android零基础入门第17节:文本框TextView

Android零基础入门第18节:输入框EditText

Android零基础入门第19节:按钮Button

Android零基础入门第20节:复选框CheckBox和单选按钮RadioButton

Android零基础入门第21节:开关组件ToggleButton和Switch

Android零基础入门第22节:图像视图ImageView

Android零基础入门第23节:图像按钮ImageButton和缩放按钮ZoomButton

Android零基础入门第24节:自定义View简单使用,打造属于你的控件

Android零基础入门第25节:简单且最常用的LinearLayout线性布局

Android零基础入门第26节:两种对齐方式,layout_gravity和gravity大不同

Android零基础入门第27节:正确使用padding和margin

Android零基础入门第28节:轻松掌握RelativeLayout相对布局

Android零基础入门第29节:善用TableLayout表格布局

Android零基础入门第30节:两分钟掌握FrameLayout帧布局

Android零基础入门第31节:少用的AbsoluteLayout绝对布局

Android零基础入门第32节:新推出的GridLayout网格布局

Android零基础入门第33节:Android事件处理概述

Android零基础入门第34节:Android中基于监听的事件处理

Android零基础入门第35节:Android中基于回调的事件处理

Android零基础入门第36节:Android系统事件的处理

Android零基础入门第37节:初识ListView

Android零基础入门第38节:初识Adapter

Android零基础入门第39节:ListActivity和自定义列表项

Android零基础入门第40节:自定义ArrayAdapter

Android零基础入门第41节:使用SimpleAdapter

Android零基础入门第42节:自定义BaseAdapter

Android零基础入门第43节:ListView优化和列表首尾使用

Android零基础入门第44节:ListView数据动态更新

Android零基础入门第45节:网格视图GridView

Android零基础入门第46节:列表选项框Spinner

Android零基础入门第47节:自动完成文本框AutoCompleteTextView

Android零基础入门第48节:可折叠列表ExpandableListView

Android零基础入门第49节:AdapterViewFlipper图片轮播

Android零基础入门第50节:StackView卡片堆叠

Android零基础入门第51节:进度条ProgressBar

Android零基础入门第52节:自定义ProgressBar炫酷进度条

Android零基础入门第53节:拖动条SeekBar和星级评分条RatingBar

Android零基础入门第54节:视图切换组件ViewSwitcher

目录
相关文章
|
Android开发 Java 数据格式
|
XML Android开发 数据格式
我的Android进阶之旅------&gt;Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器
一、简介 a.GridView(网络视图)的功能和用法 b.ImageSwitcher(图形切换器)的功能和用法 c.Gallery(画廊视图)的功能和用法 二、通过一个实例来学习Gallery、GridView和I...
1220 0