Gallery实现图片的画廊展示

来源:互联网 发布:后期软件培训 编辑:程序博客网 时间:2024/06/10 04:52


手滑动屏幕的时候,图片就换更换。



基本控件Gallery。

        <Gallery            android:id="@+id/gallery"            android:layout_width="fill_parent"            android:layout_height="200dp"            android:layout_marginTop="30dp" />

然后比较重要的是适配器adapter。好吧,在回顾一次适配器。适配器是当做一个容器,用来装载数据的,当手机屏幕要显示一些数据的时候,不是直接从数据源获取,而是先从适配器去取数据,数据源会填充数据到适配器中。适配器有几种适配器,这里用到了ArrayAdapter,数组适配器,用于定义数组获取图片,还有BaseAdapter基本适配器,这两个适配器都是经常用的。


自定义一个适配器去继承基本适配器。里面有一个主要函数个人觉得是比较重要的:

public View getView(int position, View convertView, ViewGroup parent),API的解释是:得到一个意图,用来显示这个数据,显示这个数据我们可以在指定的位置来填充这个数据。你可以通过手工去创建一个View。由于这个画廊是自定义的适配器,所以需要用自定义的布局来显示。View convertView这个参数就是说明了如果再没用新布局填充的时候,将使用旧的布局。

public class MainActivity extends Activity {private Gallery gallery;    private ImageAdapter imageAdapter;private int[] resIds = { R.drawable.bg_center1, R.drawable.bg_dialog,R.drawable.bg_dialog1, R.drawable.bg_dialog2,R.drawable.bg_dialog3, R.drawable.bg_dialog4, R.drawable.bg_gym1,R.drawable.bg_gym2 };//定义图片数组@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);gallery = (Gallery)findViewById(R.id.gallery);imageAdapter = new ImageAdapter(this);gallery.setAdapter(imageAdapter) ;}public class ImageAdapter extends BaseAdapter{       private Context context;       int mGralleyItemBackground;   //使用简单的计数器填充背景图              public ImageAdapter (Context context){//构造方法       this.context = context;       //在Adapetr设置一个类型适配器,       TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);       mGralleyItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground,0);       }@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE; //返回一个Integer value,表示我们适配器填充的值可以是一个最大值}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn resIds[position];   //当我们调用这个方法是,返回的是整个图片数组的值}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub//自定义的适配器,需要用自定义的布局来显示,通常android的通用布局是不能满足沃恩的需求//可以手工创建一个适配器,也可以用inflate填充一个xml文件//从数据源中根据position获得每一个图片的值,填充到指定的XML布局中//View convertView 是一个旧的布局,如果没有新的布局填充,就自动用旧的布局ImageView imageView = new ImageView(context);imageView.setImageResource(resIds[position % resIds.length]);imageView.setScaleType(ImageView.ScaleType.FIT_XY); //设置图片的类型数据,当前ImageView的滑动的是按照X轴滑动imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));//设置大小imageView.setBackgroundResource(mGralleyItemBackground);//设置一下背景return imageView;}

然后这里因为要用到自己的布局,所以要新建一个布局XML文件。这里我也有点不明白,eclpse里面没用自动提示declare-styleable  估计是因为自定义的所以就不显示了

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="Gallery">        <attr name="android:galleryItemBackground"></attr>    </declare-styleable></resources>


0 0
原创粉丝点击