我需要详细解释它是如何完成的。我希望使用代码修改下面的课程。
我想在圆形图像视图实现周围添加白色边框/笔触。我不想用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;
}
}
答案 0 :(得分: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;
}
}
您可以根据需要进行修改
希望这有帮助。
答案 1 :(得分:0)
只需使用简单的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做这件事。”没关系。
答案 2 :(得分: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;
}
如果你遇到任何问题,让我知道