代码如下:
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
适配器代码:
package cn.com;
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;
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_1,
R.drawable.sample_1, R.drawable.sample_1, R.drawable.sample_1,
R.drawable.sample_1, R.drawable.sample_1 };
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
分享到:
相关推荐
网上下载的一份Gallery Flow代码; 在此基础上修改了不能准确判定中间项的问题。 对部分代码进行了优化。 学习Gallery的实用代码
Gallery3D.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
画廊试图Gallery.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
PhotoGallery.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
flutter_gallery 预览apk,方便学习了解Flutter的同学下载预览。
android整个Gallery模块的应用层源代码,供大家学习。
4.0的gallery源码,学习图库加载的好帮手,就是比较复杂,请耐下心来看
自定义动画倒影的Gallery,适合新手学习之用,gb编码。
Android利用Gallery和ImageSwitcher实现在线相册图片预览功能(异步加载图片).rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
前端-ionic-官网教程学习项目photo-gallery
这个一个基于Gallery和Imageswitcher的Android简易图片播放器,适合初学者学习Android开发。
Gallery组件主要用于横向显示图像列表,不过按常规做法。工程源代码,解压导入即可学习,有注释。
048-集-GridView和Gallery视频教程,很是不错哦,有兴趣的可以学习下。
3DWondershare_Flash_Gallery动画相册完美破解版(互相学习)
自定义 gallery ,gallery的学习,gallery 源码下载喽
这是一个简单的Gallery视图演示项目,帮助你学习,如何创建自己的Gallery应用如android Gallery
图片gallery jquery 动画函数
Android_Gallery3D demo,不容易啊,大家学习学习!
NULL 博文链接:https://kusebingtang.iteye.com/blog/907193
为公司设计的XML+Flash+Gallery 使用actionscript3实现的 一个很好的学习actionscript3的项目!