方法1:
a.自定義RoundImageView
package app.downloadall.helptool.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import androidx.appcompat.widget.AppCompatImageView;
public class RoundImageView extends AppCompatImageView {
float width, height;
private int defaultRadius = 20;
private int radius;
private int leftTopRadius;
private int rightTopRadius;
private int rightBottomRadius;
private int leftBottomRadius;
public RoundImageView(Context context) {
this(context, null);
init(context, null);
}
public RoundImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context, attrs);
}
public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
// 讀取配置
@SuppressLint("CustomViewStyleable")
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
radius = array.getDimensionPixelOffset(R.styleable.RoundImageView_radius, defaultRadius);
leftTopRadius = array.getDimensionPixelOffset(R.styleable.RoundImageView_left_top_radius, defaultRadius);
rightTopRadius = array.getDimensionPixelOffset(R.styleable.RoundImageView_right_top_radius, defaultRadius);
rightBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundImageView_right_bottom_radius, defaultRadius);
leftBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundImageView_left_bottom_radius, defaultRadius);
//如果四個角的值沒有設(shè)置,那么就使用通用的radius的值儒溉。
if (defaultRadius == leftTopRadius) {
leftTopRadius = radius;
}
if (defaultRadius == rightTopRadius) {
rightTopRadius = radius;
}
if (defaultRadius == rightBottomRadius) {
rightBottomRadius = radius;
}
if (defaultRadius == leftBottomRadius) {
leftBottomRadius = radius;
}
array.recycle();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
//這里做下判斷宦焦,只有圖片的寬高大于設(shè)置的圓角距離的時候才進(jìn)行裁剪
int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
int maxRight = Math.max(rightTopRadius, rightBottomRadius);
int minWidth = maxLeft + maxRight;
int maxTop = Math.max(leftTopRadius, rightTopRadius);
int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
int minHeight = maxTop + maxBottom;
if (width >= minWidth && height > minHeight) {
@SuppressLint("DrawAllocation")
Path path = new Path();
//四個角:右上,右下顿涣,左下波闹,左上
path.moveTo(leftTopRadius, 0);
path.lineTo(width - rightTopRadius, 0);
path.quadTo(width, 0, width, rightTopRadius);
path.lineTo(width, height - rightBottomRadius);
path.quadTo(width, height, width - rightBottomRadius, height);
path.lineTo(leftBottomRadius, height);
path.quadTo(0, height, 0, height - leftBottomRadius);
path.lineTo(0, leftTopRadius);
path.quadTo(0, 0, leftTopRadius, 0);
canvas.clipPath(path);
}
super.onDraw(canvas);
}
}
b.添加styles
<declare-styleable name="RoundImageView">
<attr name="radius" format="dimension" />
<attr name="left_top_radius" format="dimension" />
<attr name="right_top_radius" format="dimension" />
<attr name="right_bottom_radius" format="dimension" />
<attr name="left_bottom_radius" format="dimension" />
</declare-styleable>
方法2:(推薦)
使用ShapeableImageView,這個目前google封裝的涛碑,還是很好用的
a.添加依賴
implementation 'com.google.android.material:material:1.3.0'
b.xml直接引用
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/im_shapeable"
app:shapeAppearanceOverlay="@style/ShapeableCircleImageStyle"
android:layout_width="100dp"
android:layout_height="126dp"
android:src="@mipmap/img"/>
c.常用style添加(可根據(jù)情況自由添加切角和弧度)
<!-- 帶有弧度的圖片 -->
<style name="ShapeableCircleImageStyle">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">10%</item>
</style>
<!-- 圓形圖片(特殊的弧度圖片) -->
<style name="ShapeableCircleImageStyle">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
<!-- 右上角90度扇形圖片(特殊的弧度圖片) -->
<style name="ShapeableTopLeftRoundImageStyle">
<item name="cornerFamilyTopRight">rounded</item>
<item name="cornerSizeTopRight">100%</item>
</style>
<!-- 葉子圖片(特殊的弧度圖片) -->
<style name="ShapeableLeafImageStyle">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyBottomRight">rounded</item>
<item name="cornerSizeTopLeft">50%</item>
<item name="cornerSizeBottomRight">50%</item>
<!-- 八邊形(切角圖片) -->
<style name="ShapeableCutImageStyle">
<item name="cornerFamily">cut</item>
<item name="cornerSize">10dp</item>
</style>
<!-- 菱形圖片(切角圖片) -->
<style name="ShapeablePrismaticImageStyle">
<item name="cornerFamily">cut</item>
<item name="cornerSize">50%</item>
</style>
d.邊框添加(需要添加padding精堕,否則會有一部分看不到)
<com.google.android.material.imageview.ShapeableImageView
...
app:strokeWidth="2dp"
android:padding="1dp"
app:strokeColor="@color/colorPrimaryDark"
/>
方法3:(推薦)
//帶弧度
Glide.with(mContext)
.load(...)
.apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
.into(holder.wImageiew);
//圓
Glide.with(mContext)
.load(...)
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.into(holder.wImageiew);