1. 程式人生 > >自定義ImageView實現圖片圓角效果

自定義ImageView實現圖片圓角效果

lamp onmeasure null wid resources mar ctf ppc fig

效果圖如下

技術分享圖片

代碼

package activity.yyzy.com.schoolsecondhand.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
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.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.TypedValue;

/**
 * Created by Administrator on 2019/5/11.
 */
public class CornerImageView extends AppCompatImageView {

private Paint mPaint;

private int mWidth;

private int mHeight;

private int mRadius;//圓半徑

private RectF mRect;//矩形凹行大小

private int mRoundRadius;// 圓角大小

private BitmapShader mBitmapShader;//圖形渲染

private Matrix mMatrix;

private int mType;// 記錄是圓形還是圓角矩形

public static final int TYPE_CIRCLE = 0;// 圓形
public static final int TYPE_ROUND = 1;// 圓角矩形
public static final int TYPE_OVAL = 2;//橢圓形
public static final int DEFAUT_ROUND_RADIUS = 10;//默認圓角大小

public CornerImageView(Context context) {
    this(context, null);
    // TODO Auto-generated constructor stub
}

public CornerImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    // TODO Auto-generated constructor stub
}

public CornerImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView();
}

private void initView() {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mMatrix = new Matrix();
    mRoundRadius = DEFAUT_ROUND_RADIUS;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // 如果是繪制圓形,則強制寬高大小一致
    if (mType == TYPE_CIRCLE) {
        mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = mWidth / 2;
        setMeasuredDimension(mWidth, mWidth);
    }

}

@Override
protected void onDraw(Canvas canvas) {
    if (null == getDrawable()) {
        return;
    }
    //第一步
    setBitmapShader();

    if (mType == TYPE_CIRCLE) {
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
    } else if (mType == TYPE_ROUND) {
        mPaint.setColor(Color.RED);
        canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);
    }else if(mType == TYPE_OVAL){
        canvas.drawOval(mRect, mPaint);
    }
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    // TODO Auto-generated method stub
    super.onSizeChanged(w, h, oldw, oldh);
    mRect = new RectF(0, 0, getWidth(), getHeight());
}

/**
 * 設置BitmapShader
 */
private void setBitmapShader() {
    Drawable drawable = getDrawable();
    if (null == drawable) {
        return;
    }
    Bitmap bitmap = drawableToBitmap(drawable);
    // 將bitmap作為著色器來創建一個BitmapShader
    mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    float scale = 1.0f;
    if (mType == TYPE_CIRCLE) {
        // 拿到bitmap寬或高的小值
        int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
        scale = mWidth * 1.0f / bSize;

    } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {
        // 如果圖片的寬或者高與view的寬高不匹配,計算出需要縮放的比例;縮放後的圖片的寬高,一定要大於我們view的寬高;所以我們這裏取大值;
        scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
    }
    // shader的變換矩陣,我們這裏主要用於放大或者縮小
    mMatrix.setScale(scale, scale);
    // 設置變換矩陣
    mBitmapShader.setLocalMatrix(mMatrix);
    mPaint.setShader(mBitmapShader);

}

/**
 * drawable轉bitmap
 *
 * @param drawable
 * @return
 */
private Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        return bitmapDrawable.getBitmap();
    }
    int w = drawable.getIntrinsicWidth();
    int h = 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;
}
/**
 * 單位dp轉單位px
 */
public int dpTodx(int dp){
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            dp, getResources().getDisplayMetrics());
}

public int getType() {
    return mType;
}


/**
 * 設置圖片類型:圓形、圓角矩形、橢圓形
 * @param mType
 */
public void setType(int mType) {
    if(this.mType != mType){
        this.mType = mType;
        invalidate();
    }

}

public int getRoundRadius() {
    return mRoundRadius;
}
/**
 * 設置圓角大小
 * @param mRoundRadius
 */
public void setRoundRadius(int mRoundRadius) {
    if(this.mRoundRadius != mRoundRadius){
        this.mRoundRadius = mRoundRadius;
        invalidate();
    }

}
}

自定義ImageView實現圖片圓角效果