Skip to content

Commit

Permalink
Issue #492 - fixing rounded images
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed May 16, 2014
1 parent 2245244 commit dfe8854
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void initNavigationDrawer() {
((TextView)findViewById(R.id.sidebar_username)).setText(pref.getString(Config.USERNAME, "User"));
ImageView image = (ImageView)findViewById(R.id.sidebar_avatar);

new ImageRequest(pref.getString(Config.PROFILE_IMAGE, ""), getApplicationContext(), image);
new ImageRequest(pref.getString(Config.PROFILE_IMAGE, "null"), getApplicationContext(), image);

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;


public class RoundedImageView extends ImageView implements Target {
public class RoundedImageView extends ImageView {

private Bitmap bitmap;

Expand All @@ -34,68 +35,4 @@ public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {

Drawable drawable = getDrawable();

if (drawable == null) {
return;
}

if (getWidth() == 0 || getHeight() == 0) {
return;
}
if( bitmap == null){
bitmap = ((BitmapDrawable)drawable).getBitmap() ;
}
Bitmap bitmapCopy = bitmap.copy(Bitmap.Config.ARGB_8888, true);

int width = getWidth(), height = getHeight();


Bitmap roundBitmap = getCroppedBitmap(bitmapCopy, width);
canvas.drawBitmap(roundBitmap, 0,0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if(bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
sbmp.getWidth() / 2+0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);


return output;
}

@Override
public void onBitmapFailed(Drawable arg0) {}

@Override
public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
bitmap = arg0;
}

@Override
public void onPrepareLoad(Drawable arg0) {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.megasoft.entangle.views;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

// enables hardware accelerated rounded corners
// original idea here : http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
public class RoundedTransformation implements com.squareup.picasso.Transformation {
private final int radius;
private final int margin; // dp

// radius is corner radii in dp
// margin is the board in dp
public RoundedTransformation(final int radius, final int margin) {
this.radius = radius;
this.margin = margin;
}

@Override
public Bitmap transform(final Bitmap source) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);

if (source != output) {
source.recycle();
}

return output;
}

@Override
public String key() {
return "rounded";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.widget.ImageView;

import com.google.android.gms.internal.im;
import com.megasoft.entangle.views.RoundedTransformation;
import com.squareup.picasso.Picasso;

/**
Expand All @@ -20,7 +21,7 @@ public ImageRequest(String url,Context context,ImageView imageView){
int id = context.getResources().getIdentifier("ic_action_person.png", "drawable", context.getPackageName());
imageView.setImageResource(id);
}else{
Picasso.with(context).load(url).into(imageView);
Picasso.with(context).load(url).transform(new RoundedTransformation(10, 0)).into(imageView);
}
}
}

0 comments on commit dfe8854

Please sign in to comment.