Android 简单图片浏览器

最后成果图:

Android 简单图片浏览器_第1张图片

第一步:编辑XML布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <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:id="@+id/preBtn"
            android:text="上一张"/>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/nextBtn"
            android:text="下一张"/>
    LinearLayout>

    <ImageView 
        android:id="@+id/image01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image03"
        android:scaleType="fitCentr"/>

LinearLayout>

第二步:在drawable中添加几个图片:我的image01.jpg,image02.jpg,imge03.jpg

第二步:编辑avtivity文件:

package com.exp.helloword;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    Button preBtn,nextBtn;
    ImageView image01;
    //定义一个访问图片的数组
    int[] images = new int[]{
            R.drawable.image01,
            R.drawable.image02,
            R.drawable.image03
    };
    int currentImg = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.img_view);
        preBtn = (Button) findViewById(R.id.preBtn);
        nextBtn = (Button) findViewById(R.id.nextBtn);
        image01 = (ImageView) findViewById(R.id.image01);
        //上一张按钮 按钮 事件监听
        preBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                image01.setImageResource(images[--currentImg % images.length]);             
            }
        });
      //下一张按钮 按钮 事件监听
        nextBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //控制ImageView显示下一张图片
                image01.setImageResource(images[++currentImg % images.length]);
            }
        });
    }

}

你可能感兴趣的:(android,android,图片浏览器)