• Android ImageSwitcher和Gallery:图片切换器和图库

    在使用 Android 手机设置壁纸的时候,会看到屏幕底部有很多可以滚动的图片,当单击某一图片时,在其上面的空间会显示当前选中的图片,此时我们用到的就是 Gallery(图库)和 ImageSwitcher(图片切换器)。

    Gallery 组件用于横向显示图像列表,并且自动将当前图像放置到中间位置。

    ImageSwitcher 则像是图片浏览器,可以切换图片,通过它可以制作简单的幻灯片等。通常将这两个类结合在一起使用,可以制作有一定效果的相册。

    下面通过一个实例来了解一下这两个组件的使用方法。

    在工程 WidgetDemo 的布局文件 main.xml 中添加一个名为 GalleryDemo 的 Button,用以启动 GalleryActivity。在 main.xml 中添加代码如下:

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GalleryDemo"/>

    单击 Button 并启动 GalleryActivity 的代码如下:

    Button gallerybtn = (Button)this.findViewById(R.id.button11);
    gallerybtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent;
            intent = new Intent(MainActivity.this, GalleryActivity.class);
            startActivity(intent);
        }
    });

    同时在 AndroidManifest.xml 文件中声明该 Activity:

    <activity android:name=".GalleryActivity"/>

    GalleryActivity 的运行效果如图 1 所示。

    GalleryActivity的运行效果
    图 1  GalleryActivity 的运行效果

更多...

加载中...