如何自定义没有拉伸的圆角控件

之前见过很多朋友写的圆角图片控件,大家的重点基本上都是圆角上,而忽略了ScaleType对图片加载的影响,所以需要考虑。

下面的就是一个完整功能的圆角控件应该做的事情:

  • 1,获取Drawable
  • 2,转Bitmap
  • 3,根据视图大小和ScaleType将Bitmap重新加载
  • 4,利用PorterDuffXfermode的SRC_IN将圆角和Bitmap绘制出来

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
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.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class RoundImageView extends ImageView {

    private int r = 20;
    private Paint paint;

    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);
        paint = new Paint();
        setBackgroundColor(Color.TRANSPARENT);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.reset();
        Drawable drawable = getDrawable();
        Bitmap dstBitmap = null;
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            dstBitmap = bitmapDrawable.getBitmap().copy(Bitmap.Config.ARGB_8888, true);

            //根据ScaleType处理
            dstBitmap = scaleBitmap(dstBitmap, getScaleType(), getWidth(), getHeight());

            //添加圆角
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);
            bitmap.eraseColor(Color.TRANSPARENT);
            Canvas canvas1 = new Canvas(bitmap);

            paint.setColor(Color.BLUE);
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);
            canvas1.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), r, r, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas1.drawBitmap(dstBitmap, 0, 0, paint);

            canvas.drawBitmap(bitmap, 0, 0, null);
        } else {
            super.onDraw(canvas);
        }

    }

    public void setRoundR(int r) {
        this.r = r;
        invalidate();
    }

    public Bitmap scaleBitmap(Bitmap dstBitmap, ScaleType scaleType, int width, int height) {

        //fitxy和视图的宽高不相关
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        bitmap.eraseColor(Color.TRANSPARENT);
        Canvas canvas = new Canvas(bitmap);

        //完全拉伸
        if (scaleType == ScaleType.FIT_XY) {
            canvas.drawBitmap(dstBitmap, new Rect(0, 0, dstBitmap.getWidth(), dstBitmap.getHeight()), new Rect(0, 0, width, height), null);
        }

        //实际大小
        else if (scaleType == ScaleType.CENTER) {
            int left = (dstBitmap.getWidth() - width) / 2;
            int top = (dstBitmap.getHeight() - height) / 2;
            canvas.drawBitmap(dstBitmap, new Rect(left, top, left + width, top + height), new Rect(0, 0, width, height), null);
        }

        //左边或顶部对齐
        else if (scaleType == ScaleType.FIT_START) {
            float x = dstBitmap.getWidth() / (float) width;
            float y = dstBitmap.getHeight() / (float) height;

            if (x >= y) {
                canvas.drawBitmap(dstBitmap, new Rect(0, 0, dstBitmap.getWidth(), (int) (height * x)), new Rect(0, 0, width, height), null);
            } else {
                canvas.drawBitmap(dstBitmap, new Rect(0, 0, (int) (width * y), dstBitmap.getHeight()), new Rect(0, 0, width, height), null);
            }
        }

        //右边或底部对齐
        else if (scaleType == ScaleType.FIT_END) {

            float x = dstBitmap.getWidth() / (float) width;
            float y = dstBitmap.getHeight() / (float) height;

            if (x >= y) {
                canvas.drawBitmap(dstBitmap, new Rect(0, dstBitmap.getHeight() - (int) (height * x), dstBitmap.getWidth(), dstBitmap.getHeight()), new Rect(0, 0, width, height), null);
            } else {
                canvas.drawBitmap(dstBitmap, new Rect(dstBitmap.getWidth() - (int) (width * y), 0, dstBitmap.getWidth(), dstBitmap.getHeight()), new Rect(0, 0, width, height), null);
            }

        } else {

            final float scale = dstBitmap.getWidth() / (float) width - dstBitmap.getHeight() / (float) height;

            //图片宽高比比视图大
            //图片宽高比与视图相等
            if (scale >= 0) {

                if (scaleType == ScaleType.CENTER_INSIDE || scaleType == ScaleType.FIT_CENTER) {

                    //高的值
                    int top = (int) ((dstBitmap.getWidth() * height / (float) width - dstBitmap.getHeight()) / 2);
                    canvas.drawBitmap(dstBitmap, new Rect(0, -top, dstBitmap.getWidth(), dstBitmap.getHeight() + top), new Rect(0, 0, width, height), null);

                } else if (scaleType == ScaleType.CENTER_CROP) {

                    int left = (int) (((dstBitmap.getHeight() * width / (float) height) - dstBitmap.getWidth()) / 2);
                    canvas.drawBitmap(dstBitmap, new Rect(-left, 0, dstBitmap.getWidth() + left, dstBitmap.getHeight()), new Rect(0, 0, width, height), null);
                }

            }

            //图片宽高比比视图小
            else {

                if (scaleType == ScaleType.CENTER_INSIDE || scaleType == ScaleType.FIT_CENTER) {

                    //高的值
                    int left = (int) (((dstBitmap.getHeight() * width / (float) height) - dstBitmap.getWidth()) / 2);
                    canvas.drawBitmap(dstBitmap, new Rect(-left, 0, dstBitmap.getWidth() + left, dstBitmap.getHeight()), new Rect(0, 0, width, height), null);

                } else if (scaleType == ScaleType.CENTER_CROP) {

                    int top = (int) ((dstBitmap.getWidth() * height / (float) width - dstBitmap.getHeight()) / 2);
                    canvas.drawBitmap(dstBitmap, new Rect(0, -top, dstBitmap.getWidth(), dstBitmap.getHeight() + top), new Rect(0, 0, width, height), null);
                }

            }
        }
        return bitmap;

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值