how can i use colorfilter in svg
finally i didnt found this project support colorfilter in svg file, help me , i really need this function
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:
- Instead of using my
SVGImageView, render your SVG to aBitmap. Then use that Bitmap with a normalImageView. You will then be able to set a colour filter on theImageView. - 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.
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.
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.
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
That fork probably set colorfilter before rendering to Canvas,large code,lazy to read
hi... I used SVG to a Bitmap option, but the bitmap is distorted, how can I do
What do you mean by "distorted"? Please post a screenshot, and/or some example code.
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?
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);
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
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