Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Android group opacity prop #2417

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions android/src/main/java/com/horcrux/svg/GroupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.horcrux.svg;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
Expand All @@ -29,9 +30,13 @@
class GroupView extends RenderableView {
@Nullable ReadableMap mFont;
private GlyphContext mGlyphContext;
private Bitmap mLayerBitmap;
private Canvas mLayerCanvas;
private final Paint mLayerPaint;

public GroupView(ReactContext reactContext) {
super(reactContext);
mLayerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}

public void setFont(Dynamic dynamic) {
Expand Down Expand Up @@ -92,6 +97,16 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
final SvgView svg = getSvgView();
final GroupView self = this;
final RectF groupRect = new RectF();
if (mLayerBitmap == null) {
mLayerBitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mLayerCanvas = new Canvas(mLayerBitmap);
} else {
mLayerBitmap.recycle();
mLayerBitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mLayerCanvas.setBitmap(mLayerBitmap);
}
elements = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
Expand All @@ -107,15 +122,15 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
((RenderableView) node).mergeProperties(self);
}

int count = node.saveAndSetupCanvas(canvas, mCTM);
node.render(canvas, paint, opacity * mOpacity);
int count = node.saveAndSetupCanvas(mLayerCanvas, mCTM);
node.render(mLayerCanvas, paint, opacity);
RectF r = node.getClientRect();

if (r != null) {
groupRect.union(r);
}

node.restoreCanvas(canvas, count);
node.restoreCanvas(mLayerCanvas, count);

if (node instanceof RenderableView) {
((RenderableView) node).resetProperties();
Expand All @@ -137,6 +152,9 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
}
}
}

mLayerPaint.setAlpha((int) (mOpacity * 255));
canvas.drawBitmap(mLayerBitmap, 0, 0, mLayerPaint);
this.setClientRect(groupRect);
popGlyphContext();
}
Expand Down
1 change: 1 addition & 0 deletions apps/test-examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Test2366 from './src/Test2366';
import Test2397 from './src/Test2397';
import Test2403 from './src/Test2403';
import Test2407 from './src/Test2407';
import Test2417 from './src/Test2417';

export default function App() {
return <ColorTest />;
Expand Down
20 changes: 20 additions & 0 deletions apps/test-examples/src/Test2417.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {View} from 'react-native';
import {G, Rect, Svg} from 'react-native-svg';

export default function App() {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Svg width="150" height="150" viewBox="0 0 150 150">
<G opacity="0.5">
<Rect width="100" height="100" fill="red" />
<Rect x="50" y="50" width="100" height="100" fill="green" />
</G>
</Svg>

<Svg width="150" height="150" viewBox="0 0 150 150" opacity="0.5">
<Rect width="100" height="100" fill="red" />
<Rect x="50" y="50" width="100" height="100" fill="green" />
</Svg>
</View>
);
}
Loading