2017-08-17 5 views
1

Je veux obtenir un CardView avec le coin en haut à droite comme vous pouvez le voir dans l'image ci-dessous, mais je n'ai aucune idée de la façon de le faire. C'est comme un papier plié (sans animation). Je ne sais pas si je devrais créer un arrière-plan personnalisé ou comment gérer le rayon du coin pour obtenir le résultat souhaité. Toute aide sera grandement appréciée, grâceAndroid CardView - Comment plier coin

enter image description here

Répondre

0

Regardez ici https://developer.android.com/studio/write/draw9patch.html Je pense que c'est une façon Righ d'utiliser la mise en page personnalisée.
Vous pouvez le dessiner sur xml, ou utiliser 9-patch png.
Aussi, vous pouvez créer votre propre classe MyCardView et s'étend de CardView, puis remplacer la méthode onDraw et dessiner CardView comme vous voulez, mais ce n'est pas une bonne idée.
Je recommande que vous utilisez 9-patch image

0

Vous pouvez aussi créer un tel drawable programme comme celui-ci:

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

Et un exemple d'utilisation:

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

Vous avez juste besoin de modifier mon extrait un peu pour ajouter des coins arrondis.