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 ...
随机推荐
- 单调队列-Hdu-4122-Alice's mooncake shop
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4122 题目意思: 一家月饼店,有n个订单,从2001年1月1日0时开始24小时营业开m个小时,且每个 ...
- maven报错cannot change version of project facet
用Eclipse创建Maven结构的web项目的时候选择了默认的catalog,由于这个catalog比较老,用的servlet还是2.3,而现在最少也是2.5,所以经常会出现问题,在Projecdt ...
- 学习笔记DAY2
Pycharm使用 1.添加模板 file => settings =>Editor=>file and code template => python script => ...
- asp.net MVC 学习笔记
1.可以看出每个区域Areas里都是个mini的MVC项目,Controller.Models.Views一个都不缺,还多了一个AdminAreaRegistration类 2.MVC 将URL映射到 ...
- Vim知识点收集
(注意: 只记录工作中实际使用的命令) 删除带有pattern的所有行 :g/pattern/d 删除不带pattern的所有行 :g!/pattern/d 匹配red和blue,无次序 ...
- T4模板_根据DB生成实体类
为了减少重复劳动,可以通过T4读取数据库表结构,生成实体类,用下面的实例测试了一下 1.首先创建一个项目,并添加文本模板: 2.添加 文本模板: 3.向T4文本模板文件添加代码: <#@ tem ...
- iBatis2之SqlMap配置总结(18条)
iBatis2之SqlMap配置总结(18条) SqlMap的配置是iBatis中应用的核心.这部分任务占据了iBatis开发的70的工作量. 1.命名空间: <sqlMap names ...
- iOS开发 XML解析和下拉刷新,上拉加载更多
iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...
- 9、Khala实现0.01版QQ
这次来个有界面的. 登录界面: 主界面: 1.服务端开发: 只需创建一个类ChatType(./examples/HelloKhala/src/ChatType.cpp),在该类型中,核心为创建的两个 ...
- C# ?? 运算符,不能忘记的知识点
最近项目中有一个bug被测试(是黑盒测试)发现了,跟了老半天代码,才找到这个问题的所在,原来是一个计算表达式中用到了??运算符,才导致了这个错误,下面让我简单讲述一下. C# ?? 运算符 msdn上 ...