17.ImageSwitcher图像切换器的使用实例:查看图片

描述

在该实例中,提供一个图片切换器和两个点击按钮,用于切换图片,并用一个TextView显示图片信息。其中,当前图片若为最后一张,点击下一张,则跳转到第一张;同理,第一张图片点击上一张,则显示最后一张图片,循环查看当前图片。
目标效果图如下所示:
这里写图片描述
这里写图片描述
这里写图片描述

页面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg67"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="我是当前图片的信息~"
        android:textSize="24dp" />


    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageSwitcher 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_gravity="center"
            android:background="#666666">
        </ImageSwitcher>

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">

            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上一张"
                android:layout_marginLeft="20dp"
                android:textSize="24dp"
                android:id="@+id/up" />

            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下一张"
                android:layout_marginLeft="20dp"
                android:textSize="24dp"
                android:id="@+id/down" />

        </LinearLayout>

    </LinearLayout>



</LinearLayout>

事件响应

package com.example.imageswitchdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity
{
    TextView show=null;
    Button up,dowm=null;
    ImageSwitcher image=null;
    private int[] images=new int[]{R.drawable.a001,R.drawable.a002,R.drawable.a003,
                                    R.drawable.a004,R.drawable.a005,R.drawable.a006,
                                    R.drawable.a007,R.drawable.a008,R.drawable.a009};
    private int index=0;

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

        //获取控件
        show=(TextView) findViewById(R.id.show);
        up=(Button) findViewById(R.id.up);
        dowm=(Button) findViewById(R.id.down);
        image=(ImageSwitcher) findViewById(R.id.image);

        //为获取到的控件添加显示效果:淡入动画和淡出动画
        image.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        image.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

        //为图像切换器设置一个ViewFactory,并重写makeView方法
        image.setFactory(new ViewFactory()
        {

            @Override
            public View makeView()
            {
                //指定视图切换工程
                return new ImageView(MainActivity.this);
            }
        });
        image.setImageResource(images[index]);
        show.setText("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");

        //当点击按钮时,图像切换并显示相应的信息
        up.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                if(index>0)
                    index--;
                else
                    index=images.length-1;

                image.setImageResource(images[index]);
                show.setText("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");
            }
        });

        //同理,当点击按钮时,图像切换并显示相应的信息
        dowm.setOnClickListener(new OnClickListener()
        {
            public void onClick(View arg0)
            {
                if(index<images.length-1)
                    index++;
                else
                    index=0;

                image.setImageResource(images[index]);
                show.setText("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值