displaying a custom map and placing pin/images on the map - java

i wanted to create a directory app for a grocery. i already have the floor plan of the place. i just wanted to display it in a view and i want to put some markers/pins/images to it.. i have no idea where to start. someone told me to use bitmap and canvass but i have no idea how to use it as the other tutorials in google is quite vague and hard to understand..
what my app requires:
display a custom map from an image source
enable me to put markers,pins and some images on top of it

A simple way is to use use ImageView:
//make a bitmap from your floorPlanImage, example from file
Bitmap floorPlanImage = BitmapFactory.decodeFile(filePath, options);
//make a mutable bitmap, so it can be changed
Bitmap mutableFloorPlanImage = floorPlanImage.copy(Bitmap.Config.ARGB_8888, true);
//create a canvas to draw on your floorPlanImage
Canvas mCanvas = new Canvas(mutableFloorPlanImage);
//draw a marker or another image on the floorPlanImage, you can position it using left and top //coordinates, in this example both 0
mCanvas.drawBitmap(markerBitmap, 0, 0, null);
//show the resulting bitmap in ImageView
imageView.setImageBitmap(mutableFloorPlanImage);

Related

How do I get the ID of a drawable and give it to a function?

I have this scrollview in which I want to inflate a 1920x1080 background image. If I add it statically via xml, my app lags in animations and general responsiveness. I checked StackOverflow and it's something that is expected to happen because images are uncompressed in memory.
Now, in the same thread linked above, a suggested answer is to use a setBackground function which uses BitmapFactory to dynamically resize and set the image as follows:
fun setBackground(context: Context, view: View, drawableId: Int) {
var bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId)
val width = Resources.getSystem().getDisplayMetrics().widthPixels
val height = Resources.getSystem().getDisplayMetrics().heightPixels
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true)
val bitmapDrawable = BitmapDrawable(context.getResources(), bitmap)
view.background = bitmapDrawable
}
However, as can be seen, this image requires the drawable's ID, which I haven't been able to get. I checked similar questions and found conflicting answers with some saying that it's not possible to get drawable IDs, and other's suggesting some ImageView-related methods that aren't related to my problem (inflating a scrollview). So, can anybody please name a method in Java/Kotlin that allows me to get my drawable image's ID? If that's not possible, can you please suggest another workaround?
Have you tried:
val resID = resources.getIdentifier("drawable_name", "drawable", packageName)

Getting image from Webview to ImageView

I have a custom WebView. Inside it I've got an image. The WebView is zoomable. I want to reset zoom when I click a button, extract image from there and place inside ImageView.
So far, with the help of x-code I've managed to write this in onClickListener:
while (wv2.zoomOut()){
wv2.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(wv2.getDrawingCache());
bitmapPath = bitmap;
wv2.setDrawingCacheEnabled(false);
new Thread(new Runnable() {
public void run() {
SaveImage(bitmap);
}
}).start();
tstImage.setImageBitmap(bitmap);
}
The WebView zooms out completely, but the Bitmap I get in ImageView is slightly zoomed in. I need to click 1 more time to get the whole bitmap inside the ImageView
I would say that the Javadoc of WebView.getDrawingCache() method explains your problem.
public Bitmap getDrawingCache (boolean autoScale)
...
Note about auto scaling in compatibility mode: When auto scaling is
not enabled, this method will create a bitmap of the same size as this
view. Because this bitmap will be drawn scaled by the parent
ViewGroup, the result on screen might show scaling artifacts. To avoid
such artifacts, you should call this method by setting the auto
scaling to true. Doing so, however, will generate a bitmap of a
different size than the view. This implies that your application must
be able to handle this size.
In your code you simply have to make this change
bitmap = Bitmap.createBitmap(wv2.getDrawingCache(true));

Android Overlaying Bitmap over another Bitmap Issue

I am working on the Android app for the Eyewer Team and I am trying to overlay a Bitmap over another Bitmap before uploading it to my database. I followed many stackoverflow questions and landed on THIS one.
Looks legit but I am facing a bit a difficulty placing the overlay bitmap and its position.
This is where I want it to be placed:
^ Bottom Left Corner
Here's what I have right now:
Bitmap bmOverlay = Bitmap.createBitmap(Bitmap_MainPic.getWidth(), Bitmap_MainPic.getHeight(), Bitmap_MainPic.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(Bitmap_MainPic, new Matrix(), null);
canvas.drawBitmap(overlay_logo, 50,50, null);
How can I position the overlay at the required position using my code or code of your suggestion?

is it right to use batcher.drawSprite to draw all images in game?

I'm developing a game and use batcher.drawSprite method to draw all images in the game (background and all characters)
in assets.java :
charAtlas = new Texture(game, "charAtlas.png");
charEnemy = new TextureRegion(charAtlas, 0,0,250,300);
in worldGame.java :
batcher.beginBatch(Assets.charAtlas); // set atlas
batcher.drawSprite(130, 628, 120,140, Assets.charEnemy);
//assets.charEnemy
is it right to use this method in all condition ?
I have 3 atlas in game , i even use 2048x2048 atlas size so i can include all my images in there..
However, the image looks blurry in game (Tested in galaxy note, tab, and galaxy young). looks at the code above, i even have the enemy char take size in my atlas as much as 250x300 , it's not make sense that it'll look blurry as i only draw it in 120x140.
note : i use no layout (i mean no layout file in res folder) .. i use drawsprite to draw all image (Character,menu, button, etc)..
update :
I tried to use character image files from other game that i unzipped, when i run the app, it also looks blurry and jagged. while in the original game, it's so smooth and sharp. why is that ?
check your code, you might use scaledbitmap , see if you can set like this.
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);

Encoding transparent animated gif in Java

I am using GifDecoder to read an animated .gif file and AnimGifEncoder to write it. (link)
If I display the original frames read by GifDecoder they display correctly and are transparent, but if I display the frames created by AnimatedGifEncoder the transparency is all wrong.
GifDecoder gif = new GifDecoder();
gif.read("image.gif");
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.start("newimage.gif");
e.setTransparent(Color.BLACK);
for (int i=0;i<gif.getFrameCount();i++) {
anim.addFrame(gif.getFrame(i));
anim.setDelay(gif.getDelay(i));
}
anim.finish();
In this example I set the transparent color to black. But actually I want to get the transparent color information from the GifDecoder but I don't know how.
I'm using the following solution, although there are still some gif files that
are a mess after scaling... :
(At least it seems to get along with most non-transparent gifs)
Color transparentColor = null;
BufferedImage firstFrameImage = ImageIO.read([sourceFileHere]);
if (firstFrameImage.getColorModel() instanceof IndexColorModel) {
IndexColorModel cm = (IndexColorModel) firstFrameImage.getColorModel();
int transparentPixel = cm.getTransparentPixel();
transparentColor = new Color(cm.getRGB(transparentPixel), true);
}
e.setTransparent(transparentColor);
This was all a long time ago, but I did manage to get it working at the time. Without digging out the code again... I got it working by:
Scan the original image and set the transparent areas to a colour that does not exist inside the original image.
Set the transparent colour inside the AnimGifEncoder to the colour that was previously assigned to the transparent areas.

Categories