圆形ImageView,加上自定义属性,可以设置边框的颜色和宽度(8以上的版本可以用前景色加padding实现)
摘自不知道什么时候copy过来的代码(忘记来源了)~~
public class CustomCircleImageView extends android.support.v7.widget.AppCompatImageView {
private final String TAG = "TAG.CustomView";
/**
* 边框画笔的颜色
*/
private int frameColor;
/**
* 边框的宽度
*/
private float frameWidth;
private Paint mPaint = new Paint();
public CustomCircleImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomCircleImageView);
frameWidth = array.getDimension(R.styleable.CustomCircleImageView_frame_width, 6);
frameColor = array.getColor(R.styleable.CustomCircleImageView_frame_color, Color.GREEN);
array.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setAntiAlias(true);
//这里的颜色决定了边缘的颜色
mPaint.setColor(frameColor);
if ( getDrawable()== null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
// 注意内存泄漏,实际情况中不能这么写的
Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap().copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth();
int h = getHeight();
//圆形ImageView的半径为布局中的ImageView定义大小
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(w / 2, h / 2, w / 2, mPaint);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public 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 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.WHITE);
//第三个参数减去的数值为白边的宽度.
canvas.drawCircle(sbmp.getWidth() / 2, sbmp.getHeight() / 2, sbmp.getHeight() / 2 -frameWidth, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
自定义属性
<!--这是自定义属性-->
<declare-styleable name="CustomCircleImageView">
<attr name="frame_color" format="color"/>
<attr name="frame_width" format="dimension"/>
</declare-styleable>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<!--自定义属性,这个要加上-->
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#222222"
android:orientation="vertical">
<!--图片只能src,别用背景属性-->
<com.xxx.CustomCircleImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_2"
<!--这个是定义边框的颜色-->
app:frame_color="@color/colorPrimary"
<!--这个是定义边框的宽度-->
app:frame_width="3dp" />
<com.xxx.CustomCircleImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_1r"
app:frame_color="@color/colorAccent"
app:frame_width="@dimen/dp_10" />
</LinearLayout>
网友评论