My Circular Imageview Not Working Properly
I follow this example [here][1] to to give circuler shape. I am getting images from server and set it via BaseAdapter, the image is showing but not in circular view. So, can anyone
Solution 1:
Use this link for library to get circular image view https://github.com/hdodenhof/CircleImageView
Solution 2:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.widget.ImageView;
publicclassCircleImageViewextendsImageView {
privatestaticfinalScaleTypeSCALE_TYPE= ScaleType.CENTER_CROP;
privatestaticfinal Bitmap.ConfigBITMAP_CONFIG= Bitmap.Config.ARGB_8888;
privatestaticfinalintCOLORDRAWABLE_DIMENSION=2;
privatestaticfinalintDEFAULT_BORDER_WIDTH=0;
privatestaticfinalintDEFAULT_BORDER_COLOR= Color.BLACK;
privatestaticfinalbooleanDEFAULT_BORDER_OVERLAY=false;
privatefinalRectFmDrawableRect=newRectF();
privatefinalRectFmBorderRect=newRectF();
privatefinalMatrixmShaderMatrix=newMatrix();
privatefinalPaintmBitmapPaint=newPaint();
privatefinalPaintmBorderPaint=newPaint();
privateintmBorderColor= DEFAULT_BORDER_COLOR;
privateintmBorderWidth= DEFAULT_BORDER_WIDTH;
private Bitmap mBitmap;
private BitmapShader mBitmapShader;
privateint mBitmapWidth;
privateint mBitmapHeight;
privatefloat mDrawableRadius;
privatefloat mBorderRadius;
private ColorFilter mColorFilter;
privateboolean mReady;
privateboolean mSetupPending;
privateboolean mBorderOverlay;
publicCircleImageView(Context context) {
super(context);
init();
}
publicCircleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
publicCircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArraya= context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);
mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_border_overlay, DEFAULT_BORDER_OVERLAY);
a.recycle();
init();
}
privatevoidinit() {
super.setScaleType(SCALE_TYPE);
mReady = true;
if (mSetupPending) {
setup();
mSetupPending = false;
}
}
@Overridepublic ScaleType getScaleType() {
return SCALE_TYPE;
}
@OverridepublicvoidsetScaleType(ScaleType scaleType) {
if (scaleType != SCALE_TYPE) {
thrownewIllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
}
}
@OverridepublicvoidsetAdjustViewBounds(boolean adjustViewBounds) {
if (adjustViewBounds) {
thrownewIllegalArgumentException("adjustViewBounds not supported.");
}
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
if (getDrawable() == null) {
return;
}
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
if (mBorderWidth != 0) {
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
}
}
@OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setup();
}
publicintgetBorderColor() {
return mBorderColor;
}
publicvoidsetBorderColor(int borderColor) {
if (borderColor == mBorderColor) {
return;
}
mBorderColor = borderColor;
mBorderPaint.setColor(mBorderColor);
invalidate();
}
publicvoidsetBorderColorResource(@ColorResint borderColorRes) {
setBorderColor(getContext().getResources().getColor(borderColorRes));
}
publicintgetBorderWidth() {
return mBorderWidth;
}
publicvoidsetBorderWidth(int borderWidth) {
if (borderWidth == mBorderWidth) {
return;
}
mBorderWidth = borderWidth;
setup();
}
publicbooleanisBorderOverlay() {
return mBorderOverlay;
}
publicvoidsetBorderOverlay(boolean borderOverlay) {
if (borderOverlay == mBorderOverlay) {
return;
}
mBorderOverlay = borderOverlay;
setup();
}
@OverridepublicvoidsetImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
mBitmap = bm;
setup();
}
@OverridepublicvoidsetImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
mBitmap = getBitmapFromDrawable(drawable);
setup();
}
@OverridepublicvoidsetImageResource(@DrawableResint resId) {
super.setImageResource(resId);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}
@OverridepublicvoidsetImageURI(Uri uri) {
super.setImageURI(uri);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}
@OverridepublicvoidsetColorFilter(ColorFilter cf) {
if (cf == mColorFilter) {
return;
}
mColorFilter = cf;
mBitmapPaint.setColorFilter(mColorFilter);
invalidate();
}
private Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable == null) {
returnnull;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
try {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
} else {
bitmap = Bitmap
.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
}
Canvascanvas=newCanvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
returnnull;
}
}
privatevoidsetup() {
if (!mReady) {
mSetupPending = true;
return;
}
if (mBitmap == null) {
return;
}
mBitmapShader = newBitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mBitmapPaint.setAntiAlias(true);
mBitmapPaint.setShader(mBitmapShader);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStrokeWidth(mBorderWidth);
mBitmapHeight = mBitmap.getHeight();
mBitmapWidth = mBitmap.getWidth();
mBorderRect.set(0, 0, getWidth(), getHeight());
mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);
mDrawableRect.set(mBorderRect);
if (!mBorderOverlay) {
mDrawableRect.inset(mBorderWidth, mBorderWidth);
}
mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);
updateShaderMatrix();
invalidate();
}
privatevoidupdateShaderMatrix() {
float scale;
floatdx=0;
floatdy=0;
mShaderMatrix.set(null);
if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
scale = mDrawableRect.height() / (float) mBitmapHeight;
dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
} else {
scale = mDrawableRect.width() / (float) mBitmapWidth;
dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
}
mShaderMatrix.setScale(scale, scale);
mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top);
mBitmapShader.setLocalMatrix(mShaderMatrix);
}
}
And use it in your containt_activity.xml like this:
<com.example.customewidget.CircleImageView
android:id="@+id/imgUserImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/no_image_available"
app:border_color="@color/white"
app:border_width="1dp" />
Add in you Activity or Fragment class:
publicclassImageActivityextendsActivity {
private CircleImageView imgUserImage;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
setContentView(R.layout.containt_activity);
imgUserImage = (CircleImageView) view.findViewById(R.id.imgUserImage);
//here set your image view
}
Post a Comment for "My Circular Imageview Not Working Properly"