package com.sclgxt.customimageview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.widget.ImageView; /** * Created by Sclgxt on 2016/5/5. * Mind:圆形图片,半径取值宽的一半,请勿高度大于宽度,否则估计会有问题,再者android:scaleType="centerCrop" */ public class CircleImageView extends ImageView { private Paint roundPaint; private Paint maskPaint; public CircleImageView(Context context) { this(context, null); } public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); init(); } private void init() { roundPaint = new Paint(); roundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); roundPaint.setAntiAlias(true); roundPaint.setColor(Color.WHITE); maskPaint = new Paint(); maskPaint.setAntiAlias(true); maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); } @Override protected void onDraw(Canvas canvas) { canvas.saveLayer(0, 0, getWidth(), getHeight(), roundPaint, Canvas.ALL_SAVE_FLAG); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, roundPaint); canvas.saveLayer(0, 0, getWidth(), getHeight(), maskPaint, Canvas.ALL_SAVE_FLAG); super.onDraw(canvas); }}
![]()
圆形图片CircleImageView
最新推荐文章于 2018-03-28 09:51:22 发布