Beispiel #1
0
  private void initRendering(int mode) {
    Paint paint = new Paint();

    switch (mode) {
      case 0:
        { // Default
          paint.setColorFilter(null);
          break;
        }
      case 1:
        { // Grayscale
          ColorMatrix matrix = new ColorMatrix();
          matrix.setSaturation(0);
          ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
          paint.setColorFilter(filter);
          break;
        }
      case 2:
        { // Inverted
          ColorMatrixColorFilter filter = new ColorMatrixColorFilter(NEGATIVE_COLOR);
          paint.setColorFilter(filter);
          break;
        }
      case 3:
        { // Inverted grayscale
          ColorMatrix matrix = new ColorMatrix();
          matrix.set(NEGATIVE_COLOR);

          ColorMatrix gcm = new ColorMatrix();
          gcm.setSaturation(0);

          ColorMatrix concat = new ColorMatrix();
          concat.setConcat(matrix, gcm);

          ColorMatrixColorFilter filter = new ColorMatrixColorFilter(concat);
          paint.setColorFilter(filter);

          break;
        }
      default:
        {
          paint.setColorFilter(null);
          break;
        }
    }

    // maybe sometime LAYER_TYPE_NONE would better?
    setLayerType(View.LAYER_TYPE_HARDWARE, paint);
  }
Beispiel #2
0
  /**
   * 图像调整
   *
   * @param bm 默认不可修改
   * @param hue 色相
   * @param saturation 饱和度
   * @param lum 亮度
   * @return
   */
  public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    ColorMatrix hueMatrix = new ColorMatrix();
    hueMatrix.setRotate(0, hue);
    hueMatrix.setRotate(1, hue);
    hueMatrix.setRotate(2, hue);

    ColorMatrix saturationMatrix = new ColorMatrix();
    saturationMatrix.setSaturation(saturation);

    ColorMatrix lumMatrix = new ColorMatrix();
    lumMatrix.setScale(lum, lum, lum, 1);

    ColorMatrix imageMatrix = new ColorMatrix();
    imageMatrix.postConcat(hueMatrix);
    imageMatrix.postConcat(saturationMatrix);
    imageMatrix.postConcat(lumMatrix);

    paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
    canvas.drawBitmap(bm, 0, 0, paint);

    return bmp;
  }
Beispiel #3
0
  public synchronized void saveEffect() {
    if (saveScreenCopy != null) {
      saveScreenCopy.recycle();
      saveScreenCopy = null;
    }

    saveScreenCopy =
        Bitmap.createBitmap(
            (int) (windowWidth * windowScale / 2),
            (int) (windowHeight * windowScale / 2),
            Config.RGB_565);
    Canvas c = new Canvas(saveScreenCopy);

    saveEffectColorMatrix.setSaturation(0.1f);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(saveEffectColorMatrix);
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    p.setColorFilter(f);
    c.scale(windowScale * 0.5f, windowScale * 0.5f);
    c.drawBitmap(screen, 0, 0, p);

    saveEffectRotation = (float) (10f + Math.random() * 20f); // 10 to 20 degrees
    if (Math.random() < 0.5f) saveEffectRotation = -saveEffectRotation;

    // saveScreenCopy = screen.copy(Config.RGB_565, false);
    saveEffectTimer = saveEffectDuration;
  }
Beispiel #4
0
  public IconUtilities(Context context) {
    final Resources resources = context.getResources();
    DisplayMetrics metrics = mDisplayMetrics = resources.getDisplayMetrics();
    final float density = metrics.density;
    final float blurPx = 5 * density;

    mIconWidth = mIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
    mIconTextureWidth = mIconTextureHeight = mIconWidth + (int) (blurPx * 2);

    mBlurPaint.setMaskFilter(new BlurMaskFilter(blurPx, BlurMaskFilter.Blur.NORMAL));

    TypedValue value = new TypedValue();
    mGlowColorPressedPaint.setColor(
        context.getTheme().resolveAttribute(android.R.attr.colorPressedHighlight, value, true)
            ? value.data
            : 0xffffc300);
    mGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
    mGlowColorFocusedPaint.setColor(
        context.getTheme().resolveAttribute(android.R.attr.colorFocusedHighlight, value, true)
            ? value.data
            : 0xffff8e00);
    mGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));

    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0.2f);

    mCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG));
  }
Beispiel #5
0
  public static Bitmap setTint(Bitmap sourceBitmap, int color) {

    try {

      Bitmap resultBitmap =
          Bitmap.createBitmap(
              sourceBitmap.getWidth(), sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888);

      Canvas canvas = new Canvas(resultBitmap);

      float r = ((color >> 16) & 0xFF) / 255.0f;
      float g = ((color >> 8) & 0xFF) / 255.0f;
      float b = ((color >> 0) & 0xFF) / 255.0f;

      float[] colorTransform = {0, r, 0, 0, 0, 0, 0, g, 0, 0, 0, 0, 0, b, 0, 0, 0, 0, 1f, 0};

      ColorMatrix colorMatrix = new ColorMatrix();
      colorMatrix.setSaturation(0f);
      colorMatrix.set(colorTransform);

      ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
      Paint paint = new Paint();
      paint.setColorFilter(colorFilter);
      canvas.drawBitmap(sourceBitmap, 0, 0, paint);

      return resultBitmap;

    } catch (IllegalArgumentException ex) {
      return null;
    }
  }
  private Bitmap generateShortcutPreview(
      ResolveInfo info, int maxWidth, int maxHeight, Bitmap preview) {
    Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
    final Canvas c = mCachedShortcutPreviewCanvas.get();
    if (tempBitmap == null
        || tempBitmap.getWidth() != maxWidth
        || tempBitmap.getHeight() != maxHeight) {
      tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
      mCachedShortcutPreviewBitmap.set(tempBitmap);
    } else {
      c.setBitmap(tempBitmap);
      c.drawColor(0, PorterDuff.Mode.CLEAR);
      c.setBitmap(null);
    }
    // Render the icon
    Drawable icon = mIconCache.getFullResIcon(info);

    int paddingTop =
        mContext.getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
    int paddingLeft =
        mContext.getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
    int paddingRight =
        mContext.getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);

    int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);

    renderDrawableToBitmap(
        icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);

    if (preview != null && (preview.getWidth() != maxWidth || preview.getHeight() != maxHeight)) {
      throw new RuntimeException("Improperly sized bitmap passed as argument");
    } else if (preview == null) {
      preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
    }

    c.setBitmap(preview);
    // Draw a desaturated/scaled version of the icon in the background as a watermark
    Paint p = mCachedShortcutPreviewPaint.get();
    if (p == null) {
      p = new Paint();
      ColorMatrix colorMatrix = new ColorMatrix();
      colorMatrix.setSaturation(0);
      p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
      p.setAlpha((int) (255 * 0.06f));
      mCachedShortcutPreviewPaint.set(p);
    }
    c.drawBitmap(tempBitmap, 0, 0, p);
    c.setBitmap(null);

    renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);

    return preview;
  }
 private void initGrayBackgroundBitmap() {
   mGrayBackgroundBitmap =
       Bitmap.createBitmap(
           mBackgroundBitmap.getWidth(), mBackgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(mGrayBackgroundBitmap);
   Paint grayPaint = new Paint();
   ColorMatrix colorMatrix = new ColorMatrix();
   colorMatrix.setSaturation(0);
   ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
   grayPaint.setColorFilter(filter);
   canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint);
 }
 /**
  * 对图标进行灰色处理
  *
  * @param srcDrawable 源图
  * @return 非彩色的图片
  */
 public static Drawable getNeutralDrawable(Drawable srcDrawable) {
   if (srcDrawable != null) {
     ColorMatrix colorMatrix = new ColorMatrix();
     colorMatrix.setSaturation(0f);
     // 设为黑白
     srcDrawable.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
     // 设为阴影
     // srcDrawable.setColorFilter(new
     // PorterDuffColorFilter(0x87000000,PorterDuff.Mode.SRC_ATOP));
     return srcDrawable;
   }
   return null;
 }
 private Bitmap toGrayscale(Bitmap bmpOriginal) {
   Bitmap bmpGrayscale =
       Bitmap.createBitmap(
           bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
   Canvas c = new Canvas(bmpGrayscale);
   Paint paint = new Paint();
   ColorMatrix cm = new ColorMatrix();
   cm.setSaturation(0);
   ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
   paint.setColorFilter(f);
   c.drawBitmap(bmpOriginal, 0, 0, paint);
   return bmpGrayscale;
 }
Beispiel #10
0
 /**
  * 将图片转化为灰度图片
  *
  * @param bitmap
  * @return
  */
 public static Bitmap grayBitmap(Bitmap bitmap) {
   int width = bitmap.getWidth();
   int height = bitmap.getHeight();
   Bitmap greyBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(greyBitmap);
   Paint paint = new Paint();
   ColorMatrix colorMatrix = new ColorMatrix();
   colorMatrix.setSaturation(0);
   ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
   paint.setColorFilter(colorMatrixFilter);
   canvas.drawBitmap(bitmap, 0, 0, paint);
   return greyBitmap;
 }
  public SelectSensorListAdapter(
      LayoutInflater inflater, Vector<String> selected, SelectSensorActionListener listener) {
    this.inflater = inflater;
    this.sensors = null;
    this.selected = selected;
    this.listener = listener;

    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0);
    bwfilter = new ColorMatrixColorFilter(matrix);

    updateSensorList(false);
  }
Beispiel #12
0
 public static Bitmap makeGaryBitmap(Bitmap bitmap, Context context) {
   final int w = bitmap.getWidth();
   final int h = bitmap.getHeight();
   final Bitmap garyBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
   final Canvas canvas = new Canvas(garyBitmap);
   Paint paint = new Paint();
   ColorMatrix colorMatrix = new ColorMatrix();
   colorMatrix.setSaturation(0);
   ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
   paint.setColorFilter(colorMatrixFilter);
   paint.setAlpha(100);
   canvas.drawBitmap(bitmap, 0, 0, paint);
   return garyBitmap;
 }
 public Bitmap toBinary(Bitmap bmpOriginal) {
   int width, height;
   height = bmpOriginal.getHeight();
   width = bmpOriginal.getWidth();
   Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
   Canvas c = new Canvas(bmpGrayscale);
   Paint paint = new Paint();
   ColorMatrix cm = new ColorMatrix();
   cm.setSaturation(0);
   ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
   paint.setColorFilter(f);
   c.drawBitmap(bmpOriginal, 0, 0, paint);
   return bmpGrayscale;
 }
    public Bitmap getDesaturatedBitmap(@NonNull Bitmap favicon) {
      Bitmap grayscaleBitmap =
          Bitmap.createBitmap(favicon.getWidth(), favicon.getHeight(), Bitmap.Config.ARGB_8888);

      Canvas c = new Canvas(grayscaleBitmap);
      if (mColorMatrix == null || mFilter == null || mPaint == null) {
        mPaint = new Paint();
        mColorMatrix = new ColorMatrix();
        mColorMatrix.setSaturation(DESATURATED);
        mFilter = new ColorMatrixColorFilter(mColorMatrix);
        mPaint.setColorFilter(mFilter);
      }

      c.drawBitmap(favicon, 0, 0, mPaint);
      return grayscaleBitmap;
    }
Beispiel #15
0
  public BezelImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Attribute initialization
    final TypedArray a =
        context.obtainStyledAttributes(
            attrs, R.styleable.BezelImageView, defStyle, R.style.BezelImageView);

    mMaskDrawable = a.getDrawable(R.styleable.BezelImageView_biv_maskDrawable);
    if (mMaskDrawable != null) {
      mMaskDrawable.setCallback(this);
    }

    mDrawCircularShadow = a.getBoolean(R.styleable.BezelImageView_biv_drawCircularShadow, true);

    mSelectorColor = a.getColor(R.styleable.BezelImageView_biv_selectorOnPress, 0);

    a.recycle();

    // Other initialization
    mBlackPaint = new Paint();
    mBlackPaint.setColor(0xff000000);

    mMaskedPaint = new Paint();
    mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // Always want a cache allocated.
    mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

    // Create a desaturate color filter for pressed state.
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    mDesaturateColorFilter = new ColorMatrixColorFilter(cm);

    // create a selectorFilter if we already have a color
    if (mSelectorColor != 0) {
      this.mSelectorFilter =
          new PorterDuffColorFilter(
              Color.argb(
                  mSelectorAlpha,
                  Color.red(mSelectorColor),
                  Color.green(mSelectorColor),
                  Color.blue(mSelectorColor)),
              PorterDuff.Mode.SRC_ATOP);
    }
  }
    BitmapsView(Context c) {
      super(c);

      mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
      mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2);

      mColorMatrixPaint = new Paint();
      final ColorMatrix colorMatrix = new ColorMatrix();
      colorMatrix.setSaturation(0);
      mColorMatrixPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

      mLightingPaint = new Paint();
      mLightingPaint.setColorFilter(new LightingColorFilter(0x0060ffff, 0x00101030));

      mBlendPaint = new Paint();
      mBlendPaint.setColorFilter(new PorterDuffColorFilter(0x7f990040, PorterDuff.Mode.SRC_OVER));
    }
  private void drawColorFilter(Canvas canvas) {
    canvas.save();
    canvas.scale(mScaleFactor, mScaleFactor);
    if (mBitmap != null) {
      ColorMatrix m = new ColorMatrix();
      m.setSaturation(0);

      ColorMatrixColorFilter filter = new ColorMatrixColorFilter(m);
      mPaint.setColorFilter(filter);

      canvas.drawBitmap(mBitmap, bx, by, mPaint);
    }
    if (message != null) {
      canvas.drawText(message, tx, ty, mPaint);
    }
    canvas.restore();
  }
 public Bitmap bitmap2Gray(Bitmap bmSrc) {
   // 得到图片的长和宽
   int width = bmSrc.getWidth();
   int height = bmSrc.getHeight();
   // 创建目标灰度图像
   Bitmap bmpGray = null;
   bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
   // 创建画布
   Canvas c = new Canvas(bmpGray);
   Paint paint = new Paint();
   ColorMatrix cm = new ColorMatrix();
   cm.setSaturation(0);
   ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
   paint.setColorFilter(f);
   c.drawBitmap(bmSrc, 0, 0, paint);
   return bmpGray;
 }
  public BezelImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    setLayerType(LAYER_TYPE_HARDWARE, null); // 性能要求
    // Attribute initialization
    final TypedArray a =
        context.obtainStyledAttributes(attrs, styleable.BezelImageView, defStyle, 0);

    mMaskDrawable = a.getDrawable(styleable.BezelImageView_maskDrawable);
    if (mMaskDrawable != null) {
      mMaskDrawable.setCallback(this);
    }

    mBorderDrawable = a.getDrawable(styleable.BezelImageView_borderDrawable);
    if (mBorderDrawable != null) {
      mBorderDrawable.setCallback(this);
    }

    mDesaturateOnPress =
        a.getBoolean(styleable.BezelImageView_desaturateOnPress, mDesaturateOnPress);

    a.recycle();

    // Other initialization
    mBlackPaint = new Paint();
    mBlackPaint.setColor(0xffff0000);
    mBlackPaint.setAntiAlias(true);

    mMaskedPaint = new Paint();
    mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    mMaskedPaint.setAntiAlias(true);

    // Always want a cache allocated.
    mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

    if (mDesaturateOnPress) {
      // Create a desaturate color filter for pressed state.
      ColorMatrix cm = new ColorMatrix();
      cm.setSaturation(0);
      mDesaturateColorFilter = new ColorMatrixColorFilter(cm);
    }
  }
  private void textExtraction(Bitmap croppedPic) {
    int width, height;
    height = croppedPic.getHeight();
    width = croppedPic.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(croppedPic, 0, 0, paint);

    baseApi.setImage(bmpGrayscale);
    baseApi.setImage(baseApi.getThresholdedImage());
    String recognizedText = baseApi.getUTF8Text();
    recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9\\s]+", "");
    extractedText.setText(recognizedText);

    croppedImage.setImageBitmap(bmpGrayscale);
  }
Beispiel #21
0
  public BezelImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    attributeInitialization(context, attrs, defStyle);

    // Other initialization
    mBlackPaint = new Paint();
    mBlackPaint.setColor(0xff000000);

    mMaskedPaint = new Paint();
    mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // Always want a cache allocated.
    mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

    if (mDesaturateOnPress) {
      // Create a desaturate color filter for pressed state.
      ColorMatrix cm = new ColorMatrix();
      cm.setSaturation(0);
      mDesaturateColorFilter = new ColorMatrixColorFilter(cm);
    }
  }
  static void setBackground(View v, Bitmap bm) {

    if (bm == null) {
      v.setBackgroundResource(0);
      return;
    }

    int vwidth = v.getWidth();
    int vheight = v.getHeight();
    int bwidth = bm.getWidth();
    int bheight = bm.getHeight();
    float scalex = (float) vwidth / bwidth;
    float scaley = (float) vheight / bheight;
    float scale = Math.max(scalex, scaley) * 1.3f;

    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    Bitmap bg = Bitmap.createBitmap(vwidth, vheight, config);
    Canvas c = new Canvas(bg);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    ColorMatrix greymatrix = new ColorMatrix();
    greymatrix.setSaturation(0);
    ColorMatrix darkmatrix = new ColorMatrix();
    darkmatrix.setScale(.3f, .3f, .3f, 1.0f);
    greymatrix.postConcat(darkmatrix);
    ColorFilter filter = new ColorMatrixColorFilter(greymatrix);
    paint.setColorFilter(filter);
    Matrix matrix = new Matrix();
    matrix.setTranslate(-bwidth / 2, -bheight / 2); // move bitmap center to origin
    matrix.postRotate(10);
    matrix.postScale(scale, scale);
    matrix.postTranslate(vwidth / 2, vheight / 2); // Move bitmap center to view center
    c.drawBitmap(bm, matrix, paint);
    v.setBackgroundDrawable(new BitmapDrawable(bg));
  }
Beispiel #23
0
 private void disableImage(ImageView imageView) {
     ColorMatrix matrix = new ColorMatrix();
     matrix.setSaturation(0); //0 means grayscale
     ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
     imageView.setColorFilter(cf);
 }
  @Override
  public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    if (position == dataSet.size() - 3) {
      campaignAdapterClickListener.onScrolledToBottom();
    }

    Resources resources = holder.itemView.getResources();
    Context context = holder.itemView.getContext();

    if (getItemViewType(position) == VIEW_TYPE_CAMPAIGN) {
      final Campaign campaign = (Campaign) dataSet.get(position);
      CampaignViewHolder campaignViewHolder = (CampaignViewHolder) holder;
      campaignViewHolder.title.setText(campaign.title);

      int height = resources.getDimensionPixelSize(R.dimen.campaign_height);

      CharSequence tag = (CharSequence) campaignViewHolder.imageView.getTag();
      if (!TextUtils.equals(campaign.imagePath, tag)) {
        Picasso.with(context)
            .load(campaign.imagePath)
            .resize(0, height)
            .into(campaignViewHolder.imageView);
        campaignViewHolder.imageView.setTag(campaign.imagePath);
      }

      if (campaign.userIsSignedUp) {
        campaignViewHolder.signedupIndicator.setVisibility(View.VISIBLE);
      } else {
        campaignViewHolder.signedupIndicator.setVisibility(View.GONE);
      }

      campaignViewHolder.root.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              selectedPosition = position;
              campaignAdapterClickListener.onCampaignExpanded(position);
              notifyItemChanged(position);
            }
          });

      ColorMatrix cm = new ColorMatrix();
      if (TimeUtils.isCampaignExpired(campaign)) {
        cm.setSaturation(0);
        campaignViewHolder.imageView.setColorFilter(new ColorMatrixColorFilter(cm));
      } else {
        cm.setSaturation(1);
        campaignViewHolder.imageView.setColorFilter(new ColorMatrixColorFilter(cm));
      }
    }
    if (getItemViewType(position) == VIEW_TYPE_CAMPAIGN_SMALL) {
      final Campaign campaign = (Campaign) dataSet.get(position);
      CampaignViewHolder smallCampaignViewHolder = (CampaignViewHolder) holder;
      smallCampaignViewHolder.title.setText(campaign.title);

      int height = resources.getDimensionPixelSize(R.dimen.campaign_small_height);

      CharSequence tag = (CharSequence) smallCampaignViewHolder.imageView.getTag();
      if (!TextUtils.equals(campaign.imagePath, tag)) {
        Picasso.with(context)
            .load(campaign.imagePath)
            .resize(0, height)
            .into(smallCampaignViewHolder.imageView);
        smallCampaignViewHolder.imageView.setTag(campaign.imagePath);
      }
      smallCampaignViewHolder.root.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              selectedPosition = position;
              campaignAdapterClickListener.onCampaignExpanded(position);
              notifyItemChanged(position);
            }
          });

      if (campaign.userIsSignedUp) {
        smallCampaignViewHolder.signedupIndicator.setVisibility(View.VISIBLE);
      } else {
        smallCampaignViewHolder.signedupIndicator.setVisibility(View.GONE);
      }

      ColorMatrix cm = new ColorMatrix();
      if (TimeUtils.isCampaignExpired(campaign)) {
        cm.setSaturation(0);
        smallCampaignViewHolder.imageView.setColorFilter(new ColorMatrixColorFilter(cm));
      } else {
        cm.setSaturation(1);
        smallCampaignViewHolder.imageView.setColorFilter(new ColorMatrixColorFilter(cm));
      }
    } else if (getItemViewType(position) == V_TYPE_CAMP_EXPANDED_EXPIRED) {
      final Campaign campaign = (Campaign) dataSet.get(position);
      final ExpandedExpireCampaignViewHolder viewHolder = (ExpandedExpireCampaignViewHolder) holder;

      viewHolder.title.setText(campaign.title);
      viewHolder.callToAction.setText(campaign.callToAction);

      int height = resources.getDimensionPixelSize(R.dimen.campaign_height_expanded);

      CharSequence tag = (CharSequence) viewHolder.imageView.getTag();

      if (!TextUtils.equals(campaign.imagePath, tag)) {
        Picasso.with(context).load(campaign.imagePath).resize(0, height).into(viewHolder.imageView);
        viewHolder.imageView.setTag(campaign.imagePath);
      }
      viewHolder.imageView.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              selectedPosition = -1;
              notifyItemChanged(position);
            }
          });

      final SlantedBackgroundDrawable background =
          new SlantedBackgroundDrawable(
              false, Color.WHITE, shadowColor, slantHeight, widthOvershoot, heightShadowOvershoot);
      viewHolder.slantedBg.setBackground(background);
      viewHolder.refreshCopy.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              campaignAdapterClickListener.onNetworkCampaignRefresh();
              // FIXME show progress bar
            }
          });

      ColorMatrix cm = new ColorMatrix();
      cm.setSaturation(0);
      viewHolder.imageView.setColorFilter(new ColorMatrixColorFilter(cm));
    } else if (getItemViewType(position) == VIEW_TYPE_CAMPAIGN_EXPANDED) {
      final Campaign campaign = (Campaign) dataSet.get(position);
      ExpandedCampaignViewHolder viewHolder = (ExpandedCampaignViewHolder) holder;

      viewHolder.title.setText(campaign.title);
      viewHolder.callToAction.setText(campaign.callToAction);

      int height = resources.getDimensionPixelSize(R.dimen.campaign_height_expanded);

      CharSequence tag = (CharSequence) viewHolder.imageView.getTag();

      if (!TextUtils.equals(campaign.imagePath, tag)) {
        Picasso.with(context).load(campaign.imagePath).resize(0, height).into(viewHolder.imageView);
        viewHolder.imageView.setTag(campaign.imagePath);
      }
      viewHolder.imageView.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              selectedPosition = -1;
              notifyItemChanged(position);
            }
          });

      if (campaign.userIsSignedUp) {
        viewHolder.signedupIndicator.setVisibility(View.VISIBLE);
        viewHolder.alreadySignedUp.setVisibility(View.VISIBLE);
        viewHolder.notSignedUp.setVisibility(View.GONE);
      } else {
        viewHolder.signedupIndicator.setVisibility(View.GONE);
        viewHolder.alreadySignedUp.setVisibility(View.GONE);
        viewHolder.notSignedUp.setVisibility(View.VISIBLE);
      }

      SlantedBackgroundDrawable background =
          new SlantedBackgroundDrawable(
              false, Color.WHITE, shadowColor, slantHeight, widthOvershoot, heightShadowOvershoot);
      viewHolder.slantedBg.setBackground(background);
      viewHolder.campaignDetailsWrapper.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              campaignAdapterClickListener.onCampaignClicked(campaign.id, campaign.userIsSignedUp);
            }
          });

      // If campaign.endTime isn't specified by the server, then don't show the expires label
      if (campaign.endTime == 0) {
        viewHolder.expire_label.setVisibility(View.GONE);
        viewHolder.daysWrapper.setVisibility(View.GONE);
      } else {
        viewHolder.expire_label.setVisibility(View.VISIBLE);
        viewHolder.daysWrapper.setVisibility(View.VISIBLE);

        List<String> campExpTime = TimeUtils.getTimeUntilExpiration(campaign.endTime);
        int dayInt = Integer.parseInt(campExpTime.get(0));
        viewHolder.daysLabel.setText(resources.getQuantityString(R.plurals.days, dayInt));
        viewHolder.days.setText(String.valueOf(dayInt));
      }
    } else if (getItemViewType(position) == VIEW_TYPE_REPORT_BACK) {
      final ReportBack reportBack = (ReportBack) dataSet.get(position);
      ReportBackViewHolder reportBackViewHolder = (ReportBackViewHolder) holder;

      Picasso.with(context).load(reportBack.getImagePath()).into(reportBackViewHolder.root);

      reportBackViewHolder.root.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              campaignAdapterClickListener.onReportBackClicked(
                  reportBack.id, reportBack.campaign.id);
            }
          });
    } else if (getItemViewType(position) == VIEW_TYPE_CAMPAIGN_FOOTER) {
      SectionTitleViewHolder sectionTitleViewHolder = (SectionTitleViewHolder) holder;
      sectionTitleViewHolder.textView.setText(resources.getString(R.string.people_doing_stuff));
    }
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (convertView == null) {
      convertView = layoutInflater.inflate(R.layout.listitem_player_achievement, parent, false);
      holder = new ViewHolder();
      holder.icon = (ImageView) convertView.findViewById(R.id.iv_listitem_player_achievement_icon);
      holder.title = (TextView) convertView.findViewById(R.id.tv_listitem_player_achievement_title);
      holder.progress =
          (ProgressBar) convertView.findViewById(R.id.pb_listitem_player_achievement_progress);
      holder.description =
          (TextView) convertView.findViewById(R.id.tv_listitem_player_achievement_description);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    boolean unlocked = getItem(position);
    Achievement ach = Achievements.getAchievement(position);

    holder.title.setText(ach.getTitle());
    holder.icon.setImageResource(ach.getDrawableID());

    if (unlocked) {
      holder.title.setTextColor(Color.parseColor("#000000"));
      holder.progress.setVisibility(View.GONE);
      holder.description.setText(ach.getUnlockedDescription());
      holder.description.setTextColor(Color.parseColor("#000000"));
      holder.icon.clearColorFilter();
    } else {
      // grayscale
      holder.title.setTextColor(Color.parseColor("#999999"));

      long maxAmount = ach.getUnlockAmount();
      long amount = profile.getPlayerStatistics().getAmount(ach.getStatType());

      holder.description.setText(
          String.format(
              fa.getResources().getString(R.string.listitem_player_achievements_progress),
              amount,
              maxAmount,
              fa.getResources().getString(ach.getUnlockDescription())));
      holder.description.setTextColor(Color.parseColor("#999999"));

      // if(amount > 0){
      holder.progress.setVisibility(View.VISIBLE);
      holder.progress.setMax((int) maxAmount);
      holder.progress.setProgress((int) amount);
      /*}else{
          holder.progress.setVisibility(View.GONE);
      }*/

      // TODO: maybe use special not-unlocked icons?
      ColorMatrix matrix = new ColorMatrix();
      matrix.setSaturation(0); // 0 means grayscale
      ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);

      holder.icon.setColorFilter(cf);
    }

    return convertView;
  }