Gallerty组件一般用于显示图像列表,Gallery只能水平显示一行,而且支持水平滑动效果。单击、选中或拖动Gallerty中的图像,Gallery中的图像列表会根据不同的情况向左或向右移动,直到显示到最后一个图像为止。

package com.example.androidtest;import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;public class MainActivity extends Activity {Integer[] imageIDs = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Gallery gallery = (Gallery)findViewById(R.id.gallery1);gallery.setAdapter(new ImageAdapter(this));gallery.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View v, int position, long id){Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", Toast.LENGTH_SHORT).show();ImageView imageView = (ImageView)findViewById(R.id.image1);imageView.setImageResource(imageIDs[position]);}});}   public class ImageAdapter extends BaseAdapter{private Context context;private int itemBackground;public ImageAdapter(Context c){context = c;TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);a.recycle();}public int getCount(){return imageIDs.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 imageView = new ImageView(context);imageView.setImageResource(imageIDs[position]);imageView.setScaleType(ImageView.ScaleType.FIT_XY);imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));imageView.setBackgroundResource(itemBackground);return imageView;}}
}

在res/values下添加styleable.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<resources> <declare-styleable name="Gallery1"> <attr name="android:galleryItemBackground" /> </declare-styleable>
</resources>

activity_main.xml:

<?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"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Images of San Francisco"/><Galleryandroid:id="@+id/gallery1"android:layout_width="fill_parent"android:layout_height="wrap_content"/><ImageViewandroid:id="@+id/image1"android:layout_width="320px"android:layout_height="250px"android:scaleType="fitXY"/></LinearLayout>

Gallery的使用(一)相关推荐

  1. android 按住拖动gallery防止马上加载数据导致gallery卡的方法

    gallery菜单滑动有一个不好的效果就是每次经过中间的菜单都默认是被选中状态,同时会加载数据 以至于切换不流畅,有一种卡卡的感觉!!其实用线程来处理这个问题,一定的时间后如果选择的index值不变, ...

  2. 第四篇 Gallery控件

    直奔主题~! 结构如图: main.xml代码: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  3. [android] 从gallery获取图片

    效果就是点击按钮,打开系统图库应用,可以选择一张里面的图片展示出来 设置隐式意图 获取Intent对象,通过new出来 调用Intent对象的setAction()方法,设置动作,参数:Intent. ...

  4. NextGEN Gallery ~ 最强WordPress相册插件

    博客照片很多?上传和管理图片太烦?想幻灯显示相册?在博客中任意插入动态图片效果?-- 你和我一样,需要NextGEN Gallery,最强WordPress相册插件! 其实网上可以搜到不少关于这个插件 ...

  5. android 图片列表,Android 列表使用(ListView GridView Gallery图片计时滚动)

    ListView 作用: 1.将数据填充到布局. 2.处理用户的选择点击等操作. 根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAd ...

  6. Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用...

    前言 我们现在在随便一个手机上用手指在屏幕上滑来滑都可以去拖动图片,其实在Android里这很简单,下面我就给大家具体讲解一下. 思路   我们首先需要Gallery这个对象,俗称画廊对象,大家都知道 ...

  7. Android gallery详解

    为什么80%的码农都做不了架构师?>>>    继承关系 public class Gallery extends AbsSpinner implements GestureDete ...

  8. Gallery 之滑动速度的问题

    2019独角兽企业重金招聘Python工程师标准>>> 用Gallery展示图片,遇到一个问题,就是滑动太快,每次轻轻一拨图片,就滑动过去几张,怎么解决呢? 1.直接继承Gralle ...

  9. Android Gallery组件详解(转)

    http://www.eoeandroid.com/forum.php?mod=viewthread&tid=182297&reltid=39709&pre_thread_id ...

  10. 【 MATLAB 】gallery 中的 uniformdata

    Test matrices gallery是一个产生测试矩阵的函数.下面讲解其语法以及用法描述,数据手册上的英文很简单,耐心看,我就不一一翻译了. Syntax [A,B,C,...] = galle ...

最新文章

  1. Eclipse安装软件长时间停留在calculating requirements and dependencies
  2. hdu3951-(Coin Game)
  3. 【渝粤题库】陕西师范大学202101 公共政策学
  4. 数风·数林 | 炉石传说中的概率(声控篇)
  5. 团队文化之舒适区和挑战区
  6. C-Free 5.0最新注册码
  7. wifi信号衰减与距离关系_无线传输距离计算公式
  8. 如何解决代理模式详解的原理详解
  9. Deep-Learned Regularization and Proximal Operator for Image Compressive Sensing
  10. 2013年新会计准则常用会计科目表经典注释整理【最全】
  11. 王道数据结构课代表 - 考研数据结构 第五章 树和二叉树 究极精华总结笔记
  12. 安装keepalive错误:configure: error: No SO_MARK declaration in headers
  13. 插装式比例插装阀比例节流阀比例顺序阀放大器
  14. includes()的用法
  15. 《詹姆斯·高斯林Java白皮书1996自译》07:多线程
  16. 【0】开始学习安卓了
  17. 手把手教你应用三种工厂模式在SpringIOC中创建对象实例【案例详解】
  18. win10怎么取消登录密码
  19. 【附源码】计算机毕业设计JAVA重庆美食网站
  20. 什么是docker及docker的基础命令

热门文章

  1. C#编写最小化时隐藏为任务栏图标的Window appllication
  2. [缓存]迅雷(XUNLEI)的工作原理揭密
  3. Matchme php script_apache php-fpm Primary script unknown\n - TechBlog
  4. python有趣小程序 表白-抖音最火的整蛊表白小程序如何做出来的?教你用python做出...
  5. python3.5下载-Python3.5.2
  6. python如何调用图片-python实现读取并显示图片的两种方法
  7. python好找工作吗2017-你为什么不来了解一下Python?
  8. python自学什么书比较好-19年学习Python有什么好的书籍推荐吗?
  9. python能做表格吗-零基础小白怎么用Python做表格?
  10. python代码画图案-python如何绘制iPhone手机图案?(代码示例)