package com.tyndalehouse.step; import com.tyndalehouse.step.R; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery.LayoutParams; import android.widget.ImageView; import android.widget.Toast; public class Viewer extends Activity { private FullGallery g; // private Cursor c; // private int columnIndex; @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.viewer ); // /** // * Query image database to get images // */ // String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.ORIENTATION }; // // c = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // projection, // MediaStore.Images.Media.DATA + " like ? ", // new String[] { "%STEP/Wilson%" }, // null ); // columnIndex = c.getColumnIndexOrThrow( MediaStore.Images.Media._ID ); /** * Set Gallery */ g = ( FullGallery ) findViewById( R.id.gallery ); g.setAdapter( new ImageAdapter( this ) ); g.setFadingEdgeLength(0); g.setSpacing(2); g.setAdapter( new ImageAdapter( this ) ); /** * Set up click listener on Gallery */ g.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { if ( v != null ) { Toast.makeText(Viewer.this, "Page " + (position + 1), Toast.LENGTH_SHORT).show(); } } }); // We also want to show context menu for longpressed items in the gallery registerForContextMenu(g); } @Override public void onCreateContextMenu( ContextMenu menu, View v, ContextMenuInfo menuInfo ) { menu.add(R.string.gallery_2_text); } @Override public boolean onContextItemSelected( MenuItem item ) { AdapterContextMenuInfo info = ( AdapterContextMenuInfo ) item.getMenuInfo(); Toast.makeText(this, "Longpress: " + ( info.position + 1 ), Toast.LENGTH_SHORT).show(); return true; } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { R.drawable.wilson1, R.drawable.wilson2 }; public ImageAdapter( Context context ) { mContext = context; TypedArray attr = mContext.obtainStyledAttributes( R.styleable.Viewer ); mGalleryItemBackground = attr.getResourceId( R.styleable.Viewer_android_galleryItemBackground, 0 ); attr.recycle(); } public int getCount() { // return c.getCount(); return mImageIds.length; } public Object getItem( int position ) { return position; } public long getItemId( int position ) { return position; } public View getView( int position, View convertView, ViewGroup parent ) { ImageView imageView = new ImageView(mContext); // // Move cursor to current position // c.moveToPosition(position); // // // Get the current value for the requested column // int imageID = c.getInt(columnIndex); // // // Obtain the image URI // Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString( imageID ) ); // // // Set the content of the image based on the image URI // imageView.setImageURI( uri ); // Set content of the image from resources imageView.setImageResource( mImageIds[position] ); imageView.setLayoutParams( new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ) ); imageView.setScaleType( ImageView.ScaleType.FIT_CENTER ); return imageView; } } }