2017-08-17 92 views
1

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

enter image description here

回答

0

看这里https://developer.android.com/studio/write/draw9patch.html 我认为这是使用自定义布局右击方式。
你可以在xml上绘制它,或者使用9-patch png。
您也可以创建自己的类MyCardView,并从CardView扩展,然后覆盖方法onDraw并根据需要绘制CardView,但这不是个好主意。
我会建议你使用9-patch image

0

还可以通过编程方式创建这样的绘制这样的:

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); 

你只需要修改我的片段位添加圆角。