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 whichListView
items were populated from an AdapterGallery的项目是从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 :
画廊的布局定义如下:
<Galleryandroid: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"><Galleryandroid:id="@+id/gallery"android:layout_width="fill_parent"android:layout_height="wrap_content" /><ImageViewandroid: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;@Overridepublic 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 ImageselectedImage.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 needpublic View getView(int index, View view, ViewGroup viewGroup){// TODO Auto-generated method stubImageView 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圆角视图
android圆角视图_Android图库视图示例教程相关推荐
- android 导航抽屉_Android导航抽屉示例教程
android 导航抽屉 In this tutorial we'll implement a Navigation Drawer in our android application. Androi ...
- android浮动按钮_Android浮动操作按钮示例教程
android浮动按钮 Today we will learn about Android Floating Action Button. We'll discuss the FloatingActi ...
- android实例教程_Android内部存储示例教程
android实例教程 Today we will look into android internal storage. Android offers a few structured ways t ...
- 免费下载谷歌maps软件_Android Google Maps示例教程
免费下载谷歌maps软件 In this tutorial we'll discuss and implement some interesting features of android googl ...
- android实例教程_Android ConstraintLayout示例教程
android实例教程 In this tutorial, we'll discuss the intricacies of android ConstraintLayout. Google had ...
- android dialog 隐藏状态栏_Android应用视图的管理者Window
点击上方蓝色文字关注我哦 Window在Android是一个窗口的概念,日常开发中我们和它接触的不多,我们更多接触的是View,但是View都是通过Window来呈现的,Window是View的直接管 ...
- android int 首位值_Android应用视图的管理者Window
点击上方蓝色文字关注我哦 Window在Android是一个窗口的概念,日常开发中我们和它接触的不多,我们更多接触的是View,但是View都是通过Window来呈现的,Window是View的直接管 ...
- iOS10 UI教程子视图和父视图UI层次结构和Views继承
iOS10 UI教程子视图和父视图UI层次结构和Views继承 iOS10 UI教程子视图和父视图UI层次结构和Views继承,本节将讲解与UI层次结构和Views继承相关的内容,其中包括子视图和父视 ...
- Android ListView示例教程
We will learn how to create a simple Android ListView and launch a new activity on selecting a singl ...
最新文章
- php安卓传输图片到mysql_php – Android应用程序将图像发送到MySQL
- PIE SDK图层树伙伴控件示例
- mysql基本操作二
- 2020 年值得再读一遍的网易云信技术干货 | 上篇
- web服务器是如何维护,我们如何维护Web客户端和Web服务器之间的会话?
- 为什么需要做归一化或者标准化
- log4j mysql_log4j写入mysql数据库 | 学步园
- 软件架构(10)---java资深架构师分布式技术分享
- usb audio -- 异步方式介绍(1)
- 从其他项目中复制过来的mapper加载不进bean_手把手带你玩转k8s-一键部署springboot项目...
- 基于OMAP-L138 DSP+ARM处理器与FPGA实现SDR软件无线电系统
- fanuc机器人与示教器配对_FANUC机器人示教器维修
- css3 - 图标元素动画效果1 - 只执行一次动画
- 直线回归和相关------(四)直线相关系数和决定系数(原理与公式推导)
- 姿态估计之CPN(Cascaded Pyramid Network)
- 重构改善即有代码的设计
- C# 企业微信:开启消息接受接收消息推送消息
- AI 隐身术,让你在视频中消失的“黑魔法”,想拥有吗?
- FAST-LIO公式推导
- 深入理解取整、取余与取模问题