android圆角视图
Android Gallery is a View commonly used to display items in a horizontally scrolling list that locks the current selection at the center. In this tutorial we’ll display a horizontal list of images and when a user clicks an image, it will be displayed in the center of the screen.
Android Gallery是一种视图,通常用于在水平滚动列表中显示项目,从而将当前选择锁定在中心。 在本教程中,我们将显示图像的水平列表,当用户单击图像时,它将显示在屏幕中央。
Android Gallery视图概述 (Android Gallery View Overview)
- The items of Gallery are populated from an Adapter, similar to
ListView
, in whichListView
items were populated from an Adapter Gallery的项目是从Adapter填充的,类似于ListView
,其中ListView
项目是从Adapter填充的 - We need to create an Adapter class which extends
BaseAdapter
class and override getView() method 我们需要创建一个Adapter类,该类扩展BaseAdapter
类并重写getView()方法。 - getView() method called automatically for all items of Gallery 对Gallery的所有项目自动调用的getView()方法
The layout for the Gallery is defined as follows :
画廊的布局定义如下:
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
It belongs to android.widget.Gallery class. However this class is deprecated now.
它属于android.widget.Gallery类。 但是,现在不推荐使用该类。
项目结构 (Project Structure)
码 (Code)
The layout of the MainActivity
is given below:
MainActivity
的布局如下:
main.xml
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>
The android:src points to the first image from the left in the gallery.
android:src指向图库左侧的第一张图片。
The MainActivity.java
is given below:
MainActivity.java
如下所示:
package com.journaldev.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]);
}
});
}
}
We need to create the GalleryImageAdapter class which extends the BaseAdapter class. This will bind to the Gallery view with a series of ImageView views. The BaseAdapter class will work as a bridge between an AdapterView and also the data source that feeds data into it.
我们需要创建GalleryImageAdapter类,该类扩展了BaseAdapter类。 这将绑定到带有一系列ImageView视图的Gallery视图。 BaseAdapter类将充当AdapterView和将数据馈入其中的数据源之间的桥梁。
For the GalleryImageAdapter class, following methods are implemented:
对于GalleryImageAdapter类,实现以下方法:
- getCount() getCount()
- getItem() getItem()
- getItemId() getItemId()
- getView() getView()
The GalleryImageAdapter class is given below:
GalleryImageAdapter类如下所示:
package com.journaldev.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
};
}
The GIF below depict the output of the project. They display the ImageView with the image of the corresponding thumbnail from the GalleryView.
下面的GIF描述了该项目的输出。 它们显示ImageView以及来自GalleryView的相应缩略图的图像。
Note: GalleryView is deprecated now. The alternatives include HorizontalScrollView and ViewPager from the support library. The best alternative way is to use ViewPager with an ImageView in its fragment layout.
注意:现已弃用GalleryView。 支持库中的替代方法包括HorizontalScrollView和ViewPager 。 最好的替代方法是在片段布局中将ViewPager与ImageView一起使用。
This brings an end to this tutorial. You can download the final Android GalleryView Project from the below link:
本教程到此结束。 您可以从以下链接下载最终的Android GalleryView项目:
翻译自: https://www.journaldev.com/9546/android-gallery-view-example-tutorial
android圆角视图