Loading .svg file from web or local cache in Android - android

I'm using Glide to load .png image asynchronously.
File file = Glide.with(context).load(location).downloadOnly(1024, 1024).get();
In case of .svg, the file is created but it is not loading into ImageView.

ImageView only accepts Bitmaps or VectorDrawables.
SVG is neither of the two, even if VectorDrawable descends from it.
Get a PictureDrawable from your SVG file. Then you need to create a Bitmap from the size of the PictureDrawable and give it to a Canvas.
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;

Related

High resolution bitmap quality issue

I get bitmap from drawable but bitmap sized is maximum 1024*1024..i will try to get
bitmap full resolution from drawable but ican't get.I will try to all possibile work but not get how to solve this problem.I try to last 4 days but not get any proper solution.I want to need get full resolution bitmap from drawable
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
Bitmap bmp = drawable.getBitmap();
Bitmap b = Bitmap.createScaledBitmap(bmp, 1025, 1025, false);
First, you need to put your image file in drawable-nodpi. nodpi will not resize your drawable.
Second, you can use BitmapFactory.decodeResource(getResources(), R.drawable.bg_pop); to retrieve Bitmap from resource.
Edit-
You can use 'inScaled' options when retrieving Bitmap from the resource. I checked from my project. (original image size is 1920x1080
val options= BitmapFactory.Options()
options.inScaled = false
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.splatoon, options)
ScreenShot, in imgur

Convert view into high quality image

i converted a view into image using this
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
this bitmap image is not good quality and distorted, i want high quality image.
please tell me how to make high quality image of a view in Android.
i want image like this
but this which i am getting is distorted

Create Bitmap from drawable

I would like to add an image (Bitmap) over another one. For that I am only using Bitmaps (fot the image I draw in and the image I add), but the Bitmap I am using is actually a resource in my drawable resource. So Is there a way to create a Bitmap that contains my drawable ?
Use decodeResource() of BitmapFactory to convert drawable resource into Bitmap as follows...
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.your_drawable);
Try this:
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_name);

Set Picture into RemoteViews

I have some SVGs in my assets folder and I need to dynamically set them in my widget (on an ImageView).
I am using this library: http://code.google.com/p/svg-android/
This library returns a Picture or a PictureDrawable.
The only methods I can see to use on RemoteViews are setImageViewBitmap which obviously takes a bitmap.
I tried looking for code to convert a Drawable to a Bitmap like this:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
But the bitmap is too small. When I create the bitmap in Illustrator I set the artboard size to 65 which is what comes through on the intrinsic width/height.
My widgets can be resized so the ImageView sizes are variable. Even if I set the width and height statically to some large number like this...
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
then the resulting bitmap just has a bunch of whitespace below and to the right of a tiny image.
I guess I need to somehow draw the picture at a scaled up value as well as creating the Bitmap at size 300. Ideally I could figure out the size of the ImageView at runtime and set the proper sized Bitmap if I knew that. Is this the best approach and how would I do this? Perhaps there is a better approach I don't even know about?
I've not used android-svg but if it's using vanilla PictureDrawables, then it should be just a matter of not using the intrinsic bounds.
Try the following:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
pictureDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
pictureDrawable.draw(canvas); // do not access the Picture directly, that defeats the purpose
currentBitmap = bitmap;
In short, use the Drawable, not its Picture and set the Drawable's bounds to be the full canvas.
Have you tried createScaledBitmap?
createScaledBitmap()
I have tried svg-android and it has not worked for me for this very reason. Not to mention its severely limited feature set.
The point of using vector graphics is that I can generate images of any appropriate size to fit the UI View size. Which means the generation method must accept the size requirements at run-time, and not always use width,height declared in <svg> tag.
Hence I used the native implementation: libsvg-android, which exactly does that.
It directly renders to a canvas with given size:
long objId = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(objId, readString(mInputStream, "UTF-8"));
SvgRaster.svgAndroidSetAntialiasing(objId, true);
SvgRaster.svgAndroidRenderToArea(objId, mCanvas, 0, 0, mWidth, mHeight);
I ended up modifying the underlying Artboard size to be 300. None of the scaling up methods worked. So the final code I used was that which I originally posted:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
Once I had a 300x300 Bitmap I was able to set it into the RemoteViews using setImageViewBitmap .
This kind of defeated the purpose of using SVGs in the first place (at least as far as using them in widgets was concerned).
My problem was not the same as User117. Perhaps he did not name the outer layer 'bounds' as was required in the library. Either way, I managed to get the library working, albeit by having to modify the SVG Artboard size.
Hopefully with the increase in screen resolution Android will introduce SVGs as part of the platform soon.

how to convert a paint image to bitmap

I am able to save the source image but not able save the image with colorfilter:
paint.setColorFilter(new ColorMatrixColorFilter(cm));
If this image is converted into bitmap it can be saved easily,
but I don't know how to do this. Is there anyone to give solution?
Have your original bitmap.
Create a new clean Bitmap that has the same width/height as your original bitmap.
Create a new Canvas using this clean Bitmap.
Set your paint object, etc for this new Canvas.
Draw your original bitmap into this new Canvas.
Since this new Canvas is backed by a Bitmap (point 3.), any drawing you do in this Canvas will become part of the new Bitmap (point 2.). Now just call 'compress' on this Bitmap from point 2. and the bitmap will be saved as a jpg/png.

Categories

Resources