Android 圆角图片 圆形图片

实现圆角图片

方法1:自定义view继承自Drawable

public class RoundDrawable extends Drawable {

    private Paint paint;
    private Bitmap bitmap;
    private RectF rectF;

    public RoundDrawable(Bitmap bitmap) {
        this.bitmap = bitmap;
        paint = new Paint();
        paint.setAntiAlias(true);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        rectF = new RectF(0,0,bitmap.getWidth()/2,bitmap.getHeight()/2);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.drawRoundRect(rectF,30,30,paint);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        rectF.set(left,top,right,bottom);
    }

    @Override
    public void setAlpha(int i) {
        paint.setAlpha(i);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

使用方法:imagView设置drawable

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.win);
        iv1.setImageDrawable(new RoundDrawable(bitmap));

 

方式2:自定义view继承自ImagView

public class RoundImageView extends AppCompatImageView {
    private Paint paint;
    private int width;
    private int height;
    private int radius = 30;
    private int shapeType; //0默认 1圆形 2方形
    public RoundImageView(Context context) {
        super(context);
        init(context,null);

    }

    public RoundImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

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

    private void init(Context context,AttributeSet attris) {
        //获取xml设置的属性值
        if (attris != null) {
            TypedArray array = context.obtainStyledAttributes(attris, R.styleable.myRoundImageViewStyle);
            radius = array.getDimensionPixelOffset(R.styleable.myRoundImageViewStyle_radius,radius);
            shapeType = array.getInteger(R.styleable.myRoundImageViewStyle_shape,0);
            Log.d("test","----radius="+radius+"--shapeType="+shapeType);
            array.recycle();
        }
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(0xffffffff);
        setDrawingCacheEnabled(true);
        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (shapeType == 0) {
            super.onDraw(canvas);
            return;
        }
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }
        width = getWidth();
        height = getHeight();
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        Bitmap bitmap = getBitMapFromView(drawable);
        drawBitMap(canvas,bitmap);
    }

    public Bitmap getBitMapFromView(Drawable drawable) {
        Bitmap bitmap;
        try {
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            } else if (drawable instanceof ColorDrawable) {
                bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
            } else {
                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressLint("WrongConstant")
    private void drawBitMap(Canvas canvas, Bitmap bitmap) {
        //叠加模式 底层方形图像,上层圆形,则显示圆形区域
        PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        int saveFlags = Canvas.MATRIX_SAVE_FLAG
                | Canvas.CLIP_SAVE_FLAG
                | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
                | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
                | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
        canvas.saveLayer(0, 0, width, height, null, saveFlags);
        if (shapeType == 1) { //画圆
            canvas.drawCircle(width/2,height/2,height/2 -1,paint);
        } else if (shapeType == 2) {
            RectF rect = new RectF(1,1,getWidth()-1,getHeight()-1);
            canvas.drawRoundRect(rect,radius+1,radius+1,paint);
        }
        paint.setXfermode(porterDuffXfermode);
        // 空间的大小 / bitmap 的大小 = bitmap 缩放的倍数
        float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
        float scaleHeight = ((float) getHeight()) / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        //bitmap 缩放
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        canvas.drawBitmap(bitmap,0,0,paint);
        canvas.restore();
    }
}

使用方法:先定义需要的属性

在values文件下新建resource资源文件attrs



    
        
        
            
            
            
        
    

在xml文件里面引入自定义view

Android 圆角图片 圆形图片_第1张图片

 

两种圆角方式,根据需要选取

******************************************

另外,最新的Glide可以显示圆角图片

使用方法:设定圆角值

RequestOptions options = new RequestOptions().transform(new CenterCrop(),new RoundedCorners(30));

加载图片

Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.default)
                .error(R.mipmap.default)
                .apply(options)
                .into(iv);

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(android,圆角图片)