Android图片长廊视图示例教程
时间:2020-02-23 14:28:56 来源:igfitidea点击:
Android Gallery是一种视图,通常用于在水平滚动列表中显示项目,从而将当前选择锁定在中心。
在本教程中,我们将显示水平的图像列表,当用户单击图像时,图像将显示在屏幕中央。
Android Gallery视图概述
Gallery的项目是从适配器填充的,类似于ListView,其中ListView的项目是从适配器填充的
我们需要创建一个Adapter类,它扩展了BaseAdapter类并覆盖getView()方法。
对Gallery的所有项目自动调用的getView()方法
画廊的布局定义如下:
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
它属于android.widget.Gallery类。
但是,现在不推荐使用该类。
代码
MainActivity的布局如下:
main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<ImageView
android:id="@+id/imageView"
android:layout_marginTop="100dp"
android:layout_width="250dp"
android:layout_gravity="center_horizontal"
android:layout_height="250dp"
android:src="@drawable/alarm"
</LinearLayout>
android:src指向图库左侧的第一张图片。
MainActivity.java如下:
package com.theitroad.galleryview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView selectedImage;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
selectedImage=(ImageView)findViewById(R.id.imageView);
gallery.setSpacing(1);
final GalleryImageAdapter galleryImageAdapter= new GalleryImageAdapter(this);
gallery.setAdapter(galleryImageAdapter);
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//show the selected Image
selectedImage.setImageResource(galleryImageAdapter.mImageIds[position]);
}
});
}
}
我们需要创建GalleryImageAdapter类,该类扩展了BaseAdapter类。
这将绑定到带有一系列ImageView视图的Gallery视图。
BaseAdapter类将充当AdapterView和将数据馈入其中的数据源之间的桥梁。
对于GalleryImageAdapter类,实现以下方法:
- getCount()
- getItem()
- getItemId()
- getView()
GalleryImageAdapter类如下所示:
package com.theitroad.galleryview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryImageAdapter extends BaseAdapter
{
private Context mContext;
public GalleryImageAdapter(Context context)
{
mContext = context;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//Override this method according to your need
public View getView(int index, View view, ViewGroup viewGroup)
{
//TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[index]);
i.setLayoutParams(new Gallery.LayoutParams(200, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
public Integer[] mImageIds = {
R.drawable.alarm,
R.drawable.explore,
R.drawable.language,
R.drawable.lock,
R.drawable.print,
R.drawable.rotation_3d,
R.drawable.spellcheck,
R.drawable.redeem
};
}
下面的GIF描述了该项目的输出。
它们显示ImageView以及来自GalleryView的相应缩略图的图像。
注意:现已弃用GalleryView。
支持库中的替代方法包括HorizontalScrollView和ViewPager。
最好的替代方法是在片段布局中将ViewPager与ImageView一起使用。

