半圓角Imageview

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;

import android.util.AttributeSet;

public class TopRoundImageView extends androidx.appcompat.widget.AppCompatImageView {

private Path mPath;
private RectF mRectF;

/*圓角的半徑,依次爲左上角xy半徑,右上角,右下角,左下角*/
private float[] rids = {dp2px(6), dp2px(6), dp2px(6),dp2px(6), 0.0f, 0.0f, 0.0f, 0.0f,};


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

public TopRoundImageView(Context context, AttributeSet attrs) {
    this(context, attrs,0);
}

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

private void init(){
    mPath= new Path();
    mRectF= new RectF();
}

private   int dp2px(final float dpValue) {
    final float scale = this.getContext().getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

/**
 * 畫圖
 * @param canvas
 */
protected void onDraw(Canvas canvas) {

    int w = this.getWidth();
    int h = this.getHeight();

    mRectF.set(0, 0, w, h);
    mPath.addRoundRect(mRectF, rids, Path.Direction.CW);
    canvas.clipPath(mPath);
    super.onDraw(canvas);
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章