圓形ImageView,加上自定義屬性,可以設(shè)置邊框的顏色和寬度(8以上的版本可以用前景色加padding實(shí)現(xiàn))
摘自不知道什么時(shí)候copy過(guò)來(lái)的代碼(忘記來(lái)源了)~~
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;
}
// 注意內(nèi)存泄漏探熔,實(shí)際情況中不能這么寫的
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);
//第三個(gè)參數(shù)減去的數(shù)值為白邊的寬度.
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"
<!--自定義屬性,這個(gè)要加上-->
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"
<!--這個(gè)是定義邊框的顏色-->
app:frame_color="@color/colorPrimary"
<!--這個(gè)是定義邊框的寬度-->
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>