Android 带边框颜色的圆形图像视图
我有以下代码使ImageView循环。如何使用颜色实现边框?我试图实施一些解决方案,也包括Stackoverflow上的解决方案,但没有成功。以下是目前的进展情况:Android 带边框颜色的圆形图像视图,android,imageview,Android,Imageview,我有以下代码使ImageView循环。如何使用颜色实现边框?我试图实施一些解决方案,也包括Stackoverflow上的解决方案,但没有成功。以下是目前的进展情况: public class RoundedImageView extends ImageView { public RoundedImageView(Context context) { super(context); // TODO Auto-generated constructor stu
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();
int radius = w < h ? w : h;
Bitmap roundBitmap = getCroppedBitmap(bitmap, radius, w, h);
// roundBitmap= ImageUtils.setCircularInnerGlow(roundBitmap, 0xFFBAB399,
// 4, 1);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius, int w, int h) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float _w_rate = 1.0f * radius / bmp.getWidth();
float _h_rate = 1.0f * radius / bmp.getHeight();
float _rate = _w_rate < _h_rate ? _h_rate : _w_rate;
sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() * _rate), (int)(bmp.getHeight() * _rate), 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(w / 2, h / 2, (w < h ? w : h) / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
public类RoundedImageView扩展了ImageView{
public RoundedImageView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
public RoundedImageView(上下文、属性集属性){
超级(上下文,attrs);
}
public RoundedImageView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
@凌驾
受保护的void onDraw(画布){
Drawable Drawable=getDrawable();
if(可绘制==null){
返回;
}
如果(getWidth()==0 | | getHeight()==0){
返回;
}
位图b=((BitmapDrawable)drawable.getBitmap();
位图Bitmap=b.copy(Bitmap.Config.ARGB_8888,true);
int w=getWidth(),h=getHeight();
int半径=w
以下是帮助您的几个步骤:p
1-在drawables文件夹中创建一个可绘制的XML文件,指定纯色和半径
2-将ImageView放置在高于ImageView尺寸的RoundImageView后面
3-将可绘制图像加载到新的ImageView中。以下是我用来实现带彩色边框的圆形图像视图的代码。希望它能帮助你