Java代码:MyImageView.java
package com.zjbk.utils;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.ImageView;public class MyImageView extends ImageView { private String namespace = "http://shadow.com"; private int color; public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); color = Color.parseColor(attrs.getAttributeValue(namespace, "BorderColor")); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 画边框 Rect rec = canvas.getClipBounds(); rec.bottom--; rec.right--; Paint paint = new Paint(); paint.setColor(color); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(rec, paint); }}
布局文件:main.xml
调用方法和ImageView相同