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~



更多相关文章

  1. Android为HttpClient设置证书(用户名和密码)
  2. TextInputLayout,让输入框更有灵性
  3. android图片上传springMvc
  4. Android(安卓)之 ProgressDialog用法介绍
  5. android spinner 选中后显示字体的颜色设置
  6. android中图片的拖拉和缩放
  7. 闹铃设置
  8. android 自定义progressBar
  9. Android实现刮刮卡抽奖(上)

随机推荐

  1. android 检测字符串是否为合法域名
  2. AndroidClipSquare安卓实现方形头像裁剪
  3. android 关于读取SD卡或者U盘的一些方法
  4. 【Android笔记】Activity涉及界面全屏的
  5. Android显示GIF动画完整示例(二)
  6. 使用Android Dropbox API检查Dropbox上是
  7. 【边做项目边学Android】手机安全卫士04_
  8. android开发中如何从当前页面返回上一页
  9. net. unknownhostexception:无法解析主机
  10. 【Android 开发教程】通过编码实现发送短