I have a ListView in which there is an ImageView, the image in the ImageView gets loaded dynamically after its fetched from the server.
Now, I want these images, of any size, to fit into a circular frame, how to do that?
Here's a sample pic of what I want
With the help of previous answer I came up with this solution.Hope it help others:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.widget.ImageView;
public class CircleImage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.circle_layout);
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.hair_four);
Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
img1.setImageBitmap(conv_bm);
// TODO Auto-generated method stub
}
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
Bitmap result = null;
try {
result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, 200, 200);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(50, 50, 50, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
} catch (NullPointerException e) {
} catch (OutOfMemoryError o) {
}
return result;
}
}
Try this code:
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
try {
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
color = 0xff424242;
paint = new Paint();
rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
rectF = new RectF(rect);
roundPx = pixels;
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);
} catch (NullPointerException e) {
// return bitmap;
} catch (OutOfMemoryError o){}
return result;
}
If you want an actual circle then you can pass 100px as parameter.
Update
There is a CircleImageView available on Github.
You can get latest version from Maven repository as add as a gradle dependency.
There are lots of tutorial regarding this. I think it will help.
https://github.com/lopspower/CircularImageView
https://github.com/wisemandesigns/CircularImageView
https://coderwall.com/p/hmzf4w
We can manage the height and width of an image from xml code and draw circle/oval from java code like
<ImageView
android:id="#+id/imageView1"
android:layout_width="#dimen/width"
android:layout_height="#dimen/height"
/>
for oval view
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.user_image);
Bitmap conv_bm = getRoundedBitmap(bm);
img1.setImageBitmap(conv_bm);
public static Bitmap getRoundedBitmap(Bitmap bitmap)
{
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}
Add following dependancies
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
implementation 'de.hdodenhof:circleimageview:3.0.0'
CircularImageView available for image fit in circle also if image is not looking proper .resize is for image resizing in circular image view.
CircleImageView img;
String Imageid;
Imageid="ImageName"; //String is not compulsary it may be drawable
Picasso.with(mContext)
.load(Imageid.get(position)) //Load the image
.error(R.drawable.ic_launcher_background) //Image resource for error
.resize(20, 20) // Post processing - Resizing the image
.into(img); // View where image is loaded.
public static Bitmap getCircleBitmap(Bitmap bitmap) {
final Bitmap circuleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getWidth(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(circuleBitmap);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return circuleBitmap;
}
Related
Hello I have this code to create a circular bitmap, but I also want to draw a circle around this circular bitmap.
I mean like the circular bitmap inside and a tiny colored stroke around the circular bitmap.
I have tryed different solutions but the circle doesn't get draw normally.
The bitmap will be used as a marker in a MapView instance.
Here is my code (this was a test code to try to draw a black circle under the circular bitmap)
public static Bitmap convertToCircularBitmap(Bitmap bitmap) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
final Paint p = new Paint();
final Rect r = new Rect(0, 0, bitmap.getWidth() + 10, bitmap.getHeight() + 10);
final RectF rF = new RectF(rect);
p.setColor(Color.BLACK);
canvas.drawOval(rF, p);
bitmap.recycle();
return output;
}
Thank you!
You can use bellow codes:
public static Bitmap convertToCircularBitmap(Bitmap bitmap, int strokeSize) {
int size = bitmap.getWidth() / 2;
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color =0xff424242;
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(color);
canvas.drawCircle(size,size,size-strokeSize,paint);
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
final int strokeColor = Color.rgb(0, 0, 0);
final Paint strokePaint = new Paint();
strokePaint.setAntiAlias(true);
strokePaint.setColor(strokeColor);
strokePaint.setStrokeWidth(strokeSize);
strokePaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(size,size,size-strokeSize,strokePaint);
return output;
}
I'm trying to do what is described here "I want to crop a circular region from this bitmap. All pixels outside the circle should be transparent."
I already trying what's in that post but all of them don't offer the transparent background, only a circle image, I tried the below and it didn't work, any ideas?
public Bitmap getCroppedCircleImage(Bitmap bmp, View view) {
Bitmap bitmap = getCroppedImage(bmp, view);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff227722;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
//canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
Paint paint1 = new Paint();
paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Rect rectTransparent=new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawRect(rectTransparent,paint1);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
OutputStream stream = null;
try {
stream = new FileOutputStream("/sdcard/test.png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
You can use this library to achieve your goal
https://github.com/hdodenhof/CircleImageView
I am fetching images from the server and they are in the square shape. But I want to show them in a circle in my application. I have tried it by making a Circle shape Drawable. But it is not working. Can anyone suggest me how this can be done.Any help would me much appreciated . Thanks.
try below code:-
Universal loader
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(config);
DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(60)).build();
ImageLoader.getInstance().displayImage(Uri.parse(imgByURL).toString(), imgThumb, options);
or
imageViewUser.setImageBitmap(getCircleBitmap(bitmap));
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
public Bitmap getCircleBitmap(Bitmap bitmap)
{
Bitmap output;
Canvas canvas = null;
final int color = 0xffff0000;
final Paint paint = new Paint();
Rect rect = null;
if (bitmap.getHeight() > 501)
{
output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
rect = new Rect(0, 0, 500, 500);
}
else
{
System.out.println("output else =======");
bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
rect = new Rect(0, 0, 500, 500);
}
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 1);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
how to use Canvas to draw an image inside another image;
like this, look at the picture :
to put it in a map app v2 like this
marker = gmap.addMarker(new MarkerOptions().title("test")
.position(new LatLng(0, 0))
.snippet("snipet test")
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
I already draw a picture in a rectangle like this
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.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);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
how to do this
please help me
Set a clipping path for your canvas with the shape of the frame you want:
Path frame = new Path();
frame.addCircle(centerX, centerY, radius, Direction.CW);
canvas.getClipBounds(oldClipBounds); //see below
canvas.clipPath(frame);
Everything you draw into the canvas afterwards will not be visible if it's outside of the circle.
If you need additional drawing, which should take place only outside of this frame, you can protect it later on by:
canvas.clipRect(oldClipBounds, Region.Op.REVERSE_DIFFERENCE);
I found a solution for this problem:
you put the image in a circle with canvas after that you resize the result with the exact size, and put it inside the marker image.
public class IconMarker {
public IconMarker(){}
public Bitmap DrawMarker(String userid,int typeid,Resources rcs){
// image from database: ...InputStream inputStream = connection.getInputStream();
//Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Bitmap img1=new UserInfoBd().getPhotoProfil(userid);
if(img1==null) img1=BitmapFactory.decodeResource(rcs,R.drawable.espace_photo);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = BitmapFactory.decodeResource(rcs,
typeid);
Bitmap output = Bitmap.createBitmap(bmp.getWidth(),
bmp.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas1 = new Canvas(output);
canvas1.drawBitmap(bmp, 0,0, null);
Bitmap output1 = Bitmap.createBitmap(img1.getWidth(),
img1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output1);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, img1.getWidth(), img1.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(img1, rect, rect, paint);
Bitmap img=getResizedBitmap(output1,bmp.getHeight()*3/4-4,bmp.getWidth()*3/4);
canvas1.drawBitmap(img,(float)4.7,(float)3.5,null);
return output;
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
}
I am using this function to get rounded images and setting to imageview its working fine for square shaped bitmaps but for rectangular shaped bitmaps its not making rounded images and shapes are uneven
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
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(10, 10,150,150);
final float roundPx = pixels;
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;
}
it sounds like similar to your code. but it is working for square and rectangle shaped Bitmaps. try this...
public static Bitmap getRoundedBitmap(Bitmap bitmap, int cornerRadius) {
if (bitmap == null) {
return null;
}
if (cornerRadius < 0) {
cornerRadius = 0;
}
// Create plain bitmap
Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawARGB(0,0,0,0);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect);
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return canvasBitmap;
}
do this operation in non UI thread
RoundedImageView .java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
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;
}
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0,0, null);
}
public 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;
}
}
And call the Bitmap by
imgview.setImageBitmap(new RoundedImageView(getApplicationContext()).getCroppedBitmap(PassedImage, 100));