`

ImageView添加边框

阅读更多
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 RoundCornerImageView extends ImageView {
	private String namespace = "http://round.com";
	private int color;

	public RoundCornerImageView(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);
	}
}


这里要注意的是super.onDraw(canvas);在前,否则边框可能会被图片所覆盖。

<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:round="http://round.com"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
	<round.widget.RoundCornerImageView
		android:id="@+id/roundCornerImageView"
		shadow:BorderColor="GRAY"
		android:layout_width="80px"
		android:layout_height="60px"
		android:layout_alignParentRight="true"
		android:src="@drawable/icon"
		android:layout_centerInParent="true"
		android:layout_marginRight="3px"
		/>
</LinearLayout>

设置边框颜色 shadow:BorderColor="GRAY"
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics