How to convert a bitmap to a RoundRect bitmap? - android

I'm trying to convert Bitmap to RoundRect shape. And i'm confused to use
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.saveLayer();
It's ok if I drawRoundRect first.
public Bitmap transform(Bitmap bitmap) {
Drawable drawable = new BitmapDrawable(Resources.getSystem(), bitmap);
Bitmap destBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(destBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
RectF srcRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawRoundRect(srcRect, mRadiusX, mRadiusY, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.saveLayer(srcRect, paint, Canvas.ALL_SAVE_FLAG);
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawable.draw(canvas);
canvas.restore();
return destBitmap;
}
But, it dosen't work if I draw bitmap first. Why?
public Bitmap transform(Bitmap bitmap) {
Drawable drawable = new BitmapDrawable(Resources.getSystem(), bitmap);
Bitmap destBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(destBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
RectF srcRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawable.draw(canvas);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.saveLayer(srcRect, paint, Canvas.ALL_SAVE_FLAG);
canvas.drawRoundRect(srcRect, mRadiusX, mRadiusY, paint);
canvas.restore();
return destBitmap;
}
Also, I don't want to create a new Bitmap, can I drawRoundRect on the bitmap directly to convert roundRect shape? like this:
public Bitmap transform(Bitmap bitmap) {
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
Path path = new Path();
path.addRoundRect(0, 0, bitmap.getWidth(), bitmap.getHeight(),50, 50,
Path.Direction.CW);
canvas.clipPath(path);
return bitmap;
}
Doesn't work.
I have some questions:
Do I have to "saveLayer" and "restore" if I use "PorterDuffXfermode"?
Do I have to create a new Bitmap, and draw on it ? can I draw the src Bitmap which is mutable?

Related

Android draw circle around Circular Bitmap

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;
}

Default black border in Canvas with Bitmap

I want to use Bitmap with rounded using Canvas, but getting black border by default.
How can I remove this border.
Code
public Bitmap transform(final Bitmap source)
{
Drawable drawable = context.getResources().getDrawable(R.drawable.dummy_image);
float scale = Math.min(((float)drawable.getIntrinsicHeight() / source.getWidth()), ((float) drawable.getIntrinsicWidth() / source.getHeight()));
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(newBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
Bitmap output = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawCircle((drawable.getIntrinsicWidth())/2,(drawable.getIntrinsicHeight())/2, getDrawableSize()-24, paint);
if (source != output)
{
source.recycle();
}
return output;
}

Circle crop image with transparent background - Android

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

Showing images in a Circlular shape

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;
}

Android draw an image inside another image

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;
}
}

Categories

Resources