extends BaseAdapter to create adapter for images
package app.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery)findViewById(R.id.gallery);
ManateeAdapter manateeAdapter = new ManateeAdapter(this);
gallery.setAdapter(manateeAdapter);
}
public static class ManateeAdapter extends BaseAdapter {
private static final String TAG = "ManateeAdapter";
private static int convertViewCounter = 0;
private Context mContext;
private LayoutInflater mInflater;
static class ViewHolder {
ImageView image;
}
private int[] manatees = {
R.drawable.icon, R.drawable.icon};
private Bitmap[] manateeImages = new Bitmap[manatees.length];
private Bitmap[] manateeThumbs = new Bitmap[manatees.length];
public ManateeAdapter(Context context) {
Log.v(TAG, "Constructing ManateeAdapter");
this.mContext = context;
mInflater = LayoutInflater.from(context);
for(int i=0; i<manatees.length; i++) {
manateeImages[i] = BitmapFactory.decodeResource(
context.getResources(), manatees[i]);
manateeThumbs[i] = Bitmap.createScaledBitmap(manateeImages[i],
100, 100, false);
}
}
public int getCount() {
Log.v(TAG, "in getCount()");
return manatees.length;
}
public int getViewTypeCount() {
Log.v(TAG, "in getViewTypeCount()");
return 1;
}
public int getItemViewType(int position) {
Log.v(TAG, "in getItemViewType() for position " + position);
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Log.v(TAG, "in getView for position " + position +
", convertView is " +
((convertView == null)?"null":"being recycled"));
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
convertViewCounter++;
Log.v(TAG, convertViewCounter + " convertViews have been created");
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.gridImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageBitmap(manateeImages[position]);
return convertView;
}
public Object getItem(int position) {
Log.v(TAG, "in getItem() for position " + position);
return manateeImages[position];
}
public long getItemId(int position) {
Log.v(TAG, "in getItemId() for position " + position);
return position;
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/gallery.xml -->
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
//row.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#555"
android:scaleType="centerInside"
android:padding="5dip"
android:maxHeight="50dip"
android:maxWidth="50dip"
/>
Related examples in the same category