6 Ecke zeichnen - 2. Frage

  • Antworten:5
  • Bentwortet
TTT Tobi Triple Talk
  • Forum-Beiträge: 7

07.04.2021, 12:21:05 via Website

Ich habe jetzt durch Hilfe von Jokel meine erste Frage gelöst. Bloß ist mir beim herum experimentieren mit dem gewonnenen Kenntnissen draufgekommen, das ich gerne mein 6-Eck mit einer schwarzen Außenlinie zeichnen will. Ich habe es probiert und auch eine Möglichkeit gefunden. Bloß wenn ich das mache dann färbt sich der ganze Canvas in diese Farbe. Ich möchte aber das es Transparent die anderen darunterliegenden Objekte anzeigt.
Falls ihr die Bilder sehen wollt, einfach https:// hinzufügen. Kann leider noch keine Links hochladen
So schaut es aus wenn ich meine maskColor anwende.

So schau es aus wenn ich es mit Color.TRANSPARENT fülle

Aber wie ich schon gesagt habe ich möchte das die leiste bleibt, so wie beim 1 Bild und der Hintergrund mein originales canvas ist.
Das ist der code zum Zeichnen für das oberste Bild.

public void draw(Canvas c){
    c.clipPath(hexagonBorderPath, Region.Op.DIFFERENCE);
    c.drawColor(Color.BLACK);
    c.save();
    c.clipPath(hexagonPath, Region.Op.DIFFERENCE);
    c.drawColor(maskColor);
    c.save();
}

Das ist der code zum Zeichnen für das unterste Bild.

public void draw(Canvas c){
    c.clipPath(hexagonBorderPath, Region.Op.DIFFERENCE);
    c.drawColor(Color.BLACK);
    c.save();
    c.clipPath(hexagonPath, Region.Op.DIFFERENCE);
    c.drawColor(Color.TRANSPARENT);
    c.save();
}

Ich habe nichts geändert bis auf die Farbe. Hat jemand dafür eine andere Lösung als das einfach als erstes aufs canvas zu malen?

— geändert am 07.04.2021, 18:57:26 durch Moderator

Kommentieren
Beste Antwort
Jokel
  • Forum-Beiträge: 1.530

07.04.2021, 19:27:12 via Website

Hallo
benutze doch verschiede Sub-Alpa-Bitmaps (ARG 8888)
Mit PorterDuff kannst du sie übereinanderlegen ähnlich wie transparente Folien.
Am ende alle Sub Bitmaps wider frei geben.

https://developer.android.com/reference/android/graphics/PorterDuff.Mode

wieso überhaupt zwei mal zeichnen?
c.clipPath(hexagonBorderPath, Region.Op.DIFFERENCE);
ist das 6 Eck

c.clipPath(hexagonPath, Region.Op.DIFFERENCE);
ist die Maske um das 6 Eck

Auch wichtig ist welche Eigenschaft du für clipPath du benutzt.
DIFFERENCE , INTERSECT, REPLACE, REVERSE_DIFFERENCE, UNION, XOR

Ps. sieht so aus als ob du den Code aus dem Link noch nicht verstanden hast.
Der zeigte dir wie du zu dem Path kommst das Zeichen ist natürlich nicht immer gleich. Und kann nicht immer 1 zu 1 übernommen werden.

— geändert am 07.04.2021, 20:00:20

Hilfreich?
TTT Tobi Triple Talk
Kommentieren
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

07.04.2021, 19:01:29 via Website

Wie sieht dein hexagonPath aus? Hast du da auch wirklich nur die Linie drin?
Vlt musst du alles außen rum nochmal extra behandeln, d.h. ein hexagon, das um die Liniendicke größer ist mit der passenden Farbe darum zeichen

LG Pascal //It's not a bug, it's a feature. :) ;)

Hilfreich?
Kommentieren
Beste Antwort
Jokel
  • Forum-Beiträge: 1.530

07.04.2021, 19:27:12 via Website

Hallo
benutze doch verschiede Sub-Alpa-Bitmaps (ARG 8888)
Mit PorterDuff kannst du sie übereinanderlegen ähnlich wie transparente Folien.
Am ende alle Sub Bitmaps wider frei geben.

https://developer.android.com/reference/android/graphics/PorterDuff.Mode

wieso überhaupt zwei mal zeichnen?
c.clipPath(hexagonBorderPath, Region.Op.DIFFERENCE);
ist das 6 Eck

c.clipPath(hexagonPath, Region.Op.DIFFERENCE);
ist die Maske um das 6 Eck

Auch wichtig ist welche Eigenschaft du für clipPath du benutzt.
DIFFERENCE , INTERSECT, REPLACE, REVERSE_DIFFERENCE, UNION, XOR

Ps. sieht so aus als ob du den Code aus dem Link noch nicht verstanden hast.
Der zeigte dir wie du zu dem Path kommst das Zeichen ist natürlich nicht immer gleich. Und kann nicht immer 1 zu 1 übernommen werden.

— geändert am 07.04.2021, 20:00:20

Hilfreich?
TTT Tobi Triple Talk
Kommentieren
TTT Tobi Triple Talk
  • Forum-Beiträge: 7

07.04.2021, 20:00:48 via Website

Danke dir nochmals. Hab es jetzt etwas anders gelöst, da ich nicht ganz verstanden habe wie man die Sub-Alpa-Bitmaps verwendet. Hab es jetzt so gelöst. Ich danke dir für deine hilfe.

   Paint hexBorder = new Paint();

    hexBorder.setColor(Color.WHITE);

    Paint hexColor = new Paint();
    hexColor.setColor(Color.BLACK);

    c.drawPath(hexagonPath, hexColor);
    c.drawPath(hexagonBorderPath, hexBorder);
Hilfreich?
Kommentieren
TTT Tobi Triple Talk
  • Forum-Beiträge: 7

07.04.2021, 20:02:01 via Website

Hab das Beispiel von dem vorherigen post genutzt. Sieht so aus

private Path hexagonPath;
private Path hexagonBorderPath;
private float radius;
private float width, height;
private float x, y;
private int maskColor;


public Hexagon(int width, int height, int x, int y) {
    init();
    setScale(width, height, x, y);
}

private void init() {
    hexagonPath = new Path();
    hexagonBorderPath = new Path();
    maskColor = 0xFF01FF77;
}
public void setRadius(float r) {
    this.radius = r;
    calculatePath();
}

public void setMaskColor(int color) {
    this.maskColor = color;
}

private void calculatePath() {
    float triangleHeight = (float) (Math.sqrt(3) * radius / 2);
    float centerX = x + radius;
    float centerY = y + radius;
    hexagonPath.moveTo(centerX, centerY + radius);
    hexagonPath.lineTo(centerX - triangleHeight, centerY + radius/2);
    hexagonPath.lineTo(centerX - triangleHeight, centerY - radius/2);
    hexagonPath.lineTo(centerX, centerY - radius);
    hexagonPath.lineTo(centerX + triangleHeight, centerY - radius/2);
    hexagonPath.lineTo(centerX + triangleHeight, centerY + radius/2);
    hexagonPath.moveTo(centerX, centerY + radius);

    float radiusBorder = radius - 5;
    float triangleBorderHeight = (float) (Math.sqrt(3) * radiusBorder / 2);
    hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);
    hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY + radiusBorder/2);
    hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY - radiusBorder/2);
    hexagonBorderPath.lineTo(centerX, centerY - radiusBorder);
    hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY - radiusBorder/2);
    hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY + radiusBorder/2);
    hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);
}

public void draw(Canvas c){
    Paint hexBorder = new Paint();
    hexBorder.setColor(Color.WHITE);

    Paint hexColor = new Paint();
    hexColor.setColor(Color.BLACK);

    c.drawPath(hexagonPath, hexColor);
    c.drawPath(hexagonBorderPath, hexBorder);
}

public void setScale(int width, int height, int x, int y) {
    this.width = width;
    this.height =  height;
    this.x = x;
    this.y = y;
    radius = (float) (height / 2);

    calculatePath();
}
Hilfreich?
Kommentieren
Jokel
  • Forum-Beiträge: 1.530

07.04.2021, 21:01:09 via Website

Richtig du kannst deinen Path auch als
Polygon Zeichen.
Nur warum zeichnet du hier auch beide 6 Ecke?

Das zweite ist ja nur 5 Pixel kleiner weil dein radius kleiner ist.
Soll der Strich dicker sein machte deinen Stift Paint dicker.

Du kannst doch auch die Füll Methode mit Paint angeben dann hast du nur das 6 Eck. Ohne das etwas überschrieben wird.

http://bildungsgueter.de/Android/Pages/Grafik001Seite008.htm

— geändert am 07.04.2021, 22:33:04

Hilfreich?
Kommentieren