Android学习笔记之ImageSwitcher 2 上一张,下一张按钮

本文介绍如何使用Android的ImageSwitcher控件创建一个简单的图片轮播应用,通过上一张和下一张按钮切换图片,展示了如何设置图片资源、定义图片显示大小及响应按钮事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

package xiaosi.imageswitcher;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class ImageSwitcherActivity extends Activity implements ViewFactory {  private ImageSwitcher is_imageSwitcher; //存放图片id的int数组 private int[] images={   R.drawable.a,   R.drawable.b,   R.drawable.c,   R.drawable.d,   R.drawable.e,   R.drawable.f,   R.drawable.g,   R.drawable.h,}; //下一张和上一张按钮 private ImageButton next; private ImageButton last; private int index=0;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                is_imageSwitcher=(ImageSwitcher)findViewById(R.id.is_imageswitch);        last=(ImageButton)findViewById(R.id.last);        next=(ImageButton)findViewById(R.id.next);                //imageSwticher必须设置一个viewfactory后才可以查看图片        is_imageSwitcher.setFactory(this);        //设置图片资源id        is_imageSwitcher.setBackgroundResource(images[index]);     }         public View makeView() {        //定义每个图像的显示大小        ImageView imageView = new ImageView(this);        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(300, 300));                return imageView;  }        //上一张的按钮事件    public void onClickLast(View v)    {     if(index == 0){      index = images.length-1;     }     else{      index--;     }  is_imageSwitcher.setBackgroundResource(images[index%images.length]);    }        //下一张的按钮事件    public void onClickNext(View v)    {     index++;  is_imageSwitcher.setBackgroundResource(images[index%images.length]);    }}


 

mian.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"  android:layout_height="fill_parent">      <ImageSwitcher android:id="@+id/is_imageswitch"       android:layout_width="fill_parent"        android:layout_height="fill_parent">   </ImageSwitcher>        <LinearLayout    android:orientation="horizontal"     android:layout_width="fill_parent"  android:layout_height="wrap_content">    <ImageButton        android:src="@drawable/previous"        android:id="@+id/last"        android:layout_width="40dp"     android:layout_height="40dp"      android:layout_marginRight="10dp"     android:onClick="onClickLast"/>   <ImageButton        android:src="@drawable/next"        android:id="@+id/next"        android:layout_width="40dp"      android:layout_height="40dp"       android:onClick="onClickNext"      />    </LinearLayout></FrameLayout>


 

 

源代码下载:点击下载

 

 

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值