Created
March 16, 2015 02:09
-
-
Save rghwang/f5deffe99353c7656f68 to your computer and use it in GitHub Desktop.
Grayscale + CircularNetworkImageView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wantedlab.android.wanted.helpers; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.ColorMatrix; | |
import android.graphics.ColorMatrixColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.util.AttributeSet; | |
import com.android.volley.toolbox.NetworkImageView; | |
public class GrayscaleNetworkImageView extends NetworkImageView { | |
Context mContext; | |
public GrayscaleNetworkImageView(Context context) { | |
super(context); | |
mContext = context; | |
} | |
public GrayscaleNetworkImageView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
mContext = context; | |
} | |
public GrayscaleNetworkImageView(Context context, AttributeSet attrs, | |
int defStyle) { | |
super(context, attrs, defStyle); | |
mContext = context; | |
} | |
@Override | |
public void setImageBitmap(Bitmap bm) { | |
if(bm==null) return; | |
setImageDrawable(new BitmapDrawable(mContext.getResources(), | |
getGrayscaleBitmap(bm))); | |
} | |
/** | |
* Creates a circular bitmap and uses whichever dimension is smaller to determine the width | |
* <br/>Also constrains the circle to the leftmost part of the image | |
* | |
* @param bitmap | |
* @return bitmap | |
*/ | |
public Bitmap getGrayscaleBitmap(Bitmap bitmap) { | |
CircularNetworkImageView giv = new CircularNetworkImageView(mContext); | |
Bitmap rounded = giv.getCircularBitmap(bitmap); | |
Bitmap output = Bitmap.createBitmap(rounded.getWidth(), rounded.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(output); | |
Paint paint = new Paint(); | |
ColorMatrix cm = new ColorMatrix(); | |
cm.setSaturation(0); | |
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); | |
paint.setColorFilter(f); | |
canvas.drawBitmap(rounded, 0,0, paint); | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires CircularNetworkImageView https://gist.github.com/rghwang/13db490a07112cc99fa1