带弧度的图片,三种实现方法(RoundImageView,Sha
2021-04-08 本文已影响0人
wenju
方法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);
//如果四个角的值没有设置,那么就使用通用的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) {
//这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪
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添加(可根据情况自由添加切角和弧度)
<!-- 带有弧度的图片 -->
<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);