如何在android的Imageview中设置圆形白色边框

如何在android的Imageview中设置圆形白色边框,android,Android,使用此代码,我可以将标记显示为圆形图像,但我想在该图像周围设置边框,我正在尝试设置边框,但无法做到这一点 我当前的屏幕: Desire Screen:以下是我为获得带边框的圆形位图所做的操作: public Bitmap getRoundedShape1(Bitmap scaleBitmapImage) { int targetWidth = 65; int targetHeight = 65; Bitmap targetBitma

使用此代码,我可以将标记显示为圆形图像,但我想在该图像周围设置边框,我正在尝试设置边框,但无法做到这一点

我当前的屏幕:


Desire Screen:

以下是我为获得带边框的圆形位图所做的操作:

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)));
我已经在一个类中以静态的形式编写了这个方法,每当我需要一个循环的位图时,我就直接用所需的变量调用它


您可以在
paint.setColor(0xff684321)中更改颜色
或者您甚至可以将其作为参数传递到方法中,以使方法更具动态性。

以下是我为获得带边框的圆形位图所做的操作:

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)));
我已经在一个类中以静态的形式编写了这个方法,每当我需要一个循环的位图时,我就直接用所需的变量调用它


您可以在
paint.setColor(0xff684321)中更改颜色使用xml通常比在代码中更容易

将ImageView的背景设置为类似于此xml形状:

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


可以根据imageview的大小更改大小中的高度和宽度值。笔划将在ImageView周围绘制边框。

使用xml通常比使用代码更容易

将ImageView的背景设置为类似于此xml形状:

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

可以根据imageview的大小更改大小中的高度和宽度值。笔划将在ImageView周围绘制一个边框。

检查