Hello, Views(一)Gallery滑动的图片(附源码下载)
来源:互联网 发布:淘宝账号申诉二维码 编辑:程序博客网 时间:2024/04/29 13:06
前言
通过官方案例学习,是最直接的方法。结合书本在此介绍一下gallery的运用。
效果
涉及到的类
· BaseAdapter
· Gallery
· ImageView
· AdapterView.OnItemClickListener
下面是工程的结构,
新建:1)一个主activity命名为HelloGalleryActivity.java,
2)一个自定义adapter命名为GalleryAdapter用于填充Gallery
3) 在drawable里面放置使用到的图片资源
4)在values里面新建一个xml,atrrs.xml用于定义图片的边框效果
5)在mian.xml里面添加一个gallery。
代码书写:
在HelloGalleryActivity里面重写的onCreate()方法添加以下代码:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery1); gallery.setAdapter(new GalleryAdapter(this)); //下面的单击监听用于显示一个toast,作用是显示所单击的图片的下标 gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(HelloGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); }
在GalleryAdapter里面,自动生成了一些需要重写的方法:
public class GalleryAdapter extends BaseAdapter{ //用于图片的背景边框 int mGalleryItemBackground; //获得上下文的引用 private Context mContext; //存放图片资源的整型数组 private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7 }; //重写构造器, public GalleryAdapter(Context c) { mContext = c; // 设置边框样式 TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground = a.getResourceId( R.styleable.HelloGallery_android_galleryItemBackground, 0); a.recycle(); } @Override public int getCount() { return mImageIds.length; } @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 i = new ImageView(mContext); i.setImageResource(mImageIds[position]); i.setLayoutParams(new Gallery.LayoutParams(150, 100)); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(mGalleryItemBackground); return i; }}
完善atrrs.xml文件:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground" /> </declare-styleable></resources>
基本上这样就OK了。
源码下载
- Hello, Views(一)Gallery滑动的图片(附源码下载)
- Hello, Views(一)Gallery滑动的图片(附源码下载)
- Hello,Views(六)下拉框Spinner(附源码)
- Hello,Views(二)GoogleMap地图使用上篇(附源码)
- Hello,Views(三)GoogleMap地图使用下篇(附源码)
- Hello,Views(四)用Web View实现浏览网页(附源码)
- Hello,Views(五)自动填充文字AutoCompleteTextView(附源码)
- 如何提高网页中图片显示的用户体验(附源码下载)
- 一个简单的滑动门示例(附源码)
- Android自定义“图片+文字”控件四种实现方法之一--------Gallery原理(提供源码下载)
- Android SDK Tutorials系列 - Hello Views - Gallery
- Hello Views之Gallery(yaozq翻页,仅供参考)
- Android页面左右滑动浏览图片(Gallery示例)
- ANDROID图片浏览(带水平滑动的GridView和类似gallery的ViewPage)
- 王立平--Gallery:实现图片的左右滑动
- Android的滑动图片Gallery弃用
- 下载Android 5.0源码(附源码)
- android Gallery滑动图片
- Oracle建表语句和约束语句案例
- 【Android】面试题
- os.path中的常用函数
- 新建供应商时付款条件取值来源
- linux poll函数
- Hello, Views(一)Gallery滑动的图片(附源码下载)
- 【Android】解决Android平台移植ffmpeg的一揽子问题
- 一道面试题:餐馆模拟
- dom4j在VisitorSupport类里删除属性抛出IndexOutOfBoundsException异常的解决方案
- 三种东西永远不要放到数据库里
- 使用ModelDriven接收参数添加用户
- 数据挖掘领域的十大经典算法
- C# Mutex实现程序单实例运行
- 写一段java代码让if else里面的语句都执行到