Introduction to the technical principles of the CircleImageView framework in the Java class library
The CircleImageView framework in the Java class library is a commonly used custom view control to display round pictures.This article will profile the CircleImageView framework from the perspective of technical principles, and also provides some Java code examples.
The implementation principle of CircleImageView mainly involves the following aspects:
1. Inherit the ImageView class: CircleImageView is inherited from the ImageView class, so you can directly use all the functions of ImageView and customize on this basis.
2. Cutting pictures are circular: The core function of CircleImageView is to cut ordinary pictures into circles to show the effect of circular avatars.To implement this function, you need to rewrite the ONDRAW () method of CircleImageView, and draw a round outline in it.First of all, set the picture to shader through the BitMapShader class, and then draw the picture into a circular shape through Canvas's DrawCircle () method.
3. Custom attributes: In order to facilitate use, CircleImageView supports some custom attributes, such as border width, frame color, rounded radius, etc.By adding corresponding attribute variables to the CircleImageView class, and reading and setting these attribute values in the constructor, you can realize the function of custom attributes.
The following is a simple CircleImageView sample code:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatImageView;
public class CircleImageView extends AppCompatImageView {
private float borderWidth;
private int borderColor;
private Paint borderPaint;
private Paint bitmapPaint;
public CircleImageView(Context context) {
super(context);
init();
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
borderWidth = 5f;
borderColor = Color.WHITE;
borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidth);
borderPaint.setColor(borderColor);
bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
bitmapPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
if (bitmap != null) {
int width = getWidth();
int height = getHeight();
int radius = Math.min(width, height) / 2;
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
bitmapPaint.setShader(shader);
canvas.drawCircle(width / 2, height / 2, radius, bitmapPaint);
canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2, borderPaint);
}
}
}
Through the above code examples, you can see that the CircleImageView framework is to use BitmapShader and Canvas to draw the round cutting of the picture by rewriting the onDraw () method.At the same time, you can also adjust the style of the border by setting a custom attribute.
Summary: CircleImageView is a custom view control of a commonly used displayed round picture.By inheriting the IMAGEVIEW class and rewriting the onDraw () method, using BitmapShader and Canvas to draw a circular tailoring effect that can achieve pictures.By setting a custom attribute, you can flexibly adjust the frame style.I hope this article will be helpful to understand the technical principles of understanding the CircleImageView framework.