Android 自定义view 圆形边框 图片裁剪 全代码
时间: 2023-11-03 12:26:05 浏览: 45
好的,下面是一个实现圆形边框和图片裁剪的自定义View的完整代码:
```
public class CircleImageView extends ImageView {
private Paint mBorderPaint;
private int mBorderColor;
private int mBorderWidth;
private Bitmap mBitmap;
private BitmapShader mBitmapShader;
private int mBitmapWidth;
private int mBitmapHeight;
private float mRadius;
private RectF mBorderRect;
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() {
mBorderPaint = new Paint();
mBorderPaint.setAntiAlias(true);
mBorderRect = new RectF();
}
@Override
protected void onDraw(Canvas canvas) {
if (mBitmapShader == null) {
mBitmap = getBitmap();
if (mBitmap != null) {
mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight = mBitmap.getHeight();
mRadius = Math.min(mBitmapWidth, mBitmapHeight) / 2;
updateShaderMatrix();
}
}
if (mBitmapShader != null) {
mBorderPaint.setShader(mBitmapShader);
canvas.drawCircle(mRadius, mRadius, mRadius, mBorderPaint);
mBorderPaint.setShader(null);
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStrokeWidth(mBorderWidth);
mBorderRect.set(0, 0, getWidth(), getHeight());
canvas.drawArc(mBorderRect, 0, 360, false, mBorderPaint);
}
}
private void updateShaderMatrix() {
float scale;
float dx = 0;
float dy = 0;
if (mBitmapWidth * getHeight() > getWidth() * mBitmapHeight) {
scale = getHeight() / (float) mBitmapHeight;
dx = (getWidth() - mBitmapWidth * scale) * 0.5f;
} else {
scale = getWidth() / (float) mBitmapWidth;
dy = (getHeight() - mBitmapHeight * scale) * 0.5f;
}
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
mBitmapShader.setLocalMatrix(matrix);
}
public void setBorderColor(int borderColor) {
if (borderColor == mBorderColor) {
return;
}
mBorderColor = borderColor;
invalidate();
}
public void setBorderWidth(int borderWidth) {
if (borderWidth == mBorderWidth) {
return;
}
mBorderWidth = borderWidth;
invalidate();
}
private Bitmap getBitmap() {
Drawable drawable = getDrawable();
if (drawable == null) {
return null;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap 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;
}
}
```
使用方法:
在布局文件中添加自定义View:
```
<com.example.CircleImageView
android:id="@+id/circle_image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/avatar"
app:border_color="#ffffff"
app:border_width="4dp" />
```
其中 `app:border_color` 和 `app:border_width` 分别表示边框的颜色和宽度,可以根据需要调整。