gpt4 book ai didi

Android CardView - 如何折叠角

转载 作者:行者123 更新时间:2023-11-30 00:24:01 25 4
gpt4 key购买 nike

我想获得一个右上角的 CardView,如下图所示,但我不知道该怎么做。它就像一张折叠纸(没有动画)。我不知道我是否应该制作自定义背景可绘制或如何管理角半径以获得所需的结果。任何帮助将不胜感激,谢谢

enter image description here

最佳答案

您也可以像这样以编程方式创建这样的可绘制对象:

public static final class FoldCornerCard extends Shape {

private final float foldPart;
private final Path cardPath = new Path();
private final Path foldPath = new Path();
private final Paint foldPaint;

public FoldCornerCard(int foldColor, float foldPart) {
if (foldPart <= 0 || foldPart >= 1) {
throw new IllegalArgumentException("Fold part must be in (0,1)");
}
this.foldPart = foldPart;
this.foldPaint = new Paint();
foldPaint.setAntiAlias(true);
foldPaint.setColor(foldColor);
}

@Override
protected void onResize(float width, float height) {
super.onResize(width, height);
this.cardPath.reset();
final float leftFold = width - width * foldPart;
final float bottomFold = height * foldPart;

cardPath.lineTo(leftFold, 0);
cardPath.lineTo(width, bottomFold);
cardPath.lineTo(width, height);
cardPath.lineTo(0, height);
cardPath.close();

foldPath.reset();
foldPath.moveTo(leftFold, 0);
foldPath.lineTo(leftFold, bottomFold);
foldPath.lineTo(width, bottomFold);
foldPath.close();
}

@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawPath(cardPath, paint);
canvas.drawPath(foldPath, foldPaint);
}
}

和用法示例:

final ShapeDrawable shapeDrawable = new ShapeDrawable(
new FoldCornerCard(Color.GREEN, 0.1f));
shapeDrawable.getPaint().setColor(Color.WHITE);
shapeDrawable.setIntrinsicHeight(-1);
shapeDrawable.setIntrinsicWidth(-1);

您只需稍微修改一下我的代码 fragment 即可添加圆角。

关于Android CardView - 如何折叠角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com