1. 自定义ImageView
/**
* 自定义带边框的ImageView,可以设置颜色和宽度
* @author cuitongliang
*
*/
public class MyImageView extends ImageView{
private int color;
private int borderwidth;
private Canvas canvas = new Canvas();
private int status = 0;
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//设置颜色
public void setColor(int color){
this.color = color;
}
//设置边框宽度
public void setBorderWidth(int width){
borderwidth = width;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
//画边框
Rect rect = canvas.getClipBounds();
rect.bottom--;
rect.right--;
Paint paint = new Paint();
//设置边框颜色
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
//设置边框宽度
paint.setStrokeWidth(borderwidth);
canvas.drawRect(rect, paint);
}
}
2. 在Activity中定义MyImageView,并为它设定边框宽度和颜色
MyImageView imge = (MyImageView)findViewById(R.id.my_imge);
imge.setBorderWidth(10);
imge.setColor(Color.WHITE);