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 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

C#实现WinForm控件焦点的获取与失去

《C#实现WinForm控件焦点的获取与失去》在一个数据输入表单中,当用户从一个文本框切换到另一个文本框时,需要准确地判断焦点的转移,以便进行数据验证、提示信息显示等操作,本文将探讨Winform控件... 目录前言获取焦点改变TabIndex属性值调用Focus方法失去焦点总结最后前言在一个数据输入表单

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用

element-ui下拉输入框+resetFields无法回显的问题解决

《element-ui下拉输入框+resetFields无法回显的问题解决》本文主要介绍了在使用ElementUI的下拉输入框时,点击重置按钮后输入框无法回显数据的问题,具有一定的参考价值,感兴趣的... 目录描述原因问题重现解决方案方法一方法二总结描述第一次进入页面,不做任何操作,点击重置按钮,再进行下

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert