android 拖拉图片 Gallery 用BaseAdapter实现

android 拖拉图片 Gallery 用BaseAdapter实现_第1张图片

 

 

在main.xml中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <Gallery

        android:id="@+id/myGallery"

        android:gravity="center_vertical"

        android:spacing="3dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

</LinearLayout>

 

 

新建ImageGalleryAdapter extends BaseAdapter

 

package com.tarena.galleryproject;

 

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.Gallery.LayoutParams;

import android.widget.ImageView;

 

public class ImageGalleryAdapter extends BaseAdapter {

  private Context context = null;

  //里面的所有方法表示的是可以根据指定的显示图片的数量,进行每个图片的处理

  private int[] imgRes = new int[]{

       R.drawable.b,R.drawable.c,R.drawable.d,

       R.drawable.e,R.drawable.f,R.drawable.g,

       R.drawable.h,R.drawable.l}; //这些是所要显示的图片的资源

  public ImageGalleryAdapter(Context context){

     this.context = context;

  }

  public int getCount() {   //取得要显示的内容的数量

     return this.imgRes.length;  //数量为资源的数量

  }

 

  public Object getItem(int position) {

     return this.imgRes[position];

  }

 

  public long getItemId(int position) { //取得项的ID

     return this.imgRes[position];

  }

 

  public View getView(int position, View convertView,

       ViewGroup parent) {  //将资源设置到一个组件之中,很明显这个组件就是ImageView组件

     ImageView img = new ImageView(this.context);

     img.setBackgroundColor(0xffffffff);

     img.setImageResource(this.imgRes[position]); //将指定的资源设置到ImageView中

     img.setScaleType(ImageView.ScaleType.CENTER);  //居中显示

     img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,

         LayoutParams.WRAP_CONTENT));

     return img;

  }

 

}

 

 

 

在MyGalleryProjectDemo.java程序中

package com.tarena.galleryproject;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.Gallery;

import android.widget.Toast;

 

public class MyGalleryProjectDemo extends Activity {

  private Gallery gallery = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        this.gallery = (Gallery) super.findViewById(R.id.myGallery);

        this.gallery.setAdapter(new ImageGalleryAdapter(this));

        this.gallery.setOnItemClickListener(new onItemClickListenerImpl());

    }

    private class onItemClickListenerImpl implements OnItemClickListener{

 

     public void onItemClick(AdapterView<?> parent, View view, int position,

         long id) {

       Toast.makeText(MyGalleryProjectDemo.this,

            String.valueOf(position),

            Toast.LENGTH_SHORT).show();

     }

   

    }

}



 

你可能感兴趣的:(gallery,用BaseAdapter)