图片圆角 上图
上代码
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.v7.widget.AppCompatImageView; import android.util.AttributeSet; import com.szkrkj.appstore.R; /** * Created by Administrator on 2018/4/12. */ public class XRectangleView extends AppCompatImageView { private Paint paint; private Bitmap sbmp; private float left; private float top; private float right; private float bottom; public XRectangleView(Context context) { this(context, null); } public XRectangleView(Context context, AttributeSet attrs) { this(context, attrs,0); } public XRectangleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); } protected void onDraw(Canvas canvas) { int roundPx = 12;//圆角的大小,这个单位是像素 paint.setAntiAlias(true); paint.setColor(getResources().getColor(R.color.white)); //这里的颜色决定了边缘的颜色 Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } if(drawable instanceof BitmapDrawable){ Bitmap b = ((BitmapDrawable) drawable).getBitmap(); //图片处理质量 ARGB_4444 降低 Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(); int h = getHeight(); RectF rectF = new RectF(0, 0, w, h); Bitmap roundBitmap = getCroppedBitmap(bitmap, w, h,roundPx); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); canvas.drawBitmap(roundBitmap, 0, 0, null); } } public Bitmap getCroppedBitmap(Bitmap bmp, int lengthx, int lengthy,int roundPx) { if (bmp.getWidth() != lengthx || bmp.getHeight() != lengthy) sbmp = Bitmap.createScaledBitmap(bmp, lengthx, lengthy, false); else sbmp = bmp; Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); //left、right -:向右 +:向左 top、bottom -:向下 +:向上 final RectF rectF = new RectF(left,top,sbmp.getWidth()+right,sbmp.getHeight()+bottom); // final RectF rectF = new RectF(-3, 0, sbmp.getWidth()+3, sbmp.getHeight() - 2); final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(sbmp, rect, rect, paint); return output; } //这个方法是通过外部来控制边框的大小 public void setRectFSize(float left,float top,float right,float bottom){ this.left = left; this.top = top; this.right = right; this.bottom = bottom; } }
这里只是做个笔记,没有详解,直接拖代码,可以实现圆角