Create a border outside the circular ImageView - android

I have created a circular ImageView, but I need to add a border outside the image.
Here is the code:
Bitmap circleBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader (bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setColor(Color.parseColor("#BAB399"));
Canvas c = new Canvas(circleBitmap);
c.drawARGB(0, 0, 0, 0);
c.drawCircle(50, 40,40, paint);
Could anybody help me to create a border outside the circular image?

First you create a circle shape like this,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="#333440" android:endColor="#333440"
android:angle="270"/>
</shape>
Then add a relative layout and add an imageview to it.Arrange it to the center of relative layout.And set this circle shape as Imageview's background.Then place your circular imageview above previously added imageview.Arrange it also to center.By changing your circular imageview margin you will get the desired border effect.
final code,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView14"
android:background="#drawable/circ"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView15"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/linked"
android:layout_margin="5dp" />
</RelativeLayout>
</LinearLayout>

Hi guys I had the same trouble But i managed with something like this.
I am sharing may be it will help.
public static LayerDrawable borderImageCircleBorder(Drawable draw, Activity activity) {
int xCordinate = returnImageDrawableSize(draw);
ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
biggerCircle.setIntrinsicHeight(xCordinate);
biggerCircle.setIntrinsicWidth(xCordinate);
biggerCircle.setBounds(new Rect(0, 0, xCordinate, xCordinate));
biggerCircle.getPaint().setColor(Color.WHITE);
biggerCircle.setPadding(4, 4, 4, 4);
ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
smallerCircle.setIntrinsicHeight(xCordinate);
smallerCircle.setIntrinsicWidth(xCordinate);
smallerCircle.setBounds(new Rect(0, 0, xCordinate, xCordinate));
smallerCircle.getPaint().setColor(Color.LTGRAY);
smallerCircle.setPadding(2, 2, 2, 2);
Drawable dd = getRoundedCornerBitmap(draw, activity);
Drawable[] d = { smallerCircle, biggerCircle, dd };
LayerDrawable composite = new LayerDrawable(d);
return composite;
}
public static int returnImageDrawableSize(Drawable image) {
Bitmap original = ((BitmapDrawable) image).getBitmap();
Bitmap bitmap = original;
//Log.i("original", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
int cropSizeWrtWidth, croppWrtHeight, xCoordinate, devideBy = 2;
cropSizeWrtWidth = bitmap.getWidth() / devideBy - bitmap.getHeight() / devideBy;
croppWrtHeight = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
xCoordinate = cropSizeWrtWidth > 0 ? cropSizeWrtWidth : croppWrtHeight;
float cordinates = bitmap.getWidth() / 2;
// Find out ratio until we get a square bitmap
while (bitmap.getHeight() > bitmap.getWidth() && xCoordinate + bitmap.getWidth() > bitmap.getWidth()) {
devideBy += 1;
xCoordinate = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
if (xCoordinate + bitmap.getWidth() <= bitmap.getWidth())
break;
}
return xCoordinate;
}
public static Drawable getRoundedCornerBitmap(Drawable image, Activity activity) {
Bitmap original = ((BitmapDrawable) image).getBitmap();
;
Bitmap bitmap = original;
//Log.i("original", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
int cropSizeWrtWidth, croppWrtHeight, xCoordinate, devideBy = 2;
cropSizeWrtWidth = bitmap.getWidth() / devideBy - bitmap.getHeight() / devideBy;
croppWrtHeight = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
xCoordinate = cropSizeWrtWidth > 0 ? cropSizeWrtWidth : croppWrtHeight;
float cordinates = bitmap.getWidth() / 2;
// Find out ratio until we get a square bitmap
while (bitmap.getHeight() > bitmap.getWidth() && xCoordinate + bitmap.getWidth() > bitmap.getWidth()) {
devideBy += 1;
xCoordinate = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
if (xCoordinate + bitmap.getWidth() <= bitmap.getWidth())
break;
}
if (bitmap.getWidth() >= bitmap.getHeight()) {
cordinates = bitmap.getHeight() / 2;
bitmap = Bitmap.createBitmap(bitmap, xCoordinate, 0, bitmap.getHeight(), bitmap.getHeight());
} else {
bitmap = Bitmap.createBitmap(bitmap, xCoordinate, 0, bitmap.getWidth(), bitmap.getWidth());
}
//Log.i("bitmap after crop", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
int color = 0xff424242;
Paint paint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);
Canvas canvas = new Canvas(output);
canvas.drawARGB(0, 0, 0, 0);
//Log.i("coordinates", "?????????" + cordinates);
canvas.drawCircle(cordinates, cordinates, cordinates, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, rect, rect, paint);
// output =
// ShadowView.getCircleWhiteBorderBitmap(original,output,convertDpToPixel(12,activity));
Drawable d = new BitmapDrawable(activity.getResources(), output);
return d;
}

I found a library that doing exactly what you wish, worked fine for me. Check it out. https://android-arsenal.com/details/1/932

Related

How to set an image into a circle?

I found the following code in one of the prvious topics:
public Bitmap getLetterTile(String displayName, String key, int width, int height) {
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final char firstChar = displayName.trim().charAt(0);
final Canvas c = mCanvas;
c.setBitmap(bitmap);
c.drawColor(pickColor(key));
mFirstChar[0] = Character.toUpperCase(firstChar);
mPaint.setTextSize(mTileLetterFontSize);
mPaint.getTextBounds(mFirstChar, 0, 1, mBounds);
c.drawText(mFirstChar, 0, 1, width / 2, height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint);
return bitmap;
}
private int pickColor(String key) {
final int color = Math.abs(key.hashCode()) % NUM_OF_TILE_COLORS;
try {
return mColors.getColor(color, Color.BLACK);
} finally {
mColors.recycle();
}
}
public Bitmap getCircularBitmap(Bitmap bitmap) {
Bitmap output;
if (bitmap.getWidth() > bitmap.getHeight()) {
output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
} else {
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());
float radius = 0;
if (bitmap.getWidth() > bitmap.getHeight()) {
radius = bitmap.getHeight() / 2;
} else {
radius = bitmap.getWidth() / 2;
}
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(radius, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
It's basically creates a circle with the first letter of the name in the menu. If the getLetterTile fails, I want it to set a default method. It can fail if the displayName is empty. I have a profile default avatar picture but I want it to be in a circle as the image is not in circle:
How can I use the default image and set it into the circle?
In the XML:
<ImageView
android:id="#+id/user_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
From XML you said?
Try Below.
<androidx.cardview.widget.CardView
app:cardCornerRadius="35dp"
app:cardElevation="0dp"
android:layout_width="70dp"
android:layout_height="70dp">
<ImageView
android:id="#+id/user_avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/pro_pic"
android:scaleType="fitXY"
android:contentDescription="#string/todo" />
</androidx.cardview.widget.CardView>
Just incase you don't have, simply add cardView to your gradle, also I don't know if you're on androidX or below, implementation still same anyways.
implementation "androidx.cardview:cardview:1.0.0"

how to set rounded white border in Imageview in android

public Bitmap getRoundedShape1(Bitmap scaleBitmapImage) {
int targetWidth = 65;
int targetHeight = 65;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(targetBitmap,Shader.TileMode.CLAMP, Shader.TileMode.REPEAT);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),((float) targetHeight)) / 2),Path.Direction.CCW);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
paint.setStyle(Paint.Style.STROKE);
canvas.clipPath(path);
Bitmap sourceBitmap = targetBitmap;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,targetHeight), null);
return targetBitmap;
}
String imagePath = Environment.getExternalStorageDirectory().toString()
+ "/.LociiImages/" + member_id + ".jpg";
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
BitmapDescriptor icon = BitmapDescriptorFactory
.fromBitmap(getRoundedShape(getResizedBitmap(bitmap, 100, 100)));
using this code i m able to display Marker as rounded image but i want to set Border around that image i m trying to set border but unable to do that .
My Current Screen :
Desire Screen:
Here is what I did to get a circular bitmap with a border:
public static Bitmap getCircleBitmap(Bitmap bitmap, boolean border) {
int width=0;
int height=0;
int borderWidth=6;
final Paint paint = new Paint();
float radius = 0;
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);
}
if (bitmap.getWidth() > bitmap.getHeight()) {
radius = output.getHeight() / 2;
} else {
radius = output.getWidth() / 2;
}
if(border){
width = output.getWidth() + borderWidth;
height = output.getHeight() + borderWidth;
}
else{
width = output.getWidth();
height = output.getHeight();
}
Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawCircle(width/2, height/2, radius, paint);
paint.setShader(null);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(0xff684321);
if(border){
paint.setStrokeWidth(borderWidth);
canvas.drawCircle(width/2, height/2, radius - borderWidth / 2, paint);
}
return canvasBitmap;
}
I have written this method in a class as static and I just directly call it, with required variables, whenever I need a bitmap to be circular.
You can change the colour in paint.setColor(0xff684321); or you can even pass that as a parameter in method to make the method more dynamic.
It's often easier to use xml rather than do it in code.
Set the background of your ImageView to be something similar to this xml shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:height="20dp"
android:width="20dp"/>
<stroke
android:color="#color/white"
android:width="2dp"/>
</shape>
You can change the height and width values in size based on the size of your imageview. The stroke would draw a border around the ImageView.

Circular Quick Contact Badge

Hi guys I a have contact list view with Quick contact badge.It displaying square images but my requirement is to show circular quick contact badge (like below screen shot). Plz let me know how can I achieve this. Thanks in advance.
Below are two methods for creating circular images. you just have to pass bitmap image to make it circular.
/**
* To make image in a round shape. Use this method if you want to specify required height and width
*
* #param i
*/
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int i) {
int targetWidth = i;
int targetHeight = i;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
/**
* To make image in a round shape
*/
public static 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;
}
Now a days, you should look into RoundedBitmapDrawable. A round drawable can easily be created by
Bitmap bitmap = <the bitmap you want to be circular>
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
Don't forget (Thanks Gil SH for pointing this out):
drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
Source: https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawable.html

set boarder to circled image issue

I found couple of example of how to create circled image and took the easiest one.
I'm trying to create a black boarder on the circular image but I can't see it I think.
How to draw a black boarder to the new circular image.
this is my code:
public Bitmap getCircleBitmap(Bitmap bitmap){
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader (bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
Path path = new Path();
path.addCircle(((float) bitmap.getWidth()) / 2,((float) bitmap.getHeight()) / 2 ,
(Math.min(((float) bitmap.getWidth()),((float) bitmap.getHeight())) / 2),Path.Direction.CCW);
c.clipPath(path);
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2 , bitmap.getWidth()/2, paint);
return circleBitmap;
}
thanks.
Hope it helps you, This is what worked for me..
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int radius = Math.min(h / 2, w / 2);
Bitmap output = Bitmap.createBitmap(w + 8, h + 8, Config.ARGB_8888);
Paint p = new Paint();
p.setAntiAlias(true);
Canvas c = new Canvas(output);
c.drawARGB(0, 0, 0, 0);
p.setStyle(Style.FILL);
c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);
p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
c.drawBitmap(bitmap, 4, 4, p);
p.setXfermode(null);
p.setStyle(Style.STROKE);
p.setColor(Color.WHITE);
p.setStrokeWidth(3);
c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);
return output;
try this..
public static Bitmap getCircularBitmap(Bitmap bitmap) {
if (bitmap == null || bitmap.isRecycled()) {
return null;
}
float radius = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap
.getHeight()) / 2f : ((float) bitmap.getWidth()) / 2f;
Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
radius, paint);
return canvasBitmap;
}
Try this,
create circle.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#ff000000"/>
</shape>
set this circle drawable as background to view.

Canvas draw rectangle android

public static Bitmap getCircularBitmapWithWhiteBorder(Bitmap bitmap,
int borderWidth) {
if (bitmap == null || bitmap.isRecycled()) {
return null;
}
final int width = bitmap.getWidth() + borderWidth;
final int height = bitmap.getHeight() + borderWidth;
Bitmap canvasBitmap = Bitmap.createBitmap(width*2, height, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(canvasBitmap);
float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
canvas.drawCircle(width / 2, height / 2, radius, paint);
paint.setShader(null);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(borderWidth);
canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2, paint);
return canvasBitmap;
}
I have circular image, i want to attach next to circular image sort of a rectangle..similar to his
this is a circle, next to it there is a rectangle attached. how can I do this?
You could draw your Rectangle more or less like this:
canvas.drawRect(width/2 + radius, height/2 - radius, width , height, paint);
just adjust the parameters for positioning it correctly if needed

Categories

Resources