Android UI控件系列:Gallery(画廊视图)

2024-09-01 20:58

本文主要是介绍Android UI控件系列:Gallery(画廊视图),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Gallery能够水平显示其内容,一般用来浏览图片,被选中的选项位于中间,并且可以相应事件显示信息。下面结合ImageSwitcher组件来实现一个通过缩略图来浏览图片的程序,具体步骤如下

第一步:

创建一个Andorid工程”GalleryTest”,该工程的入口是Activity类GalleryTest继承Activity并实现OnItemSelectedListener和ViewFactory接口,来实现图片和视图的创建

package org.hualang.Gallery;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;
//继承Activity,实现onItemSelectedListener和ViewFactory接口
public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}@Overridepublic View makeView() {// TODO Auto-generated method stubreturn null;}@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}
}

第二步:

在工程的res\drawable\目录下添加7张图片和对应的缩略图

第三步:

在工程res\layout\目录下创建一个布局文件main.xml,在其中那个添加一个Gallery组件和一个ImageSwitcher组件,并设置相应的属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<ImageSwitcher android:id="@+id/switcher"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"/><Gallery android:id="@+id/gallery"android:background="#55000000"android:layout_width="fill_parent"android:layout_height="60dp"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:gravity="center_vertical"android:spacing="16dp"/>
</LinearLayout>

第四步:在GalleryTest顶部声明使用到的ImageSwitcher实例图片资源Integer数组

public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{/** Called when the activity is first created. *///声明ImageSwitcherprivate ImageSwitcher switcher;//缩略图片id数组private Integer[] thumbids={R.drawable.thumb0,R.drawable.thumb1,R.drawable.thumb2,R.drawable.thumb3,R.drawable.thumb4,R.drawable.thumb5,R.drawable.thumb6,R.drawable.thumb7};//图片id数组private Integer[] imgids={R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,R.drawable.img7};

第五步:

在GalleryTest的onCreate()方法中,将窗口样式设置为无标题,设置当前布局视图,获得ImageSwitcher实例,并设置渐进渐出动画,获得Gallery实例

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置窗口特征无标题requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);//通过findViewById方法获得ImageSwitcher对象switcher=(ImageSwitcher)findViewById(R.id.switcher);//为ImageSwitcher设置工厂switcher.setFactory(this);//设置动画渐入效果switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置动画渐出效果switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//通过findViewById方法获得Gallery对象Gallery g=(Gallery)findViewById(R.id.gallery);}

第六步:

创建内部类ImageAdapter,该类继承BaseAdapter,为Gallery设置Adapter实例

public class ImageAdapter extends BaseAdapter {//构造方法public ImageAdapter(Context c) {mContext = c;}//获得数量public int getCount() {return thumbids.length;}//获得当前选项public Object getItem(int position) {return position;}//获得当前选项IDpublic long getItemId(int position) {return position;}//获得View对象public View getView(int position, View convertView, ViewGroup parent) {//实例化ImageView对象ImageView i = new ImageView(mContext);//设置缩略图片资源i.setImageResource(thumbids[position]);//设置边界对齐i.setAdjustViewBounds(true);//设置布局参数i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));//设置背景资源i.setBackgroundResource(R.drawable.picturefrom);return i;}private Context mContext;}

第七步:

实现onItemSelected()方法,更换图片

@Overridepublic void onItemSelected(AdapterView<?> adapter, View v, int position,long id) {switcher.setImageResource(imgids[position]);}

第八步:

实现makeView()方法,为ImageView设置布局格式

@Overridepublic View makeView() {// TODO Auto-generated method stub//创建ImageViewImageView i=new ImageView(this);//设置背景颜色i.setBackgroundColor(0xFF000000);//设置精度类型i.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置布局参数i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));return i;}

第九步:

为Gallery添加Adapter并添加OnItemSelectedListener监听器

g.setAdapter(new ImageAdapter(this));g.setOnItemSelectedListener(this);

至此,全部,结束,运行结果如下

完整源代码:

package org.hualang.Gallery;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory {private ImageSwitcher mSwitcher;private Integer[] mThumbIds = { R.drawable.thumb0,R.drawable.thumb1, R.drawable.thumb2,R.drawable.thumb3, R.drawable.thumb4,R.drawable.thumb5, R.drawable.thumb6,R.drawable.thumb7 };private Integer[] mImageIds = { R.drawable.img0, R.drawable.img1,R.drawable.img2, R.drawable.img3, R.drawable.img4,R.drawable.img5, R.drawable.img6, R.drawable.img7 };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);mSwitcher.setFactory(this);mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));Gallery g = (Gallery) findViewById(R.id.gallery);g.setAdapter(new ImageAdapter(this));g.setOnItemSelectedListener(this);}public class ImageAdapter extends BaseAdapter {public ImageAdapter(Context c) {mContext = c;}public int getCount() {return mThumbIds.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 i = new ImageView(mContext);i.setImageResource(mThumbIds[position]);i.setAdjustViewBounds(true);i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));i.setBackgroundResource(R.drawable.picturefrom);return i;}private Context mContext;}@Overridepublic void onItemSelected(AdapterView<?> adapter, View v, int position,long id) {mSwitcher.setImageResource(mImageIds[position]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}@Overridepublic View makeView() {ImageView i = new ImageView(this);i.setBackgroundColor(0xFF000000);i.setScaleType(ImageView.ScaleType.FIT_CENTER);i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return i;}
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent">
<ImageSwitcher android:id="@+id/switcher"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"/>
<Gallery android:id="@+id/gallery"android:background="#55000000"android:layout_width="fill_parent"android:layout_height="60dp"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:gravity="center_vertical"android:spacing="16dp"/>
</RelativeLayout>

这篇关于Android UI控件系列:Gallery(画廊视图)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128024

相关文章

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的