Android Open Source - ImageScanner Gallery Grid Adapter






From Project

Back to project page ImageScanner.

License

The source code is released under:

Apache License

If you think the Android project ImageScanner 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.scanner.adapter;
/*from   www.j a  v  a  2 s . com*/
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import com.atermenji.android.iconicdroid.icon.IconicIcon;
import com.example.imagescanner.GalleryGridActivity;
import com.example.imagescanner.R;
import com.scanner.beans.GridImageBean;
import com.scanner.utils.BitmapUtils;
import com.scanner.utils.DensityUtil;
import com.scanner.utils.ImageLoaderCacheHelper;
import com.scanner.utils.ImageLoaderCacheHelper.SetImageCallBack;

public class GalleryGridAdapter extends BaseAdapter{
  private final static int TYPE_COUNT = 2;
  private final static int TYPE_FIRST = 0;
  private final static int TYPE_SECOND = 1;
  
  private Context context;
  private List<GridImageBean> listItems;
  private GridView gridView;
  private LayoutInflater listContainer;
  private int itemViewResource;

  // the last item you clicked
  private int pre_position = -1;
  // current item you click
  private int cur_position = 0;
  private int gridSize;
  private ImageLoaderCacheHelper cache;
  private final Random mRandom;
  private final ArrayList<Integer> mBackgroundColors;

  static class ViewHolder {
    public ImageView grid_img;
    public ImageView grid_ok;
    public ImageView grid_preview;
    public View grid_vague;
  }

  public GalleryGridAdapter(final Context context, GridView gridView,
      final List<GridImageBean> listItems, int itemViewResource) {
    this.cache = ImageLoaderCacheHelper.getInstance(context);
    this.context = context;
    this.listContainer = LayoutInflater.from(context);
    this.gridView = gridView;
    this.gridView.setSelected(true);
    this.listItems = listItems;
    this.itemViewResource = itemViewResource;
    this.gridSize = DensityUtil.getScreenWidth(context) / 3 - 10;
    ;

    gridView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1,
          int position, long arg3) {
        // TODO Auto-generated method stub
        // if (listItems.get(position).isClickable()) {
        if (pre_position >= 0 && pre_position < listItems.size()
            && listItems.get(pre_position).isCheck()) {
          listItems.get(pre_position).setCheck(false);
        } else {
          ((GalleryGridActivity) context).showOk();
        }
        listItems.get(position).setCheck(true);
        pre_position = position;
        cur_position = position;
        GalleryGridAdapter.this.notifyDataSetChanged();
      }
    });
    mRandom = new Random();
    mBackgroundColors = new ArrayList<Integer>();
    mBackgroundColors.add(R.color.preview_color_1);
    mBackgroundColors.add(R.color.preview_color_2);
    mBackgroundColors.add(R.color.preview_color_3);
    mBackgroundColors.add(R.color.preview_color_4);
    mBackgroundColors.add(R.color.preview_color_5);
  
  }

  @Override
  public void notifyDataSetChanged() {
    // TODO Auto-generated method stub
    super.notifyDataSetChanged();
  }

  public void initGrid(ViewHolder holder) {
    FrameLayout.LayoutParams lp_img = (FrameLayout.LayoutParams) holder.grid_img
        .getLayoutParams();
    lp_img.width = gridSize;
    lp_img.height = gridSize;
    holder.grid_img.setLayoutParams(lp_img);
    FrameLayout.LayoutParams lp_vague = (FrameLayout.LayoutParams) holder.grid_vague
        .getLayoutParams();
    lp_vague.width = gridSize;
    lp_vague.height = gridSize;
    holder.grid_vague.setLayoutParams(lp_vague);
    FrameLayout.LayoutParams lp_preview = (FrameLayout.LayoutParams) holder.grid_preview
        .getLayoutParams();
    lp_preview.width = gridSize;
    lp_preview.height = gridSize;
    holder.grid_preview.setLayoutParams(lp_preview);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    GridImageBean bean = listItems.get(position);
    ViewHolder holder;
    if (convertView == null) {
      convertView = listContainer.inflate(this.itemViewResource, null);
      holder = new ViewHolder();
      holder.grid_img = (ImageView) convertView
          .findViewById(R.id.grid_img);
      holder.grid_vague = convertView.findViewById(R.id.grid_vague);
      holder.grid_ok = (ImageView) convertView.findViewById(R.id.grid_ok);
      holder.grid_preview = (ImageView) convertView
          .findViewById(R.id.grid_preview);
      initGrid(holder);
      convertView.setTag(holder);

    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    int backgroundIndex = position >= mBackgroundColors.size() ? position
        % mBackgroundColors.size() : position;
    holder.grid_preview.setBackgroundResource(mBackgroundColors
        .get(backgroundIndex));
    holder.grid_preview.setVisibility(View.VISIBLE);
    holder.grid_img.setVisibility(View.INVISIBLE);

    if (getItemViewType(position) == TYPE_FIRST) {
      holder.grid_vague.setBackgroundColor(Color.parseColor("#88000000"));
      BitmapUtils.setIconFont(context, holder.grid_ok, IconicIcon.OK,
          R.color.theme_color);
    } else {
      // ?????????????????
      holder.grid_vague.setBackgroundColor(Color.TRANSPARENT);
      holder.grid_ok.setBackgroundColor(Color.TRANSPARENT);
    }

    cache.loadBitmaps(holder.grid_img, listItems.get(position).getPath(),
        holder.grid_preview, new SetImageCallBack() {

          @Override
          public void setImage(ImageView imageView, Bitmap bitmap) {
            // TODO Auto-generated method stub
            imageView.setImageBitmap(bitmap);
          }
        });
    return convertView;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return listItems.size();
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listItems.get(position);
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  public int getChosenPosition() {
    return cur_position;
  }



  @Override
  public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    if(listItems.get(position).isCheck())
      return TYPE_FIRST;
    else 
      return TYPE_SECOND;
  }

  @Override
  public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return TYPE_COUNT;
  }


}




Java Source Code List

com.atermenji.android.iconicdroid.IconicFontDrawable.java
com.atermenji.android.iconicdroid.icon.EntypoIcon.java
com.atermenji.android.iconicdroid.icon.EntypoSocialIcon.java
com.atermenji.android.iconicdroid.icon.FontAwesomeIcon.java
com.atermenji.android.iconicdroid.icon.Icon.java
com.atermenji.android.iconicdroid.icon.IconicIcon.java
com.atermenji.android.iconicdroid.util.TypefaceManager.java
com.example.imagescanner.GalleryFolderActivity.java
com.example.imagescanner.GalleryGridActivity.java
com.example.imagescanner.MainActivity.java
com.scanner.adapter.GalleryFolderListAdapter.java
com.scanner.adapter.GalleryGridAdapter.java
com.scanner.beans.GalleryFolderBean.java
com.scanner.beans.GridImageBean.java
com.scanner.utils.BitmapUtils.java
com.scanner.utils.ConstantsUtils.java
com.scanner.utils.DensityUtil.java
com.scanner.utils.FormatBitmapUtils.java
com.scanner.utils.ImageLoaderCacheHelper.java
com.scanner.utils.L.java
com.scanner.utils.ScannerUtils.java
com.scanner.utils.UIHelper.java