安卓 java方法将一张图片切成圆形

private void roundBitmap(){
    //如果是圆的时候,我们应该把bitmap图片进行剪切成正方形, 然后再设置圆角半径为正方形边长的一半即可
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.me);
    Bitmap bitmap = null;
    //将长方形图片裁剪成正方形图片
    if (image.getWidth() == image.getHeight()) {
        bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight());
    } else {
        bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth());
    }
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
    //圆角半径为正方形边长的一半
    roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2);
    //抗锯齿
    roundedBitmapDrawable.setAntiAlias(true);
    imageView.setImageDrawable(roundedBitmapDrawable);
}

猜你喜欢

转载自blog.csdn.net/qq_17358703/article/details/78638979