extends BaseAdapter to create adapter for images : Image « 2D Graphics « Android






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

1.Capture Image
2.extends BaseAdapter to create Image adapter
3.Capture Image and display
4.Load up the image's dimensions
5.Lazy Loading Image
6.Resize Image
7.Fit Image No Margin
8.To Rotate Texture Image
9.Image Resize
10.Create Image and resize Image
11.image To Byte
12.Image Loader
13.Save Image and Text to SD card
14.Scale and rotate Image
15.create Image
16.Resize Photo