Android 设置图片为圆角或者圆形
自己写了一个工具类,使用自定义view,设置图片为圆角,当rect_adius=100时,圆角就会变成圆形
public class RoundImageView extends AppCompatImageView {
private final RectF roundRect = new RectF();
private float rect_adius = 10;
private final Paint maskPaint = new Paint();
private final Paint zonePaint = new Paint();
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
//根据属性名称获取对应的值,属性名称的格式为类名_属性名
rect_adius = typedArray.getFloat(R.styleable.RoundImageView_rect_adius, 10f);
init();
}
public RoundImageView(Context context) {
super(context);
init();
}
private void init() {
maskPaint.setAntiAlias(true);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
zonePaint.setAntiAlias(true);
zonePaint.setColor(Color.WHITE);
float density = getResources().getDisplayMetrics().density;
rect_adius = rect_adius * density;
}
public void setRectAdius(float adius) {
rect_adius = adius;
invalidate();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int w = getWidth();
int h = getHeight();
roundRect.set(0, 0, w, h);
}
@Override
public void draw(Canvas canvas) {
canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
super.draw(canvas);
canvas.restore();
}
}
在styles中添加
<declare-styleable name="RoundImageView">
<attr name="rect_adius" format="float" />
</declare-styleable>
还没有评论,来说两句吧...