android 组件ImageSwitch实例

来源:互联网 发布:开源crm java 源码 编辑:程序博客网 时间:2024/06/16 00:07

      本例子是对ImageSwitch的练习。通过两个按钮“上一张”、“下一张”达到对4张图片切换的效果。还附加了动画效果哦~


布局文件:imageswitch_layout.xml


<?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"
    android:orientation="vertical" >


    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="200dp" >
    </ImageSwitcher>



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


        <Button
            android:id="@+id/btn_last"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="上一张" />


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


</LinearLayout>


AtyImageSwitch.java文件:


package com.fxj.composit;


import com.fxj.compractice.R;


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 AtyImageSwitch extends Activity implements OnClickListener {
private ImageSwitcher imageSwitcher;
private Button btnLast;
private Button btnNext;
// 图片资源
private int[] images = new int[] { R.drawable.jay01, R.drawable.jay02,
R.drawable.jay03, R.drawable.jay04 };
// 当前图片id
private int currentImageId = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitch_layout);
// 实例化控件
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
btnLast = (Button) findViewById(R.id.btn_last);
btnNext = (Button) findViewById(R.id.btn_next);
// 为两个按钮设置监听
btnLast.setOnClickListener(this);
btnNext.setOnClickListener(this);
// 为imageSwitcher设置工厂,每设置一张图片需要一个ImageView来承载这个图片
imageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
// 生产出的是ImageView
return new ImageView(AtyImageSwitch.this);
}
});

// 设置首次显示的图片
imageSwitcher.setImageResource(images[currentImageId]);
// 设置图片切换的动画
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
AtyImageSwitch.this, android.R.anim.slide_in_left));// 从左滑入
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
AtyImageSwitch.this, android.R.anim.slide_out_right));// 从又滑出

}


// 按钮监听事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_last:// 上一张,如果是第0张,则切换到最后一张,循环显示
currentImageId--;
if (currentImageId == -1) {
currentImageId = 3;
}
imageSwitcher.setImageResource(images[currentImageId]);
break;
case R.id.btn_next:// 下一张,如果是最后一张,则切换到第0张,循环显示
currentImageId++;
if (currentImageId == 4) {
currentImageId = 0;
}
imageSwitcher.setImageResource(images[currentImageId]);
break;
}
}
}


运行效果:





动画效果





                                                                                   动画效果



这些截图就足以说明效果了。

结束。

0 0
原创粉丝点击