如何在 RoundedImageView 自定义 ImageView 实现周围添加边框



我需要详细说明它是如何完成的。我希望使用代码修改下面的类。

我想在圆角图像视图实现周围添加白色边框/描边。我不想在XML中这样做。我希望通过修改此类以编程方式完成它。我不知道如何完成它。到目前为止,我尝试过的所有方法都会创建方形边框之类的东西,所以我需要您的帮助。addWhiteBorder函数是我尝试使用的,但它似乎不能很好地工作。

public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
    Drawable drawable = getDrawable();
    if (drawable == null) {
        return;
    }
    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
    int w = getWidth(), h = getHeight();

    Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);

    canvas.drawBitmap(roundBitmap, 0,0, null);

}
private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if(bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
            sbmp.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
            sbmp.getWidth() / 2+0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}
}

只需使用简单的 XML 可绘制对象作为 imageView 的背景。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android>
<stroke android:width="1dp" android:color="#000000" />
</shape>

编辑:好的,现在我读到"我不想用XML做。那没关系。

使用以下代码:

public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
    Drawable drawable = getDrawable();
    if (drawable == null) {
        return;
    }
    if (getWidth() == 0 || getHeight() == 0) {
        return; 
    }
    Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
    int w = getWidth(), h = getHeight();

    Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);
    canvas.drawBitmap(roundBitmap, 0,0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if(bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
            sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
            sbmp.getWidth() / 2+0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

            return output;
}
}

您可以根据需要对其进行修改

希望这有帮助。

嗨,

伙计们,我遇到了同样的麻烦,但我设法做到了这样的事情。我正在分享可能会有所帮助。

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

如果您遇到任何问题,请告诉我

最新更新