androidsvg icon indicating copy to clipboard operation
androidsvg copied to clipboard

how can i use colorfilter in svg

Open Jey1992 opened this issue 6 years ago • 11 comments

finally i didnt found this project support colorfilter in svg file, help me , i really need this function

Jey1992 avatar Aug 28 '19 06:08 Jey1992

If you are using the SVGImageView class, then no. It is based on a normal ImageView and creates a PictureDrawable from the SVG. Unfortunately, ImageView does not support colour filters on PictureDrawables.

There are a couple of ways around that however:

  1. Instead of using my SVGImageView, render your SVG to a Bitmap. Then use that Bitmap with a normal ImageView. You will then be able to set a colour filter on the ImageView.
  2. AndroidSVG allows you to specify some custom CSS rules when rendering the SVG. You can use some CSS rules to override the colours in your SVG.

Hope this helps. If you tell me more about what you are trying to do, I will perhaps be able to offer more detailed advice.

BigBadaboom avatar Aug 28 '19 07:08 BigBadaboom

really thanks for your quick reply! I didnt use imageview,but used for picturedrawable to draw on canvas, ths for you suggestion, if I trans picturedrawable to bitmap,the picture will not support vectorgraph,that is not what I expected.

Jey1992 avatar Aug 28 '19 07:08 Jey1992

Then it sounds like you probably have to go with the CSS option. AndroidSVG doesn't currently have a way to set a ColorFilter when rendering directly to a Canvas.

BigBadaboom avatar Aug 28 '19 08:08 BigBadaboom

yes, but I found there is a solution to set colorfilter in SVG Use this fork of svg-android: https://github.com/japgolly/svg-android It supports ColorFilters, but I used this can not show SVG image entirely,can you give me some suggestion best wishes

Jey1992 avatar Aug 28 '19 08:08 Jey1992

That fork probably set colorfilter before rendering to Canvas,large code,lazy to read

Jey1992 avatar Aug 28 '19 08:08 Jey1992

hi... I used SVG to a Bitmap option, but the bitmap is distorted, how can I do

Jey1992 avatar Aug 28 '19 08:08 Jey1992

What do you mean by "distorted"? Please post a screenshot, and/or some example code.

BigBadaboom avatar Aug 28 '19 09:08 BigBadaboom

File file = new File(data.getFileLocalPath()+"/"+viewsBean.getLogo_name()); InputStream inputStream = new FileInputStream(file); SVG svg = SVG.getFromInputStream(inputStream); Picture picture = svg.renderToPicture(); Drawable drawable = new PictureDrawable(picture); bitmap = ImageUtils.drawable2Bitmap(drawable); finally the bitmap is Severe fuzzy. Im think of if I set the filter before rendering, does it works?

Jey1992 avatar Aug 28 '19 10:08 Jey1992

How big is the Bitmap you are drawing the Picture to? Is drawable2Bitmap() your own code?

If you are creating a Bitmap, you don't need to create a Picture first. Just create a Canvas from the Bitmap and do a renderToCanvas().

// Create a canvas to draw onto
Bitmap  newBM = Bitmap.createBitmap(desiredWidth, desiredHeight, Bitmap.Config.ARGB_8888);
Canvas  bmcanvas = new Canvas(newBM);

// Render our document onto our canvas
svg.renderToCanvas(bmcanvas);

Then you should be able to draw the bitmap to your real canvas, something like the following (note I have not tested this to check that it works).

Paint  bmPaint = new Paint();
bmPaint.setColorFilter(...);
myRealCanvas.drawBitmap(newBM, x, y, bmPaint);

BigBadaboom avatar Aug 28 '19 11:08 BigBadaboom

yes I did this too,but also didnt optimized today I found that your code in SVGAndroidRenderer to draw the picture by use canvas so I think if we can setColorfilter in its Paint. ep:strokePaint.setcolorfilter and fillPaint.setcolorfilter can you give me the source code so that I can do more things for optimized code do me a favor please

Jey1992 avatar Aug 29 '19 02:08 Jey1992

Thanks for your attention I download your code and edit the fillPaint to set colorfilter finally I found this way is complete feasible! Im going to use your code in my app the project is expected to go online on next month

Jey1992 avatar Aug 29 '19 04:08 Jey1992