2

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

我想在圆形图像视图实现周围添加一个白色边框/笔触。我不想用 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;
}

}
4

3 回答 3

0

只需使用简单的 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 来做”。没关系。

于 2013-09-20T18:55:17.337 回答
0

使用此代码:

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

}

你可以根据你的需要修改它

希望这可以帮助。

于 2013-09-20T18:46:17.273 回答
0

嗨,伙计们,我遇到了同样的麻烦,但我设法解决了这样的问题。我正在分享可能会有所帮助。

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

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

于 2013-09-24T09:36:03.593 回答