Create a border outside the circular ImageView

2019-02-16 06:29发布

问题:

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?

回答1:

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>



回答2:

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


回答3:

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