Beispiel #1
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 #2
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;
    }
  }
Beispiel #3
0
 public ViewHolder setBrightness(int viewId, int brightness) {
   ImageView imageView = (ImageView) getView(viewId);
   ColorMatrix colorMatrix = new ColorMatrix();
   colorMatrix.set(
       new float[] {
         1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0
       });
   imageView.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
   return this;
 }
 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);
 }
  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 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;
 }
  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);
  }
 /**
  * 对图标进行灰色处理
  *
  * @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;
 }
Beispiel #9
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;
 }
    private int rotateColor(int color, float rad) {
      float deg = rad * 180 / 3.1415927f;
      int r = Color.red(color);
      int g = Color.green(color);
      int b = Color.blue(color);

      ColorMatrix cm = new ColorMatrix();
      ColorMatrix tmp = new ColorMatrix();

      cm.setRGB2YUV();
      tmp.setRotate(0, deg);
      cm.postConcat(tmp);
      tmp.setYUV2RGB();
      cm.postConcat(tmp);

      final float[] a = cm.getArray();

      int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b);
      int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b);
      int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

      return Color.argb(
          Color.alpha(color), pinToByte(ir),
          pinToByte(ig), pinToByte(ib));
    }
 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;
 }
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;
 }
Beispiel #13
0
  public static Bitmap colorMatrixBmp(Bitmap bmp, float[] matrixSrc) {

    int width = bmp.getWidth();
    int height = bmp.getHeight();

    ColorMatrix matrix = new ColorMatrix();
    matrix.set(matrixSrc);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
    Paint p = new Paint();
    p.setColorFilter(filter);
    Bitmap colorBmp = Bitmap.createBitmap(width, height, bmp.getConfig());
    Canvas c = new Canvas(colorBmp);
    c.drawBitmap(bmp, 0, 0, p);
    return colorBmp;
  }
Beispiel #14
0
 protected void onDraw(Canvas canvas) {
   canvas.drawColor(0xff333333);
   for (byte i = 0; i < 7; i++)
     for (byte j = 0; j < 6; j++)
       if (curMap[i][j] != -1)
         canvas.drawBitmap(
             brickBmp,
             new Rect(
                 (int) (curMap[i][j] * temp), 0, (int) (curMap[i][j] * temp + temp), (int) temp),
             new Rect(XOff + j * Size, i * Size, XOff + j * Size + Size, i * Size + Size),
             null);
   // canvas.drawBitmap(scaleBmp[(int)curFrame],dx, dy, null);
   canvas.drawBitmap(
       scaleBmp[(int) curFrame],
       new Rect(0, 0, 116, 63),
       new RectF(dx, dy, dx + Size * 2 + scaleXOff * 2, dy + Size + scaleYOff * 2),
       null);
   Paint p = new Paint();
   p.setColor(0xffffffff);
   p.setTextSize(textSize); // pixel
   canvas.drawText(
       "第  " + (index + 1) + " 关        " + "剩余 " + moveLeft + " 步",
       textOff,
       Size * 7 + textSize,
       p);
   if (state == 1) {
     for (byte i = 0; i < 30; i++) {
       if (explosionIndex[i] != 9) {
         Paint mPaint = new Paint();
         ColorMatrix cm = new ColorMatrix();
         cm.set(colorM[cBMP[i]]);
         mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
         canvas.drawBitmap(
             explosionBmp[explosionIndex[i]],
             new Rect(0, 0, 49, 48),
             new RectF(XY[i][0], XY[i][1], XY[i][0] + Size, XY[i][1] + Size),
             mPaint);
       }
     }
   } else if (state == 3) {
     data.mainActivity.saveRecord(data.mainActivity.curLevel);
     ((HDOSActivity) cont).showDialog(0);
     state = 0;
   } else if (state == 4) {
     state = 0;
     ((HDOSActivity) cont).showDialog(1);
   }
 }
    /** Keep all colorspace representations in sync. */
    private void updateYUVfromRGB() {
      float r = mRGB[0] / 255.0f;
      float g = mRGB[1] / 255.0f;
      float b = mRGB[2] / 255.0f;

      ColorMatrix cm = new ColorMatrix();
      cm.setRGB2YUV();
      final float[] a = cm.getArray();

      mYUV[0] = a[0] * r + a[1] * g + a[2] * b;
      mYUV[0] = pinToUnit(mYUV[0]);
      mYUV[1] = a[5] * r + a[6] * g + a[7] * b;
      mYUV[1] = pin(mYUV[1], -.5f, .5f);
      mYUV[2] = a[10] * r + a[11] * g + a[12] * b;
      mYUV[2] = pin(mYUV[2], -.5f, .5f);
    }
Beispiel #16
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;
  }
    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));
    }
 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;
 }
Beispiel #19
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);
    }
  }
  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 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);
    }
  }
Beispiel #22
0
  public static Bitmap blackWhiteBmp(Bitmap bmp) {
    // black and white

    int width = bmp.getWidth();
    int height = bmp.getHeight();

    ColorMatrix matrix = new ColorMatrix();
    float[] src = {
      0.308F, 0.609F, 0.082F, 0, 0, 0.308F, 0.609F, 0.082F, 0, 0, 0.308F, 0.609F, 0.082F, 0, 0, 0,
      0, 0, 1, 0
    };
    matrix.set(src);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
    Paint p = new Paint();
    p.setColorFilter(filter);
    Bitmap colorBmp = Bitmap.createBitmap(width, height, bmp.getConfig());
    Canvas c = new Canvas(colorBmp);
    c.drawBitmap(bmp, 0, 0, p);
    return colorBmp;
  }
Beispiel #23
0
    public ComponentPeerView(Context context, boolean opaque_, long host) {
      super(context);
      this.host = host;
      setWillNotDraw(false);
      opaque = opaque_;

      setFocusable(true);
      setFocusableInTouchMode(true);
      setOnFocusChangeListener(this);
      requestFocus();

      // swap red and blue colours to match internal opengl texture format
      ColorMatrix colorMatrix = new ColorMatrix();

      float[] colorTransform = {
        0, 0, 1.0f, 0, 0, 0, 1.0f, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0
      };

      colorMatrix.set(colorTransform);
      paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    }
  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 #25
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);
    }
  }
Beispiel #26
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);
  }
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private void animateTextColor(int targetColor) {
    if (mCurrentColorAnim != null) {
      mCurrentColorAnim.cancel();
    }

    mCurrentColorAnim = new AnimatorSet();
    mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);

    if (mSrcFilter == null) {
      mSrcFilter = new ColorMatrix();
      mDstFilter = new ColorMatrix();
      mCurrentFilter = new ColorMatrix();
    }

    DragView.setColorScale(getTextColor(), mSrcFilter);
    DragView.setColorScale(targetColor, mDstFilter);
    ValueAnimator anim1 =
        ValueAnimator.ofObject(
            new FloatArrayEvaluator(mCurrentFilter.getArray()),
            mSrcFilter.getArray(),
            mDstFilter.getArray());
    anim1.addUpdateListener(
        new AnimatorUpdateListener() {

          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
            invalidate();
          }
        });

    mCurrentColorAnim.play(anim1);
    mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
    mCurrentColorAnim.start();
  }
  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));
  }
    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;
    }
  @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));
    }
  }