Skip to content

Instantly share code, notes, and snippets.

@martyglaubitz
Last active January 8, 2016 11:59
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save martyglaubitz/f3144c421991f579c7ef to your computer and use it in GitHub Desktop.
Save martyglaubitz/f3144c421991f579c7ef to your computer and use it in GitHub Desktop.
Circle mask NetworkImageView for Volley
/* The MIT License (MIT)
Copyright (c) 2014, Marty Glaubitz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import com.android.volley.toolbox.NetworkImageView;
public class CircleNetworkImageView extends NetworkImageView {
private Paint _Paint;
private RectF _RectF;
public CircleNetworkImageView(Context context) {
super(context);
}
public CircleNetworkImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setImageBitmap(Bitmap bitmap) {
if (bitmap == null) {
_Paint = null;
super.setImageBitmap(bitmap);
return;
}
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();
final int imageWidth = canvas.getWidth() - paddingLeft * 2;
final int imageHeight = canvas.getHeight() - paddingTop * 2;
_RectF.set(0.0f, 0.0f, imageWidth, imageHeight);
_RectF.offset(paddingLeft, paddingTop);
final int radiusX = imageWidth / 2;
final int radiusY = imageHeight / 2;
canvas.drawRoundRect(_RectF, radiusX, radiusY, _Paint);
}
@Override
protected void onDraw(Canvas canvas) {
if (_Paint == null) {
super.onDraw(canvas);
return;
}
_RectF.set(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight());
final int radiusX = canvas.getWidth() / 2;
final int radiusY = canvas.getHeight() / 2;
canvas.drawRoundRect(_RectF, radiusX, radiusY, _Paint);
}
}
@martyglaubitz
Copy link
Author

the view no respects horizontal & vertical padding

@azharuniverse
Copy link

Can't resolve symbol "canvas" in

@Override
public void setImageBitmap(Bitmap bitmap) {
if (bitmap == null) {
_Paint = null;

super.setImageBitmap(bitmap);
return;
}


final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();

final int imageWidth = canvas.getWidth() - paddingLeft * 2;
final int imageHeight = canvas.getHeight() - paddingTop * 2;

_RectF.set(0.0f, 0.0f, imageWidth, imageHeight);
_RectF.offset(paddingLeft, paddingTop);

final int radiusX = imageWidth / 2;
final int radiusY = imageHeight / 2;

canvas.drawRoundRect(_RectF, radiusX, radiusY, _Paint);
}

@erango
Copy link

erango commented Oct 17, 2014

In setImageBitmap, before using canvas, add this line:

Canvas canvas = new Canvas(bitmap);

@bkurzius
Copy link

bkurzius commented Nov 7, 2014

I had problems getting this to work for me so using another post I made this class -- it seems to work pretty well: https://gist.github.com/bkurzius/99c945bd1bdcf6af8f99

@sfatdisk
Copy link

sfatdisk commented Oct 8, 2015

Thanks, it is perfect.

@SultanAmmar
Copy link

bkurzius the solution you posted is bad organized.

martyglaubitz thank you for the Gist, but ...

When you created rectF and paint, you used the underscore method, it doesn't work properly

private Paint _Paint;
private RectF _RectF;

Whenever setImageBitmap is called, NullPointerException is thrown :

  • java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.RectF.set(float, float, float, float)' on a null object reference
    • at com.matekap.mwc21.utils.CircleNetworkImageView.setImageBitmap(CircleNetworkImageView.java:61)

So it needed to be initialized somewhere :

private Paint paint = new Paint();
private RectF rectF = new RectF();

No errors but, it shows black rounded pictures, because of the paint default values.

Any solution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment