Analysis of the Technical Principles of CirCleimage Applied in Java Class Libraries
CircleImageView is a commonly used Java class library that is used to display circular pictures in Android applications.It is based on the ImageView class of the Android platform and achieves the display effect of circular pictures through technical principles.This article will analyze the technical principles of the CircleImageView framework in detail and provide some Java code examples.
CircleImageView technical principles can be divided into the following aspects:
1. Custom View class: CircleImageView inherits from ImageView, and rewrite the custom drawing logic by rewriting the ONDRAW method.This can control the display method of the picture to achieve the round effect.
2. Image processing: CircleImageView uses the BitMapShader class to achieve the color of the picture.First, by obtaining the DRAWABLE object of ImageView, it converts it into Bitmap objects.Then, create a color device object through the BitMapShader class, and set it to Paint.In this way, when drawing a picture, you can use the color of the brush to fill the area of ImageView to achieve a circular effect.
The following is an example code of a simple CircleImageView class:
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.Drawable;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatImageView;
public class CircleImageView extends AppCompatImageView {
private Paint mPaint;
private Bitmap mBitmap;
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() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
float radius = Math.min(getWidth() / 2f, getHeight() / 2f);
canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, mPaint);
} else {
super.onDraw(canvas);
}
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
mBitmap = drawableToBitmap(drawable);
invalidate();
}
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) {
return null;
}
Bitmap bitmap;
if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
} 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;
}
}
The above is an example code of a simple CircleImageView class.It rewritten the ONDRAW method, using BitmapShader to achieve a circular effect when drawing a picture.In addition, it also rewritten the SetImageDrawable method to convert the Drawable object to the Bitmap object when setting the picture, and initialize before drawing.
Through the above technical principles analysis and sample code, we can see how CircleImageView achieves the display effect of circular pictures.By customizing View and using BitmapShader technology, CircleImageView can be easily applied to Android applications to achieve circular pictures display.