Android 之 Gallery
1 在 xml 布局中添加 Gallery
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2 自定义 ImageAdapter
package com.example.gallery;
import java.util.List;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class ImageAdapter extends BaseAdapter { private Context context;
private List<Integer> list;
private TypedArray typedArray;
private int item_background; public ImageAdapter(Context context ,List<Integer> list)
{
this.context=context;
this.list=list;
this.typedArray = context.obtainStyledAttributes(R.styleable.gallery_style);
item_background=typedArray.getResourceId(R.styleable.gallery_style_android_galleryItemBackground, 0);
typedArray.recycle();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//设置显示的图片
imageView.setImageResource(list.get(position)); //设置伸缩规格
imageView.setScaleType(ImageView.ScaleType.FIT_XY); //设置布局参数
imageView.setLayoutParams(new Gallery.LayoutParams(150,100)); //设置背景边框
imageView.setBackgroundResource(item_background); return imageView;
}
}
3 每个 ImageView 的背景参数
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="gallery_style">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
4 在 MainActivity 中绑定数据与设置监听
package com.example.gallery;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery=(Gallery)findViewById(R.id.gallery); ArrayList<Integer>list=new ArrayList<Integer>();
list.add(R.drawable.img1);
list.add(R.drawable.img2);
list.add(R.drawable.img3);
list.add(R.drawable.img4);
list.add(R.drawable.img5);
list.add(R.drawable.img6);
list.add(R.drawable.img7); ImageAdapter adapter=new ImageAdapter(this,list);
gallery.setAdapter(adapter); gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,int position, long id) {
Toast.makeText(getApplicationContext(), "选择了: "+
String.valueOf(position), Toast.LENGTH_SHORT).show();
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
//这里不做响应
}
});
}
}
5 图片资源

6 结果展示

Android 之 Gallery的更多相关文章
- android 32 Gallery:横着滚动的列表
Gallery:横着滚动的列表 mainActivity.java package com.sxt.day05_01; import java.util.ArrayList; import java. ...
- Android 解决Gallery下ScrollView滑动事件冲突
在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...
- android学习Gallery和ImageSwitch的使用
Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了, * @deprec ...
- Android之Gallery和Spinner-Android学习之旅(二十九)
Spinner简介 spinner是竖直方向展开一个列表供选择.和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可 ...
- android学习---Gallery画廊视图
Gallery与Spinner有共同父类:AbsPinner.说明Gallery与Spinner都是一个列表框. 它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是 ...
- Android在Gallery中每次滑动只显示一页
import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; impo ...
- android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器
为了使图片浏览器左右无限循环滑动 我们要自己定义gallery的adapter 假设要想自己定义adapter首先要了解这几个方法 @Override public int getCount() { ...
- Android 使用Gallery组件实现图片播放预览
Gallery(画廊)扩展了LayoutParams,以此提供可以容纳当前的转换信息和先前的位置转换信息的场所. Activity package com.app.test01; import com ...
- Android 从Gallery获取图片
本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...
随机推荐
- 新手使用ThinkPHP3.2.3的命名空间问题
ThinkPHP3.2.3的命名空间问题 命名空间的出现是为了避免命名冲突. 我们在TP3.2.3的Collection和Model的创建过程中经常会遇到这样的两行代码: 这是在控制器中的写法.其中n ...
- Number of Parallelograms(求平行四边形个数)
Number of Parallelograms time limit per test 4 seconds memory limit per test 256 megabytes input sta ...
- Python 获取Facebook用户Friends的爱好类别中的Top10
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...
- [Protractor] Use protractor to catch errors in the console
For any reason, there is an error in your code, maybe something like undefined error. Protractor sti ...
- Redis 安装教程 (Windows 2.6.13 稳定版)
redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- Android--Toast时间
/** * * 显示toast,自己定义显示长短. * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time le ...
- ExtJS 饼状图报表
简单的ExtJS饼状图报表. 先上源码,咱再慢慢解析: Ext.onReady(function(){ var store = Ext.create('Ext.data.JsonStore', { f ...
- java程序的10个调试技巧
参看下面链接:http://www.kuqin.com/java/20120906/330130.html
- <正见>摘抄
1- 没有全能的力量能够扭转死亡之路,因此也就不会困在期待之中.如果没有盲目的期待,就不会有失望,如果能够了解一切都是无常,就不会攀缘执著.如果不攀缘执著,就不会患得患失,也才能真正完完全全地活着. ...
- NET Core的知识
NET Core的基础知识补遗 阅读目录 前言 在.NET Core之前 在.NET Core起步 .NET Core 1.0 .NET平台 开发环境 FAQ 写在最后 回到目录 前言 .NET Co ...