RoundedBitmapDrawable是系统提供的类,用来实现圆角
package androidx.core.graphics.drawable;
/**
* A Drawable that wraps a bitmap and can be drawn with rounded corners. You can create a
* RoundedBitmapDrawable from a file path, an input stream, or from a
* {@link android.graphics.Bitmap} object.
* <p>
* Also see the {@link android.graphics.Bitmap} class, which handles the management and
* transformation of raw bitmap graphics, and should be used when drawing to a
* {@link android.graphics.Canvas}.
* </p>
*/
public abstract class RoundedBitmapDrawable extends Drawable {
.....
}
效果:
用法:
ImageView imageView = findViewById(R.id.rounded_bitmap_drawable_circle);
RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.magazine));
circleDrawable.setCircular(true);
imageView.setImageDrawable(circleDrawable);
imageView = findViewById(R.id.rounded_bitmap_drawable_corner);
circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.magazine));
circleDrawable.setCornerRadius(50f);
imageView.setImageDrawable(circleDrawable);
原理:
使用BitmapShader实现圆角Bitmap