Je veux dessiner sur une toile un rectangle rond remplir avec la couleur (ou bitmap) et d'un coup, mais sans certains coins sélectionnés et aussi sans frontière sur certains côtés sélectionnés. des idées comment je peux y parvenir?Comment dessiner un rect rectangle partiel sur une toile android?
1
A
Répondre
1
Vous pouvez diviser le processus de dessin en deux parties.
dessiner la zone de remplissage
en essayant de dessiner une forme qui ne supporte par les API standard sdk, méthode Canvas.drawPath sera une bonne façon de le faire it.you peut simplement définir quatre variables pour représenter les rayons de quatre coins.
Path path = new Path(); Rect drawingRect = {the rect area you want to draw} RectF topLeftArcBound = new RectF(); RectF topRightArcBound = new RectF(); RectF bottomLeftArcBound = new RectF(); RectF bottomRightArcBound = new RectF(); topRightArcBound.set(drawingRect.right - topRightRadius * 2, drawingRect.top, drawingRect.right, drawingRect.top + topRightRadius * 2); bottomRightArcBound.set(drawingRect.right - bottomRightRadius * 2, drawingRect.bottom - bottomRightRadius * 2, drawingRect.right, drawingRect.bottom); bottomLeftArcBound.set(drawingRect.left, drawingRect.bottom - bottomLeftRadius * 2, drawingRect.left + bottomLeftRadius * 2, drawingRect.bottom); topLeftArcBound.set(drawingRect.left, drawingRect.top, drawingRect.left + bottomLeftRadius * 2, drawingRect.top + bottomLeftRadius * 2); path.reset(); path.moveTo(drawingRect.left + topLeftRadius, drawingRect.top); //draw top horizontal line path.lineTo(drawingRect.right - topRightRadius, drawingRect.top); //draw top-right corner path.arcTo(topRightArcBound, -90, 90); //draw right vertical line path.lineTo(drawingRect.right, drawingRect.bottom - bottomRightRadius); //draw bottom-right corner path.arcTo(bottomRightArcBound, 0, 90); //draw bottom horizontal line path.lineTo(drawingRect.left - bottomLeftRadius, drawingRect.bottom); //draw bottom-left corner path.arcTo(bottomLeftArcBound, 90, 90); //draw left vertical line path.lineTo(drawingRect.left, drawingRect.top + topLeftRadius); //draw top-left corner path.arcTo(topLeftArcBound, 180, 90); path.close(); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint);
et vous pouvez simplement définir le rayon à zéro pour les coins sélectionnés
tirage frontière
frontière contient huit parties:
- coin supérieur gauche
- top ligne
- haut à droite coin
- ligne droite
- coin inférieur droit
- ligne de fond
- coin inférieur gauche
- de ligne gauche
utiliser une valeur int pour contient des parties sélectionnées seront une bonne idée
private static final int TOP_LEFT_CORNER = 0x1; private static final int TOP_LINE = 0x2; private static final int TOP_RIGHT_CORNER = 0x4; private static final int RIGHT_LINE = 0x8; private static final int BOTTOM_RIGHT_CORNER = 0x10; private static final int BOTTOM_LINE = 0x20; private static final int BOTTOM_LEFT_CORNER = 0x40; private static final int LEFT_LINE = 0x80; private int selectedParts = TOP_LEFT_CORNER | TOP_LINE | TOP_RIGHT_CORNER;
et maintenant nous pouvons dessiner bordure basée sur selectedParts:
paint.setStyle(Paint.Style.STROKE); if((selectedParts & TOP_LINE) > 0){ canvas.drawLine(drawingRect.left + topLeftRadius, drawingRect.top, drawingRect.right - topRightRadius, drawingRect.top); } if((selectedParts & TOP_RIGHT_CORNER) > 0){ canvas.drawArc(topRightArcBound, -90, 90, false, paint); } if((selectedParts & RIGHT_LINE) > 0){ canvas.drawLine(drawingRect.right, drawingRect.top + topRightRadius, drawingRect.right, drawingRect.bottom - bottomRightRadius, paint); } if((selectedParts & BOTTOM_RIGHT_CORNER) > 0){ canvas.drawArc(bottomRightArcBound, 0, 90, false, paint); } if((selectedParts & BOTTOM_LINE) > 0){ canvas.drawLine(drawingRect.right - bottomRightRadius, drawingRect.bottom. drawingRect.left + bottomLeftRadius, drawingRect.bottom, paint); } if((selectedParts & BOTTOM_LEFT_CORNER) > 0){ canvas.drawArc(bottomLeftArcBound, 90, 90, false, paint); } if((selectedParts & LEFT_LINE) > 0){ canvas.drawLine(drawingRect.left, drawingRect.bottom - bottomLeftRadius, drawingRect.left, drawingRect.top + topLeftRadius, paint); } if((selectedParts & TOP_LEFT_CORNER) > 0){ canvas.drawArc(topLeftArcBound, 180, 90, false, paint); }