Android Open Source - AndroidHandsOn Image Grid






From Project

Back to project page AndroidHandsOn.

License

The source code is released under:

Apache License

If you think the Android project AndroidHandsOn listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.globant.mobile.handson;
/*from   www  .  j  a  va 2 s. co  m*/
import java.io.File;
import java.util.zip.Inflater;

import android.annotation.TargetApi;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ShareActionProvider;

import com.globant.mobile.handson.media.BitmapCache.ImageCacheParams;
import com.globant.mobile.handson.media.BitmapFetcher;
import com.globant.mobile.handson.provider.Bitmaps;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Activities that
 * contain this fragment must implement the
 * {@link ImageGrid.OnFragmentInteractionListener} interface to handle
 * interaction events. Use the {@link ImageGrid#newInstance} factory method to
 * create an instance of this fragment.
 * 
 */
public class ImageGrid extends Fragment implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{
   private static final String TAG = "ImageGridFragment";
   private static final String IMAGE_CACHE_DIR = "thumbs";

   private int mImageThumbSize;
   private int mImageThumbSpacing;
   private ImageAdapter mAdapter;
   private BitmapFetcher mImageFetcher;   
   private ActionMode mActionMode;   

  
    public ImageGrid() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
        mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing);

        mAdapter = new ImageAdapter(getActivity());

        ImageCacheParams cacheParams = new ImageCacheParams(getActivity(), IMAGE_CACHE_DIR);

        cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory

        // The ImageFetcher takes care of loading images into our ImageView children asynchronously
        mImageFetcher = new BitmapFetcher(getActivity(), mImageThumbSize);
        mImageFetcher.setLoadingImage(R.drawable.empty_photo);
        mImageFetcher.addImageCache(getActivity().getSupportFragmentManager(), cacheParams);
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View v = inflater.inflate(R.layout.fragment_image_grid, container, false);
        final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);        
        mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                // Pause fetcher to ensure smoother scrolling when flinging
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                    mImageFetcher.setPauseWork(true);
                } else {
                    mImageFetcher.setPauseWork(false);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
            }
        });
        //Setting the Context Menu for the GridVew id API level is lower than Honeycomb
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
          registerForContextMenu(mGridView);
        }else{
          mGridView.setOnItemLongClickListener(this);
        }
        // This listener is used to get the final width of the GridView and then calculate the
        // number of columns and the width of each column. The width of each column is variable
        // as the GridView has stretchMode=columnWidth. The column width is used to set the height
        // of each view so we get nice square thumbnails.
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (mAdapter.getNumColumns() == 0) {
                            final int numColumns = (int) Math.floor(
                                    mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                            if (numColumns > 0) {
                                final int columnWidth =
                                        (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                                mAdapter.setNumColumns(numColumns);
                                mAdapter.setItemHeight(columnWidth);
                                if (BuildConfig.DEBUG) {
                                    Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                                }
                            }
                        }
                    }
                });

        return v;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  @Override
    public void onResume() {
        super.onResume();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
          final GridView mGridView = (GridView) this.getActivity().findViewById(R.id.gridView);
          mGridView.clearChoices();
        }
        mImageFetcher.setExitTasksEarly(false);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
        mImageFetcher.setPauseWork(false);
        mImageFetcher.setExitTasksEarly(true);
        mImageFetcher.flushCache();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mImageFetcher.closeCache();
    }

    @TargetApi(16)
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        final Intent i = new Intent(getActivity(), ImageDetailActivity.class);
        i.putExtra(ImageDetailActivity.EXTRA_IMAGE, (int) id);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            // makeThumbnailScaleUpAnimation() looks kind of ugly here as the loading spinner may
            // show plus the thumbnail image in GridView is cropped. so using
            // makeScaleUpAnimation() instead.
            ActivityOptions options =
                    ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight());
            getActivity().startActivity(i, options.toBundle());
        } else {
            startActivity(i);
        }
    }
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
  public boolean onItemLongClick(AdapterView<?> parent, View v, int position,
      long id) {
      //Setting the Context Action Bar if API Level is Honeycomb and higher
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
          if(mActionMode != null){
            return false;
          }
          
          final int index = position;
          final int realIndex = (int)id;
          ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
        
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {          
          return false;
        }
        
        @Override
        public void onDestroyActionMode(ActionMode mode) {
          mActionMode = null;
          
        }
        
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
          // Inflate a menu resource providing context menu items
              MenuInflater inflater = mode.getMenuInflater();
              inflater.inflate(R.menu.image_context_bar, menu);
              
              
              return true;
        }
        
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
          switch(item.getItemId()){
          case R.id.action_delete:{
            deleteBitmap(index, realIndex);
            mode.finish(); // Action picked, so close the CAB
            return true;
          }
          case R.id.action_share:{
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
                  getShareIntent(index);
                }
            mode.finish();
            return true;
          }
          default:
            return false;
          }
        }
      };
          
          mActionMode = this.getActivity().startActionMode(mActionModeCallback);
          v.setSelected(true);                    
          return true;
        }else{
          return false;
        }
  }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo){
      super.onCreateContextMenu(menu, view, menuInfo);
      
      MenuInflater inflater = this.getActivity().getMenuInflater();
      inflater.inflate(R.menu.image_context_bar, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {        
        return super.onOptionsItemSelected(item);
    }
    
  @Override
    public boolean onContextItemSelected(MenuItem item){
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      
        switch (item.getItemId()) {
            case R.id.action_delete:
                deleteBitmap(info.position, (int)info.id);              
                return true;
            case R.id.action_share:
              getShareIntent(info.position);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    private void deleteBitmap(int position, int realPosition) {
      final GridView mGridView = (GridView)this.getActivity().findViewById(R.id.gridView);
      String bitmapPath = (String)mGridView.getAdapter().getItem(position);      
      File fileToDelete = new File(bitmapPath);
      if(fileToDelete.exists()){
        fileToDelete.delete();
        Bitmaps.removeItemAt(realPosition);
        mAdapter.notifyDataSetChanged();
      }
  }
    
    private void getShareIntent(int position) {
      final GridView mGridView = (GridView)this.getActivity().findViewById(R.id.gridView);
      String bitmapPath = (String)mGridView.getAdapter().getItem(position);      
      //Create the intent
      Intent shareIntent = new Intent();
      shareIntent.setAction(Intent.ACTION_SEND);
      shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(bitmapPath));    
      shareIntent.setType("image/jpeg");
      
      startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.action_share)));
  }
  /**
     * The main adapter that backs the GridView. This is fairly standard except the number of
     * columns in the GridView is used to create a fake top row of empty views as we use a
     * transparent ActionBar and don't want the real top row of images to start off covered by it.
     */
    private class ImageAdapter extends BaseAdapter {

        private final Context mContext;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private int mActionBarHeight = 0;
        private GridView.LayoutParams mImageViewLayoutParams;

        public ImageAdapter(Context context) {
            super();
            mContext = context;
            mImageViewLayoutParams = new GridView.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            // Calculate ActionBar height
            TypedValue tv = new TypedValue();
            if (context.getTheme().resolveAttribute(
                    android.R.attr.actionBarSize, tv, true)) {
                mActionBarHeight = TypedValue.complexToDimensionPixelSize(
                        tv.data, context.getResources().getDisplayMetrics());
            }
        }

        @Override
        public int getCount() {
            // Size + number of columns for top empty row
            return Bitmaps.imageThumbUrls.length + mNumColumns;
        }

        @Override
        public Object getItem(int position) {
            return position < mNumColumns ?
                    null : Bitmaps.imageThumbUrls[position - mNumColumns];
        }

        @Override
        public long getItemId(int position) {
            return position < mNumColumns ? 0 : position - mNumColumns;
        }

        @Override
        public int getViewTypeCount() {
            // Two types of views, the normal ImageView and the top row of empty views
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return (position < mNumColumns) ? 1 : 0;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup container) {
            // First check if this is the top row
            if (position < mNumColumns) {
                if (convertView == null) {
                    convertView = new View(mContext);
                }
                // Set empty view with height of ActionBar
                convertView.setLayoutParams(new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));
                return convertView;
            }

            // Now handle the main ImageView thumbnails
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, instantiate and initialize
                imageView = new RecyclingImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setLayoutParams(mImageViewLayoutParams);
            } else { // Otherwise re-use the converted view
                imageView = (ImageView) convertView;
            }

            // Check the height matches our calculated column width
            if (imageView.getLayoutParams().height != mItemHeight) {
                imageView.setLayoutParams(mImageViewLayoutParams);
            }

            // Finally load the image asynchronously into the ImageView, this also takes care of
            // setting a placeholder image while the background thread runs
            mImageFetcher.loadImage(Bitmaps.imageThumbUrls[position - mNumColumns], imageView, "false", null);
            return imageView;
        }

        /**
         * Sets the item height. Useful for when we know the column width so the height can be set
         * to match.
         *
         * @param height
         */
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams =
                    new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }

        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }
    }
  
}




Java Source Code List

com.globant.mobile.handson.BaseActivity.java
com.globant.mobile.handson.CameraActivity.java
com.globant.mobile.handson.DisplayMessageActivity.java
com.globant.mobile.handson.GalleryActivity.java
com.globant.mobile.handson.ImageDetailActivity.java
com.globant.mobile.handson.ImageDetailFragment.java
com.globant.mobile.handson.ImageGrid.java
com.globant.mobile.handson.MainActivity.java
com.globant.mobile.handson.RecyclingImageView.java
com.globant.mobile.handson.exception.CameraNotAvailableException.java
com.globant.mobile.handson.media.BitmapCache.java
com.globant.mobile.handson.media.BitmapDecoder.java
com.globant.mobile.handson.media.BitmapFetcher.java
com.globant.mobile.handson.media.CustomCamera.java
com.globant.mobile.handson.media.FaceDetection.java
com.globant.mobile.handson.media.RecyclingBitmapDrawable.java
com.globant.mobile.handson.media.task.AsyncTask.java
com.globant.mobile.handson.media.task.BitmapWorker.java
com.globant.mobile.handson.media.task.MustacheWorker.java
com.globant.mobile.handson.media.task.WorkerListener.java
com.globant.mobile.handson.provider.Bitmaps.java
com.globant.mobile.handson.provider.Images.java
com.globant.mobile.handson.sensor.RotationListener.java
com.globant.mobile.handson.util.DiskLruCache.java
com.globant.mobile.handson.util.SystemUiHiderBase.java
com.globant.mobile.handson.util.SystemUiHiderHoneycomb.java
com.globant.mobile.handson.util.SystemUiHider.java