Im porting an app from Flex to Android and wondering about how to build a composite image and display it.
Specifically I have a map (PNG or JPG) of a house and Im placing different markers in various locations. I've implemented this in HTML using DIV and in Flex using a canvas. Each marker has an X,Y pair based on the original size of the image. Ideally I'd like to display the image, place the markers and then support resize, drag (of the image, not the markers), etc.
There is info about 'multi-touch' available here though its a bit dated.
Suggestions on where to start?
You have to watch out for non-mutable bitmaps. When you load your bitmap, you have to create a copy which will be mutable. Then just apply your overlay using a Canvas.
Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.id.background, options);
Bitmap overlay = BitmapFactory.decodeResource(getResources(), R.id.overlay, options);
Bitmap finalBitmap = Bitmap.createBitmap(tempBitmap.getWidth(), tempBitmap.getHeight(), tempBitmap.getConfig());
Canvas canvas = new Canvas(finalBitmap);
canvas.drawBitmap(tempBitmap, new Matrix(), null);
canvas.drawBitmap(badge, new Matrix(), null);
// finalBitmap will contain your background and its overlay
-I_Artist
Related
In a Framelayout, i have several images. Like a Small markers Image on a big image map in the back.
I am trying to implement a zoom and i was able to implement zoom on one ImageView but rest of the images are not getting zoom In proportionally and position of small images (Marker image) gets affected due to zoom In.
The position of the rest of images should also be zoom in/move with respect to its previous position.
This can be done by merging all your Bitmap on a canvas, that will make one Bitmap as a combination of all bitmap. once canvas is ready just connect it to ImageView.
Bitmap drawnBitmap = Bitmap.createBitmap(1280, 720, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(drawnBitmap);
canvas.drawBitmap(, null, , new Paint());
mapImageView.setImageBitmap(drawnBitmap);
I am using Google Maps V2. I am trying to create custom markers at run time with images and text that come from a server. I would like to have a balloon like marker that would stretch accordingly to the size of the image bitmap with a text on top of it.
For example:
Lets say I have a balloon image like this:
A image like this:
What I am trying to achieve would be to place this image inside the balloon with some text above the image, something like this:
If it is possible, how can I achieve this?
I have to place the final image as a icon for the marker option in google maps, something like this:
// Resource ID from the drawable folder
int balloon_id = getResources().getInteger(R.drawable.balloon_image);
// A bitmap image that came from the server (I got this done)
Bitmap image_from_server;
// Some text that came from the server (I got this done too)
String some_text;
// A method that will return the final resulted image to be placed to the marker options
// What I need is to find out the work out of this method (This is where I am struggling
BitmapDescriptor result_image = some_method(balloon_id, image_from_server, some_text);
// Marker Options for the custom marker
MarkerOptions mo = new MarkerOptions()
.position(latlng)
.icon(result_image);
// Then I can have my marker
Marker my_marker = google_map.addMarker(mo);
I have looked into StackOverflow for similar solutions to no avail. The accepted answer would contain the method that I am struggling to build or a very good direction to where I can achieve the solution on my own (but please take a look at the parameters of my method and what it returns, if I need to adjust that is not a big problem), thanks in advance!
Well, you can draw your images on canvas, overlaying each other. This function would do just that.
so you just put your images/text in the correct position and create them as bitmap, then overlay them in order.
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
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.
I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.
I want to create a bitmap / image which has many images like "Collage" which has more then one images in a single picture.
I have stored all my images in a grid view but now i want to create a single image from all those images. And even i want to make few images click able
so what can be the road map to do this ? any sort of help / example will be helpful.
reference image
Bitmap pic1 = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
Bitmap pic2 = BitmapFactory.decodeResource(getResources(), R.drawable.pic2);
Bitmap bg= BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap out1 = Bitmap.createBitmap(bg) ;
Canvas comboImage = new Canvas(out1);
comboImage.drawBitmap(pic1, 10f, 20f, null);
comboImage.drawBitmap(pic2, 30f, 40f, null);
out1 will have pic1 & pic2, with a background image bg.
To create a single image from multiple images look at using Canvas. You can put bitmaps (and drawables) on a canvas. You can commit the changes and then push then to a single bitmap that you can then use. As far as making certain sections clickable after making it one single image, I will leave this up to someone else to explain, I am not worked directly with the ontouch() functions.