自定义ImageView圆角

附上原文链接https://www.jianshu.com/p/626dbd93207d

自定义View代码块

package com.qjtc.arcamera.utils.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.qjtc.arcamera.R;


public class RoundImageView extends ImageView {


    //圆角大小,默认为0
    private int mBorderRadius = 0;

    private Paint mPaint;

    // 3x3 矩阵,主要用于缩小放大
    private Matrix mMatrix;
    private BitmapShader mBitmapShader;

    public RoundImageView(Context context) {
        this(context,null);
    }

    public RoundImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
        mMatrix = new Matrix();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

    }

    private void init(Context context, AttributeSet attrs) {
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.circle_progress_image_attrs,0,0);
        mBorderRadius = array.getInteger(R.styleable.circle_progress_image_attrs_circle, 0);
        Log.e("TAG","dimensionPixelOffset:"+mBorderRadius);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null){
            return;
        }

        Bitmap bitmap = drawableToBitamp(getDrawable());
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale = 1.0f;

        if (!(bitmap.getWidth() == getWidth() && bitmap.getHeight() == getHeight())) {
//             如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
            scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
        }

//         shader的变换矩阵,我们这里主要用于放大或者缩小
        mMatrix.setScale(scale, scale);
        // 设置变换矩阵
        mBitmapShader.setLocalMatrix(mMatrix);
        // 设置shader
        mPaint.setShader(mBitmapShader);
        canvas.drawRoundRect(new RectF(0,0,getWidth(),getHeight()), mBorderRadius, mBorderRadius, mPaint);

    }

    private Bitmap drawableToBitamp(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }

        int w = drawable.getIntrinsicWidth() <= 0 ? getWidth() : drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight() <= 0 ? getHeight() : drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h); drawable.draw(canvas);
        return bitmap;

    }
}

在  res ---- values 下创建attrs.xml  主标签为  

    
        
    

布局文件     roundiv为临时声明:主要是后面地址。roundiv才有用



    
    

Activity调用代码块

        RoundImageView imageView = findViewById(R.id.image);
        String avatarUrl ="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1516644385815&di=c0552674db9f07a5f889d7c0980e33db&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170529%2F83d3ce719e9d4c0a8f1cd033ecac3692_th.jpg";
        Glide.with(this).load(avatarUrl).into(imageView);

 

你可能感兴趣的:(自定义ImageView圆角)