Last active
January 8, 2016 11:59
-
-
Save martyglaubitz/f3144c421991f579c7ef to your computer and use it in GitHub Desktop.
Circle mask NetworkImageView for Volley
This file contains 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
/* 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); | |
} | |
} |
Thanks, it is perfect.
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
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