繁体   English   中英

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

[英]How to add a border around RoundedImageView custom ImageView implementation

我需要详细说明它是如何进行的。 我想用代码修改下面的类。

我想在圆角的图像视图实现周围添加白色边框/描边。 我不想用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;
}

}

使用此代码:

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

}

您可以根据需要进行修改

希望这可以帮助。

只需使用简单的XML drawable作为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 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;
    }

让我知道你是否遇到任何问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM