2016-07-20 15 views
1

Ich zeichne einen hexagonalen Pfad in Android App. Jetzt möchte ich ein Bild in den gezeichneten Pfad legen. Unten ist der Code, den ich verwende, um den Pfad zu zeichnen.Hinzufügen eines Zeichens innerhalb einer angegebenen "Path" Variable in Android?

combPath = getHexPath(cellWidth/2f, cellWidth/2f, (float) (cellWidth * Math.sqrt(3)/4)); 
fillPaint.setColor(cellSet[c][r] ? Color.RED : Color.WHITE); 
canvas.drawPath(combPath, fillPaint); 

Method getHexPath()

private Path getHexPath(float size, float centerX, float centerY) { 
    Path path = new Path(); 

    for (int j = 0; j <= 6; j++) { 
     double angle = j * Math.PI/3; 
     float x = (float) (centerX + size * Math.cos(angle)); 
     float y = (float) (centerY + size * Math.sin(angle)); 
     if (j == 0) { 
      path.moveTo(x, y); 
     } else { 
      path.lineTo(x, y); 
     } 
    } 


    return path; 
} 

Jetzt habe ich ein Bild in der hexagonalen Pfad zu platzieren "path()" Variable. Wie kann ich es erreichen? TIA

Antwort

0

Versuchen, etwas zu verwenden:

Bitmap bitmap = getBitmap(); // retrieve bitmap that you want to draw somehow 
Paint paint = new Paint(); 
paint.setAntiAlias(true); 

combPath = getHexPath(cellWidth/2f, cellWidth/2f, (float) (cellWidth * Math.sqrt(3)/4)); 

canvas.drawPath(combPath, paint); 

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
canvas.drawBitmap(bitmap, 0, 0, paint); 

AKTUALISIERT

Hier ist ein Beispiel davon ab, wie Sie tun können, dass

private Bitmap getCroppedBitmap(Bitmap original) { 
    Bitmap output = Bitmap.createBitmap(original.getWidth(), 
      original.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, original.getWidth(), original.getHeight()); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 

    Path path = new Path(); 
    path.moveTo(30, 20); 
    path.lineTo(120, 30); 
    path.lineTo(110, 120); 
    path.lineTo(20, 110); 
    path.lineTo(30, 20); 

    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
    canvas.drawPath(path, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(original, rect, rect, paint); 
    return output; 
} 

Sie müssen nur platzieren deine Bitmap, spiele mit Breite/Höhe und füge deinen Pfad ein. Dieser Code hat für mich funktioniert.

+0

Dies zeichnet die Bitmap in der gesamten Aktivität nicht in der spezifischen hexagonalen Ansicht. – Sarfaraz

+0

Ich habe meine Antwort aktualisiert. Es wird nicht intern geändert, aber Sie müssen mit einer Zielbitmap arbeiten. Ein ähnlicher Code in der 'OnDraw'-Methode schlägt jedoch fehl. –

Verwandte Themen