2017-08-17 77 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); 

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