Android 图像切换器

17 Mar 2025 | 阅读 2 分钟

Android 图像切换器提供图像动画,用于从一个图像过渡到另一个图像。为了使用图像切换器,我们需要在 .xml 文件中实现 ImageSwitcher 组件。

ImageSwitcher 的 setFactory() 方法提供了 ViewFactory 接口的实现。ViewFactory 接口实现了其未实现的方法并返回一个 ImageView。

图像切换器示例

让我们实现一个图像切换器。

在 layout 文件夹中创建 activity_main.xml 和 content_main.xml 文件。

将一些要切换的图像放在 drawable 文件夹中。

activity_main.xml

文件:activity_main.xml

content_main.xml

文件:content_main.xml

Activity 类

文件:MainActivity.java

输出

android Image Switcher 1
android Image Switcher 2
下一个主题Android 图像滑块