Displaying SVG file on android - zero sized drawable? - android

I am using the svg-android library to load and display SVG images:
ImageView iv = (ImageView)findViewById(R.id.imgLoginLogo);
// Set the background color to white
iv.setBackgroundColor(Color.WHITE);
// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
// Get a drawable from the parsed SVG and set it as the drawable for the ImageView
Drawable d = svg.createPictureDrawable();
iv.setImageDrawable(d);
Problem is the drawable I get has zero dimensions, so it is not visible.
What is wrong here ?
Thanks
Shimon

I use this to create a drawable:
public static Bitmap createBitmap(InputStream in, int w, int h) {
com.larvalabs.svgandroid.SVG svg = new SVGBuilder().readFromInputStream(in).build();
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Picture pic = svg.getPicture();
canvas.drawPicture(pic, new Rect(0, 0, w, h));
return bitmap;
}
Notice that you must care about recycling the bitmap after done with it.

Related

How can i convert a statelistDrawable to bitmapdrawable in android

How can i convert a state list Drawable to bitmap drawable in android.
I want to convert a statelist drawable into bitmap drawable.
I found a work around if i find any icon whose type is other then bitmap drawable then use this code
here searchResult.getIcon() is my icon value
Bitmap bitmap = Bitmap.createBitmap(searchResult.getIcon().getIntrinsicWidth(),searchResult.getIcon().getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
searchResult.getIcon().setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
searchResult.getIcon().draw(canvas);
itemIconBitmap = bitmap;
e.printStackTrace();
You can use toBitmap or casting the current drawable
(stateListDrawable as? StateListDrawable)?.let { drawable ->
(drawable.current as BitmapDrawable).bitmap
}

Android Image Masking with Rotate & Scale

How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);
Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);
original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
//mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setBackgroundResource(R.drawable.background_drawable);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again

Android water mark

I have some question about water mark within android code!
Following code showed my idea about WaterMark!
However,It does not work normally.
e.g. only the image end with .png can be watered mark
Is there a scheme about water mark(.jpeg, .jpg, .wbmp, .bmp, .png or others)
protected static Bitmap getDrmPicture(Context context,String path){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap originMap = BitmapFactory.decodeFile (path,options);
Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.close);
InputStream input;
byte[] b;
Bitmap waterMark = null;
try {
input = context.getResources().openRawResource(R.drawable.lock);
b = new byte[input.available()];
input.read(b);
waterMark = DecodeUtils.requestDecode(jc, b, null);
}catch(IOException e){
}
int w = originMap.getWidth();
int h = originMap.getHeight();
int ww = waterMark.getWidth();
int wh = waterMark.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888;);
Canvas cv = new Canvas(newb);
cv.drawBitmap(originMap, 0, 0, null);
cv.drawBitmap(waterMark, w - ww, h - wh, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newb;
}
Thanks !
This is the code I use to apply watermark to a jpeg, it should work for you too,
public Bitmap applyWatermarkColorFilter(Drawable drawable) {
Bitmap image = ((BitmapDrawable)drawable).getBitmap();
Bitmap result = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(image, 0, 0, null);
Bitmap watermark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark);
canvas.drawBitmap(watermark, image.getWidth()/2 - watermark.getWidth()/2,
image.getHeight()/2 - watermark.getHeight()/2,
null);
return result;
}
Basically after this u have to use Bitmap.compress(<arguments>) to get a jpg out of it.
Din't try for the other formats. May be it might be possible if you can extract the Bitmap out of them like how we do for jpg and png.
https://stackoverflow.com/questions/6756975/draw-multi-line-text-to-canvas
Measure height of multiline text
To center text vertically we need to know text height. Instantiate StaticLayout with text width according to your needs, for us it is simple the width of Bitmap/Canvas minus 16dp padding. The getHeight() then returns height of text.
Positioning text on Canvas
There are four simple steps to position text on Canvas:
Save the current matrix and clip with Canvas.save().
Apply translation to Canvas matrix with Canvas.translate(x,y).
Draw StaticLayout on Canvas with StaticLayout.draw(canvas).
Revert matrix translation with Canvas.restore() method.

How to convert PictureDrawable into ScaleDrawble?

I am trying to scale a picture drwable which I get from svg-android-library. I can create the drawable but do not get how to scale it.
Here is the code I tried:
Drawable drawable = svg.createPictureDrawable();
int size = 108;
Bitmap img = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(img);
//resize drawable here according to screen width
drawable = new ScaleDrawable(drawable, 0, size*2, size*2).getDrawable();
drawable.setBounds(0, 0, size*2, size*2);
drawable.draw(canvas);
Any suggestions?
I used
Picture picture = svg.getPicture();
and then draw it with a new Rect which will stretch it to fit the new Rect
canvas.drawPicture(picture, newRect);

How to clip and fill a canvas using an alpha mask

I have some .png icons that are alpha masks. I need to render them as an drawable image using the Android SDK.
On the iPhone, I use the following to get this result, converting the "image" alpha mask to the 'imageMasked' image using black as a fill:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, thumbWidth,
thumbHeight, 8, 4*thumbWidth, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect frame = CGRectMake(0,0,thumbWidth,thumbHeight);
CGContextClipToMask(context, frame, [image CGImage]);
CGContextFillRect(context, frame);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
How do I accomplish the above in Android SDK?
I've started to write the following:
Drawable image = myPngImage;
final int width = image.getMinimumWidth();
final int height = image.getMinimumHeight();
Bitmap imageMasked = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(iconMasked);
image.draw(canvas); ???
I'm not finding how to do the clipping on imageMasked using image.
Solved:
Drawable icon = An_Icon_That_Is_An_Alpha_Mask;
int width = icon.getIntrinsicWidth();
int height = icon.getIntrinsicHeight();
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(bm);
icon.setBounds(new Rect(0,0,width,height));
icon.draw(canvas);

Categories

Resources