This question already has answers here:
How to Make an ImageView in Circular Shape? [duplicate]
(2 answers)
Closed 8 years ago.
i am trying to convert imageview into 60px circular image but its not happening ...
the way i am trying is...
File imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp" + File.separator + "profile_" + userId + ".jpg");
if(imgFile.exists()){
Bitmap correctBmp=null;
ExifInterface exif;
try {
exif = new ExifInterface(imgFile.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(90);
Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(imgFile), null, null);
/* correctBmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);*/
correctBmp = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(correctBmp);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(bmp1, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect((new RectF(0.0f, 0.0f, bmp1.getWidth(), bmp1.getHeight())), 10, 10, paint);
profileImage.setImageBitmap(correctBmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
it is loading rectangle shape image so what should i do ?
Please help...
here is my code and it works for me:
public class ImageHelper {
public Bitmap getCroppedBitmap(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());
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(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
}
just create a class and put this code in it, and then you can use it like this:
// create round image
public void setCircularImage() {
ImageView imv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitImg = BitmapFactory.decodeResource(getResources(), R.drawable.icon_person);
ImageHelper imgHlp = new ImageHelper();
imv.setImageBitmap(imgHlp.getCroppedBitmap(bitImg));
}
For that the better option you have universal image loader.
you can easily make image rounded with create an option of image loader
options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(50))
.showStubImage(R.drawable.ic_app)
.showImageForEmptyUri(R.drawable.camera)
.showImageOnFail(R.drawable.ic_error)
.cacheOnDisc()
.build();
you can take more reference here
see below link :-
Add border to getRoundedCornerBitmap android
or use Universal Loader
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.displayer(new RoundedBitmapDisplayer(60))
.build();//value which you want to round
ImageLoader.getInstance().displayImage(Uri.parse(imgByURL).toString(), imgThumb, options);
Related
We are showing a notification with a large icon get from an URL. So, in order to load the image we are using Universal Image Loader with the following code:
mImageLoader.loadImage(imagePath, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
notificationBuilder.setLargeIcon(loadedImage);
notificationManager.notify(notification.getId(), notificationBuilder.build());
}
});
This code is working fine and the image is successfully shown. Our problem is that we want to show a circular large icon, so we need the bitmap to be circular. We have tried by setting:
final DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(1000))
.build();
But this is not working, we've tried with RoundedBitmapDrawable, but we need a bitmap and not a bitmapdrawable.
Please, how could we do in order to get a circular bitmap?
Thanks in advance.
Try this:
public static Bitmap getCircularBitmap(Bitmap bitmap) {
Bitmap output;
if (bitmap.getWidth() > bitmap.getHeight()) {
output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
} else {
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());
float r = 0;
if (bitmap.getWidth() > bitmap.getHeight()) {
r = bitmap.getHeight() / 2;
} else {
r = bitmap.getWidth() / 2;
}
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Did you try setting the option .displayer(new CircleBitmapDisplayer()) ? For example:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.displayer(new CircleBitmapDisplayer())
.build();
I have an ImageView. I have done some manipulation to make it circular. All is good. But, I realize that the image is not centered. Apparently, the image is positioned from the top left of the ImageView. How can I make this image centered to the circular ImageView?
Here is my code:
// Decode the Byte[] into bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
// Set the Bitmap into the imageView
mImage.setImageBitmap(bmp);
//circle img
int wbmp = bmp.getWidth();
int hbmp = bmp.getHeight();
int diameter;
if (wbmp > hbmp) {
diameter = hbmp;
} else {
diameter = wbmp;
}
Bitmap resized = Bitmap.createScaledBitmap(bmp, wbmp, hbmp, true);
Bitmap conv_bm = ImageHelper.getRoundedRectBitmap(resized, diameter);
mImage.setImageBitmap(conv_bm);
// TODO Auto-generated method stub
//circle img ends
and this is the ImageHelper:
public class ImageHelper {
//circle image
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int radius) {
Bitmap result = null;
try {
Bitmap sbitmap;
if(bitmap.getWidth() != radius || bitmap.getHeight() != radius) sbitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,false);
else sbitmap = bitmap;
result = Bitmap.createBitmap(sbitmap.getWidth(), sbitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, sbitmap.getWidth(), sbitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(sbitmap.getWidth() / 2, sbitmap.getHeight() / 2,
sbitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
} catch (NullPointerException e) {
} catch (OutOfMemoryError o) {
}
return result;
}
//circle image ends
}
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;
}
}
Here is the map page I have, which shows all the users of my App. Also the images(markers) are obtained from the URL given from my server. These markers has to put inside a Drawable(a circle like image as shown). I created a circle like Bitmap from the url using Canvas.
public Drawable showMe(String url)
{
Bitmap bitmap=null;
try {
URL newurl = new URL(url);
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap=getBitmap(url);
Paint paint = new Paint();
paint.setFilterBitmap(true);
int targetWidth = 30;
int targetHeight = 30;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
RectF rectf = new RectF(0, 0, 30, 30);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRoundRect(rectf, targetWidth, targetHeight, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap( bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), paint);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 30, 30, matrix, true);
Bitmap bitmap_circle=mergeBitmaps(resizedBitmap);
BitmapDrawable bd = new BitmapDrawable(bitmap_circle);
return bd;
}
The above function will create the final drawable for the marker.Also the mergeBitmaps() function merge the both the resource drawable and the bit map together..
public Bitmap mergeBitmaps(Bitmap manBitmap){
try{
Bitmap markerBitmap = BitmapFactory.decodeResource( this.getResources(), R.drawable.circle_bg);
Bitmap bmOverlay = Bitmap.createBitmap(markerBitmap.getWidth(), markerBitmap.getHeight(), markerBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
canvas.drawBitmap(markerBitmap, matrix, null);
canvas.drawBitmap(manBitmap, 5, 5, null);
return bmOverlay;
}
catch(Exception ex){
ex.printStackTrace();
return null;
}
}
But the problem is, this bitmap is not best fit inside the background Drawable in order to get a feeling that both together will give a single image.
Can anyone help me ?
change
canvas.drawBitmap(manBitmap, 5, 5, null);
to smtg like
canvas.drawBitmap(manBitmap, (markerBitmap.getWidth()-manbitmap.getWidth())/2,
(markerBitmap.getHeight()-manbitmap.getHeight())/2, null);