圆形头像控件

来源:互联网 发布:淘宝营销大学 编辑:程序博客网 时间:2024/05/16 05:58
import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Path;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;public class CircleImageView extends ImageView {private Path mPath = new Path();public interface LockScreenLayoutListener {    public void onUnLock();}public CircleImageView(Context context) {    this(context, null);}public CircleImageView(Context context, AttributeSet attrs) {    this(context, attrs, 0);}@SuppressLint("NewApi")public CircleImageView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);    if(android.os.Build.VERSION.SDK_INT>=11)    setLayerType(View.LAYER_TYPE_SOFTWARE, null);    this.setScaleType(ScaleType.CENTER_CROP);}@Overrideprotected void onDraw(Canvas canvas) {    float cx = getMeasuredWidth() / 2;    float cy = getMeasuredHeight() / 2;    float cr = cx < cy ? cx : cy;    mPath.reset();    mPath.addCircle(cx, cy, cr, Path.Direction.CCW);    canvas.clipPath(mPath);    super.onDraw(canvas);    mPath.reset();}}

0 0
原创粉丝点击