65.9K
CodeProject 正在变化。 阅读更多。
Home

Android 图片库应用

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2017 年 6 月 27 日

CPOL

2分钟阅读

viewsIcon

28579

downloadIcon

1570

这是一个在 Android 中演示图片库应用的示例。

 

引言

本文档解释了如何在 Android 中创建我们自己的图片库。Gallery 是一种视图,用于显示水平滚动的图片列表。我们可以指定在 Gallery 中选择图片时执行的操作。例如,我们可以使用 ImageView 控件以更大的尺寸显示所选图片。

背景

在示例应用程序中,我创建了一个包含固定图片的 Gallery。这些图片被复制到 res/drawable 文件夹中。用户可以水平滚动浏览所有图片。单击 Gallery 中的图片会在 ImageView 控件中显示该图片。

Using the Code

res/drawable 文件夹中的图片在 MainActivity.java 文件中如下引用

Integer[] imageIDs =
            {R.drawable.pic1, R.drawable.pic2, 
             R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7};

activity_main.xml 文件中的以下代码创建一个线性布局,其中包含 TextViewGalleryImageView 控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.azim.mygalleryapp.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Image Gallery"/>
    <Gallery
        android:id="@+id/mygallery"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="25dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/myimage"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="25dp"
        android:layout_width="330dp"
        android:layout_height="250dp"
        android:scaleType="fitXY" />
</LinearLayout>

res/values 文件夹中添加一个名为 attrs.xml 的文件,并在其中添加以下代码

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <declare-styleable name="MyGallery">
        <attr name="android:galleryItemBackground"/>
    </declare-styleable>
</resources>

MainActivity.java 文件中检索 Gallery 对象如下

Gallery gallery = (Gallery) findViewById(R.id.mygallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(this);

在上面的代码中,Gallery 类的 setAdapter() 方法用于使用名为 ImageAdapter 的内部类指定 Gallery 的数据和数据格式。setOnItemClickListener() 方法注册回调方法,当在 Gallery 中单击图片时调用该方法。

以下是 ImageAdapter 内部类的代码

   public class ImageAdapter extends BaseAdapter
   {
        Context ctx;
        int itemBackground;
        public ImageAdapter(Context ctx)
        {
            this.ctx = ctx;
            TypedArray array = obtainStyledAttributes(R.styleable.MyGallery);
            itemBackground = array.getResourceId
            (R.styleable.MyGallery_android_galleryItemBackground, 0);
            array.recycle();
        }
        public int getCount()
        {
            return imageIDs.length;
        }
        public Object getItem(int position)
        {
            return position;
        }
        public long getItemId(int position)
        {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView=new ImageView(ctx);
            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150,120));
            imageView.setBackgroundResource(itemBackground);
            return imageView;
        }
    }

在上面的代码中,ImageAdapter 内部类是从 BaseAdapter 类派生的。在该类的构造函数中,使用 obtainStyledAttributes() 方法检索上下文主题中样式的属性信息,并将其存储在 TypedArray 对象中。TypedArray 对象的 recycle() 方法用于供后续调用者重用该对象。getView() 方法返回一个 ImageView,该 ImageView 表示要在 Gallery 中显示的图片,具体取决于位置。setImageResource() 方法将可绘制对象设置为此 ImageView 的内容。setScaleType() 方法指定图像在 ImageView 中的大小和位置。setLayoutParams() 方法指定排列 ImageView 的布局参数。setBackgroundResource() 方法为 ImageView 设置背景。

以下 onItemClick() 方法是在 Gallery 中单击图片时要调用的回调方法,它会在 ImageView 对象中显示所选图片。

public void onItemClick(AdapterView adapterView,View view,int position,long id)
{
    ImageView imageView=(ImageView)findViewById(R.id.myimage);
    imageView.setImageResource(imageIDs[position]);
}

以下是 MainActivity.java 文件的完整代码

package com.example.azim.mygalleryapp;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener
{
    Integer[] imageIDs =
            {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, 
             R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Gallery gallery = (Gallery) findViewById(R.id.mygallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView adapterView,View view,int position,long id)
    {
        ImageView imageView=(ImageView)findViewById(R.id.myimage);
        imageView.setImageResource(imageIDs[position]);
    }

    public class ImageAdapter extends BaseAdapter
    {
        Context ctx;
        int itemBackground;
        public ImageAdapter(Context ctx)
        {
            this.ctx = ctx;
            TypedArray array = obtainStyledAttributes(R.styleable.MyGallery);
            itemBackground = array.getResourceId
                             (R.styleable.MyGallery_android_galleryItemBackground, 0);
            array.recycle();
        }
        public int getCount()
        {
            return imageIDs.length;
        }
        public Object getItem(int position)
        {
            return position;
        }
        public long getItemId(int position)
        {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView=new ImageView(ctx);
            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150,120));
            imageView.setBackgroundResource(itemBackground);
            return imageView;
        }
    }
}

以下是在实际 Android 移动设备上运行该应用程序的输出

关注点

我希望本文档对读者理解如何在 Android 中轻松创建图片库应用程序有所帮助。

© . All rights reserved.