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~
分享到:
相关推荐
NULL 博文链接:https://sunzone.iteye.com/blog/1998086
具体说明可以参考博客:我的Android进阶之旅------>Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器 博客地址:...
Android 重写Gallery实现gallery的动画切换效果
Android应用源码之Gallery2_Android
android 4.0 Gallery源码
android开发中,Gallery控件的使用,详细可以参见博客:http://www.cnblogs.com/plokmju/p/android_Gallery.html
Android照片墙Gallery程序源码
本篇文章主要介绍了Android之Gallery使用例子,Gallery用来显示图片列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
实现Android中gallery图片自动切换。
android gallery简单应用,里面有对gallery源码例子
android整个Gallery模块的应用层源代码,供大家学习。
android gallery例子android gallery例子android gallery例子
Android应用源码之Gallery_Gallery
android gallery demoandroid gallery demoandroid gallery demo
Android应用源码之Gallery1_Gallery
android Gallery + ImageSwitcher
android 环形gallery 源码 超炫效果
Android Gallery 自定义ImageView和Gallery实现图片浏览功能,类似Android图库软件,主要实现的功能有:图片左右滑动、放大缩小、放大弹回、缩小弹回。 一开始使用Viewpager实现图片浏览,发现两张图片滑动时没有...
自己写的一个Android Gallery 小例子,共享给大家顺便自己保存一下。