android圆角视图_Android图库视图示例教程

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 which ListView 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.

android gallery view example, android gallery example

下面的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。 支持库中的替代方法包括Horizo​​ntalScrollViewViewPager 。 最好的替代方法是在片段布局中将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圆角视图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值