Android 如何在RoundeMageView自定义ImageView实现周围添加边框
我需要一个详细的解释,说明这是怎么做的。我想用代码修改下面的类。 我想在圆形图像视图实现周围添加白色边框/笔划。我不想在XML中这样做。我希望通过修改这个类以编程方式完成。我不知道该怎么做。到目前为止,我所尝试的一切都会产生一个方形边框,所以我需要你的帮助。 addWhiteBorder函数是我试图使用的,但它似乎工作不好Android 如何在RoundeMageView自定义ImageView实现周围添加边框,android,imageview,Android,Imageview,我需要一个详细的解释,说明这是怎么做的。我想用代码修改下面的类。 我想在圆形图像视图实现周围添加白色边框/笔划。我不想在XML中这样做。我希望通过修改这个类以编程方式完成。我不知道该怎么做。到目前为止,我所尝试的一切都会产生一个方形边框,所以我需要你的帮助。 addWhiteBorder函数是我试图使用的,但它似乎工作不好 public class RoundedImageView extends ImageView { public RoundedImageView(Context cont
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>
嗨,伙计们,我也有同样的问题,但我还是设法解决了这样的问题。
我在分享可能会有帮助
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;
}
公共静态层可绘制边框图像圆形顺序(可绘制绘图、活动){
int XCOLLITE=返回图像可绘制尺寸(绘制);
ShapeDrawable biggerCircle=新的ShapeDrawable(新的椭圆形());
大圆圈。集合内点八(XCOLODIM);
biggerCircle.setIntrinsicWidth(xCordinate);
biggerCircle.setBounds(新的矩形(0,0,xColominate,xColominate));
biggerCircle.getPaint().setColor(Color.WHITE);
设置填充(4,4,4,4);
ShapeDrawable smallerCircle=新的ShapeDrawable(新的椭圆形());
小圆集内点八(xCOLODIM);
smallerCircle.setIntrinsicWidth(xCordinate);
smallerCircle.setBounds(新的矩形(0,0,xCordinate,xCordinate));
smallerCircle.getPaint().setColor(Color.LTGRAY);
设置填充(2,2,2,2);
Drawable dd=getRoundedCornerBitmap(绘制、活动);
可绘制的[]d={smallerCircle,biggercle,dd};
LayerDrawable复合=新的LayerDrawable(d);
收益组合;
}
公共静态int returnImageDrawableSize(可绘制图像){
位图原始=((BitmapDrawable)图像).getBitmap();
位图=原始位图;
//Log.i(“原始”、“ht”+位图.getHeight()+”,wt=“+位图.getWidth());
int cropSizeWrtWidth,cropwrtheight,xCoordinate,devideBy=2;
cropSizeWrtWidth=bitmap.getWidth()/devideBy-bitmap.getHeight()/devideBy;
cropwrtheight=bitmap.getHeight()/devideBy-bitmap.getWidth()/devideBy;
xCoordinate=cropSizeWrtWidth>0?cropSizeWrtWidth:cropsWright;
float cordinates=bitmap.getWidth()/2;
//找出比例,直到我们得到一个正方形位图
while(bitmap.getHeight()>bitmap.getWidth()&&xCoordinate+bitmap.getWidth()>bitmap.getWidth()){
devideBy+=1;
xCoordinate=bitmap.getHeight()/devideBy-bitmap.getWidth()/devideBy;
如果(xCoordinate+bitmap.getWidth()0?cropSizeWrtWidth:CropWrTheRight;
float cordinates=bitmap.getWidth()/2;
//找出比例,直到我们得到一个正方形位图
while(bitmap.getHeight()>bitmap.getWidth()&&xCoordinate+bitmap.getWidth()>bitmap.getWidth()){
devideBy+=1;
xCoordinate=bitmap.getHeight()/devideBy-bitmap.getWidth()/devideBy;
如果(xCoordinate+bitmap.getWidth()=bitmap.getHeight()){
cordinates=bitmap.getHeight()/2;
bitmap=bitmap.createBitmap(bitmap,xCoordinate,0,bitmap.getHeight(),bitmap.getHeight());
}否则{
bitmap=bitmap.createBitmap(bitmap,xCoordinate,0,bitmap.getWidth(),bitmap.getWidth());
}
//Log.i(“裁剪后位图”,“ht”+位图.getHeight()+”,wt=“+位图.getWidth());
位图输出=Bitmap.createBitmap(Bitmap.getWidth()、Bitmap.getHeight()、Config.ARGB_8888);
int color=0xFF4242;
油漆=新油漆();
油漆。设置颜色(颜色);
paint.setAntiAlias(真);
画布=新画布(输出);
drawARGB(0,0,0,0);
//Log.i(“坐标“,”坐标“+”坐标);
帆布。画圈(科迪酸盐、科迪酸盐、科迪酸盐、油漆);
setXfermode(新的PorterDuffXfermode(Mode.SRC_IN));
Rect Rect=new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
绘制位图(位图、矩形、矩形、绘制);
//输出=
//getCircleHiteBorderbitMap(原始,输出,convertDpToPixel(12,活动));
Drawable d=新的BitmapDrawable(activity.getResources(),output);
返回d;
}
让我知道,如果你遇到任何问题在这里出错不起作用:canvas.drawCircle(rectF、cornerSizePx、cornerSizePx、paint);它不能接受rectF的参数