Skip to content

Instantly share code, notes, and snippets.

@folivares
Forked from bkurzius/CircularNetworkImageView
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save folivares/00fffd8b9388e65a3660 to your computer and use it in GitHub Desktop.

Select an option

Save folivares/00fffd8b9388e65a3660 to your computer and use it in GitHub Desktop.

Revisions

  1. folivares revised this gist Jul 7, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,13 @@ public class CircularNetworkImageView extends NetworkImageView {
    getCircularBitmap(bm)));
    }

    @Override
    public void setImageResource(int resId) {
    Bitmap circularImage = BitmapFactory.decodeResource(getResources(),
    resId);
    setImageBitmap(circularImage);
    }

    /**
    * 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
  2. @bkurzius bkurzius revised this gist Nov 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ public class CircularNetworkImageView extends NetworkImageView {

    /**
    * Creates a circular bitmap and uses whichever dimension is smaller to determine the width
    * <br/>Also onstrains the circle to the leftmost part of that image
    * <br/>Also constrains the circle to the leftmost part of the image
    *
    * @param bitmap
    * @return bitmap
  3. @bkurzius bkurzius revised this gist Nov 7, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@ import android.graphics.PorterDuff.Mode;
    import android.graphics.drawable.BitmapDrawable;
    import android.util.AttributeSet;

    import com.aetn.history.android.historyhere.utils.ImageUtils;
    import com.android.volley.toolbox.NetworkImageView;

    public class CircularNetworkImageView extends NetworkImageView {
  4. @bkurzius bkurzius revised this gist Nov 7, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package org.imemorize.widgets;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
  5. @bkurzius bkurzius revised this gist Nov 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package com.aetn.history.android.historyhere.widgets;
    package org.imemorize.widgets;

    import android.content.Context;
    import android.graphics.Bitmap;
  6. @bkurzius bkurzius revised this gist Nov 7, 2014. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package org.imemorize.widgets;
    package com.aetn.history.android.historyhere.widgets;

    import android.content.Context;
    import android.graphics.Bitmap;
    @@ -36,12 +36,14 @@ public class CircularNetworkImageView extends NetworkImageView {

    @Override
    public void setImageBitmap(Bitmap bm) {
    if(bm==null) return;
    setImageDrawable(new BitmapDrawable(mContext.getResources(),
    getCircularBitmap(bm)));
    }

    /**
    * creates a circular bitmap using the width of the image as the constrainer
    * Creates a circular bitmap and uses whichever dimension is smaller to determine the width
    * <br/>Also onstrains the circle to the leftmost part of that image
    *
    * @param bitmap
    * @return bitmap
    @@ -50,14 +52,13 @@ public class CircularNetworkImageView extends NetworkImageView {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    int width = bitmap.getWidth();
    if(bitmap.getWidth()>bitmap.getHeight())
    width = bitmap.getHeight();
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, width);
    final RectF rectF = new RectF(rect);
    final float roundPx = width / 2;

    paint.setAntiAlias(true);
  7. @bkurzius bkurzius renamed this gist Nov 7, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion CiccularNetworkImageView → CircularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,10 @@ public class CircularNetworkImageView extends NetworkImageView {
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = bitmap.getWidth() / 2;
    int width = bitmap.getWidth();
    if(bitmap.getWidth()>bitmap.getHeight())
    width = bitmap.getHeight();
    final float roundPx = width / 2;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
  8. @bkurzius bkurzius created this gist Nov 7, 2014.
    71 changes: 71 additions & 0 deletions CiccularNetworkImageView
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    package org.imemorize.widgets;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.Bitmap.Config;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.drawable.BitmapDrawable;
    import android.util.AttributeSet;

    import com.aetn.history.android.historyhere.utils.ImageUtils;
    import com.android.volley.toolbox.NetworkImageView;

    public class CircularNetworkImageView extends NetworkImageView {
    Context mContext;

    public CircularNetworkImageView(Context context) {
    super(context);
    mContext = context;
    }

    public CircularNetworkImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    mContext = context;
    }

    public CircularNetworkImageView(Context context, AttributeSet attrs,
    int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
    setImageDrawable(new BitmapDrawable(mContext.getResources(),
    getCircularBitmap(bm)));
    }

    /**
    * creates a circular bitmap using the width of the image as the constrainer
    *
    * @param bitmap
    * @return bitmap
    */
    public Bitmap getCircularBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = bitmap.getWidth() / 2;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
    }

    }