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






extends BaseAdapter to create Image adapter

   
package app.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class Test extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setTitle("GridViewActivity");
    GridView gridview = (GridView) findViewById(R.id.grid_view);
    gridview.setAdapter(new ImageAdapter(this));
  }

  public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
      mContext = c;
    }

    public int getCount() {
      return mThumbIds.length;
    }

    public Object getItem(int position) {
      return null;
    }

    public long getItemId(int position) {
      return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
      } else {
        imageView = (ImageView) convertView;
      }

      imageView.setImageResource(mThumbIds[position]);
      return imageView;
    }

    private Integer[] mThumbIds = { R.drawable.icon, R.drawable.icon, };
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
 
       
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/grid_view"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/> 
 

   
    
    
  








Related examples in the same category

1.Capture Image
2.extends BaseAdapter to create adapter for images
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