ImageSwitcher 图片阅览

1、简介

ImageSwitcher 用来进行图片的 切换预览
在这里插入图片描述

2、功能实现
  1. 文件结构
    在这里插入图片描述

2)activity_main.xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal" >

    <ImageSwitcher
        android:id="@+id/imageswitcher"
        android:layout_width="300dip"
        android:layout_height="200dip" >
    </ImageSwitcher>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageswitcher"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一张" />

        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张" />
    </LinearLayout>

</RelativeLayout>

3)anim 文件定义的动画
lefe_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="600"
        />
    <alpha 
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="600"
        />
</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="600"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

    <alpha
        android:duration="600"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />
</set>

reight_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="600"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="600"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />
</set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="600"
        />
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="600"
        />
</set>
  1. mainActivity.java
package myapplication21.lum.com.mytestview;

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

public class MainActivity extends Activity implements OnClickListener,
		ViewFactory {
	private ImageSwitcher imageSwitcher;
	private Button btn_next;
	private Button btn_previous;
	// 获取图片资源
	private int[] images = { R.drawable.image1, R.drawable.image2,
			R.drawable.image3, R.drawable.image4, R.drawable.image5 };
	private int index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);

		imageSwitcher.setFactory(this);
		btn_next = (Button) findViewById(R.id.btn_next);
		btn_previous = (Button) findViewById(R.id.btn_previous);

		btn_next.setOnClickListener(this);
		btn_previous.setOnClickListener(this);
		imageSwitcher.setImageResource(images[0]);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_next:
			imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.right_in));
			imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.left_out));
			index++;
			if (index >= images.length) {
				index = images.length - 1;
				return;
			}
			imageSwitcher.setImageResource(images[index]);
			break;
		case R.id.btn_previous:
			imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.left_in));
			imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.right_out));
			index--;
			if (index < 0) {
				index = 0;
				return;
			}
			imageSwitcher.setImageResource(images[index]);
			break;
		}
	}

	@Override
	public View makeView() {
		ImageView imageView = new ImageView(this);
		return imageView;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值