The technical principles of the CircleImageView framework in the Java class library
CircleImageView is a framework commonly used in the Java class library, which is used to achieve circular pictures.This article will analyze the technical principles of CircleImageView and provide examples of Java code.
Technical principle:
The implementation of CircleImageView mainly depends on the following two aspects: inheriting the ImageView class and custom circular boundaries.
1. Inherit the ImageView class:
The CircleImageView class is inherited from ImageView, which allows us to directly use various functions of the ImageView class, such as pictures loading, zooming, etc.By inheriting the ImageView class, CircleImageView can easily handle the picture to be displayed and make a circular display.
2. Customized circular boundary:
In order to achieve a circular display effect, the CircleImageView requires a custom circular boundary.During the drawing process, a circular boundary is drawn through the DrawCircle () method of the Canvas class, and the picture is displayed inside this circular boundary.Because the default ImageView is a rectangular boundary, it is necessary to rewrite the ONDRAW () method to convert the rectangular boundary into a circular boundary.
The following is a simple CircleImageView sample code:
public class CircleImageView extends ImageView {
public CircleImageView(Context context) {
super(context);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap circularBitmap = getCircularBitmap(bitmap);
canvas.drawBitmap(circularBitmap, 0, 0, null);
}
private Bitmap getCircularBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
In the above sample code, the getcircularbitMap () method is created by creating a new Bitmap object output and drawing a circular boundary on it.Then use the drawbitMap () method to draw the pictures in the circular boundary to the output.
By inheriting the method of inheriting the ImageView class and custom circular boundaries, the CircleImageView can achieve the effect of a round display image.This is very useful in many scenes that require circular pictures, such as user avatar display in chat applications.
Summarize:
CircleImageView is a widely used framework in the Java class library. Its technical principle relies on inheriting the IMAGEVIEW class and custom circular boundaries.By inheriting the ImageView class, CircleImageView can easily handle the picture to be displayed.By customizing the circular boundary, the CircleImageView can achieve the display effect of circular pictures.The Java code example shows how to achieve a simple CircleImageView.For scenes that need to be displayed in round pictures, CircleImageView is a very practical framework.