ImageView相册案例

本文介绍了一个简单的Android应用案例,该应用通过两个按钮实现图片的前后切换功能。文章提供了完整的代码实现,包括图片资源的定义、按钮点击事件的响应及布局文件的设计。

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

package com.bw.yztc_imageview;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

/**
* 相册
*/

public class MainActivity extends Activity {
// 存储图片资源id的数组容器
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, R.drawable.i, R.drawable.j };
private ImageView lv;
private int index;// 数组的下标

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ImageView) findViewById(R.id.lv);
}

// 点击按钮切换图片显示
public void click(View v) {
    switch (v.getId()) {
    case R.id.btn_last:// 点击上一张
        index--;
        break;
    case R.id.btn_next:// 点击下一张
        index++;
        break;
    }
    if (index < 0) {
        index = 0;
    }
    if (index > images.length - 1) {
        index = images.length - 1;
    }
    // 确定当前展示的图片的资源id
    lv.setImageResource(images[index]);// 根据参数指定的图片资源id动态展示对应图片
    }
}

布局

<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" >

<ImageView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/a" />

<Button
    android:id="@+id/btn_last"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_margin="15dp"
    android:background="@null"
    android:onClick="click"
    android:text="上一张" />

<Button
    android:id="@+id/btn_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="15dp"
    android:background="@null"
    android:onClick="click"
    android:text="下一张" />

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值