前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ImageSwitcher和TextSwitcher

ImageSwitcher和TextSwitcher

作者头像
分享达人秀
发布2018-02-02 18:05:47
6740
发布2018-02-02 18:05:47
举报
文章被收录于专栏:分享达人秀分享达人秀

上一期我们了解了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文件,在其中填充如下代码片段:

代码语言:javascript
复制
<?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文件,加载上面新建的布局文件,具体代码如下:

代码语言:javascript
复制
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文件,在其中填充如下代码片段:

代码语言:javascript
复制
<?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文件,加载上面新建的布局文件,具体代码如下:

代码语言:javascript
复制
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组件学习完毕,如果还有不清楚的地方建议回头再多做练习。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档