Android图片圆角 用简单的方法实现

package com.example.ysmb.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.ImageView;

/**  * Created by YSMB on 2016/2/16.  */ public class RoundImageView extends ImageView {
    private Path mPath;

    public RoundImageView(Context context) {
        super(context);
        init(context);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPath = new Path();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPath.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), 50, 50, Path.Direction.CW);
        canvas.save();
        canvas.clipPath(mPath);
        //这一行代码是为了去掉底部两个圆角
        canvas.clipRect(new RectF(0, getHeight() / 2, getWidth(), getHeight()), Region.Op.UNION);
        super.onDraw(canvas);
        canvas.restore();
    }
}

你可能感兴趣的:(android,图片,imageview,圆角,RoundImageView)