1    在 xml 布局中添加 Gallery

activity_main.xml
<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

ImageAdapter.java
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 的背景参数

res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="gallery_style">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>

4    在 MainActivity 中绑定数据与设置监听

MainActivity.java
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    图片资源

注:图片最好为 png 格式的图片,由于jpg是压缩后的图片,在android 中解压缩有可能导致内存溢出错误。
 

6    结果展示

 




注:转载请注明出处 :)   毕竟代码是一个一个敲出来的啊,O(∩_∩)O~




Android 之 Gallery的更多相关文章

  1. android 32 Gallery:横着滚动的列表

    Gallery:横着滚动的列表 mainActivity.java package com.sxt.day05_01; import java.util.ArrayList; import java. ...

  2. Android 解决Gallery下ScrollView滑动事件冲突

    在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...

  3. android学习Gallery和ImageSwitch的使用

    Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了, * @deprec ...

  4. Android之Gallery和Spinner-Android学习之旅(二十九)

    Spinner简介 spinner是竖直方向展开一个列表供选择.和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可 ...

  5. android学习---Gallery画廊视图

    Gallery与Spinner有共同父类:AbsPinner.说明Gallery与Spinner都是一个列表框. 它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是 ...

  6. Android在Gallery中每次滑动只显示一页

    import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; impo ...

  7. android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器

    为了使图片浏览器左右无限循环滑动 我们要自己定义gallery的adapter 假设要想自己定义adapter首先要了解这几个方法 @Override public int getCount() { ...

  8. Android 使用Gallery组件实现图片播放预览

    Gallery(画廊)扩展了LayoutParams,以此提供可以容纳当前的转换信息和先前的位置转换信息的场所. Activity package com.app.test01; import com ...

  9. Android 从Gallery获取图片

    本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...

随机推荐

  1. 无心插柳OR志在必得?阿里推“来往”的意图

        近年来,阿里巴巴在外围的动作确实不少,投资新浪微博.投资陌陌,配合阿里自身的一些战略调整,让人觉得这家公司似乎正在经历一场前所未有的“蜕变”.其实这也不难理解,在BAT三国演义中,任何一方都不 ...

  2. 多线程 AfxBeginThread 与 CreateThread 的区别

      简言之:  AfxBeginThread是MFC的全局函数,是对CreateThread的封装. CreateThread是Win32 API函数,前者最终要调到后者. 1>.具体说来,Cr ...

  3. WebApplication和WebSite的区别

    不同点 1. 创建方式不同 一个是FILE->NEW->PROJECT->ASP.NET WEB APPLICATION 另外一个是 FILE->NEW->WEBSITE ...

  4. 关于.net 对.manifest清单文件查找缓存的猜想

    问题背景: winform调用unity web player 插件. 按如下操作: ,编译后会生成.manifest清单文件: 通过清单内容可以看出程序在运行时是按照以上信息来查找ActiveX控件 ...

  5. C# 让textbox 只能输入数字的方法

    使用textBox控件的KeyPress事件 private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.Key ...

  6. ios开发 AFNetworking的基本使用方法

    AFNetworking的基本使用方法 什么是GET请求? 如果只是单纯的下载数据, 使用GET请求 什么是POST请求? 特点:  请求的内容不会出现在URL网址中 向服务器发送用户名和密码, 或者 ...

  7. xcode 7 添加空模板

    文件下载链接: http://pan.baidu.com/s/1pKbyf4R 密码: ppi7 下载完成后将模板放入以下路径应用程序->Xcode->右键显示包内容->/Conte ...

  8. spring mvc ajax

    <%@ page contentType="text/html;charset=UTF-8" %> <%@ include file="/WEB-INF ...

  9. Java IO5:管道流、对象流

    前言 前面的文章主要讲了文件字符输入流FileWriter.文件字符输出流FileReader.文件字节输出流FileOutputStream.文件字节输入流FileInputStream,这些都是常 ...

  10. Android 6.0 闪光灯的使用

    Android6.0 已经抛弃了Camer 相关的API,改用新的API接口CamerManager,下面给出使用的简单实例 package com.inper.duqiang.slashlight; ...