Simple way to put graphic on the screen in android - java

I've been looking around for a simple way to put graphic on the screen in android for a while now and I'm really confused.
I've a simple game written in java with swing for graphics and that's all I really need as the graphics doesn't really matter in this project.
Now we want to rewrite it so it works on devices with android and honestly I can't find a simple way to just put a image on the screen.
I'd love to avoid using complex game engine because I just don't need it. All I want is a possibility to draw an image (or 50 images) on x,y given by me and refreshing a screen every 100 milliseconds and I thought I'd be the first thing I'd learn in any android tutorial but well... it's not. Of course I know how to draw an Image with .xml but I need something more automatic - for dozens of images changing all the time.
So what is the best to do this? Isn't there really any way to do it with just some android built in function? If not what engine should I use just for the simplest things I mentioned?

A simple but not necessarily very clean way to do this would be to create an xml layout that is just an empty RelativeLayout.
Then you could create your images like this:
ImageView image = new ImageView(this);
image.setLayoutParams(new RelativeLayout.LayoutParams(IMAGE_WIDTH,IMAGE_HEIGHT));
//Other setup code for your image goes here
myRelativeLayout.addView(image);
This should give you an image of the specified width and height that sits at the origin.
Then you could move the image into position like this:
image.setX(IMAGE_X_POS);
image.setY(IMAGE_Y_POS);
When to do the moving is up to you as it will depend on when you know the positions that need to be moved to.

Related

Are there any ways to modify a JavaFX Image Object?

I'm showing an Image on a Canvas using JavaFX, but I would like the color of certain parts of the image to vary based on a variable. Ordinarily, I imagine this would be accomplished by creating a color mask and adding that to the image, but as far as I can find JavaFX Image doesn't allow any operations like that, and from the documentation it looks like JavaFX Images can only be loaded from a file. I don't want to modify and then reload the file each time the color changes, because that seems pretty inefficient performance-wise.
My goal is to create an interactive offline app in which a small character is shown, and the user can (for example) use an input to change the characters eye/hair color.
Are there any ways to accomplish this? Essentially doing basic image masking/coloration with JavaFX Images and canvases. (Not ImageView)
Thanks for any advice you can give me!

One image works fine and other one slow code so much

So I am doing small game in android studio - java. And I was using 400x400 .png image as a ship. It worked well.
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.playermove),(int)(length),(int)(height),false);
I am using drawBitmap to draw image on screen.
Now I want to use different ship. Only thing that I changed is that instead of this image I use another one that is 150x150 also .png and game becomes so laggy and sloopy.
Length and Height are 1/10 size of the screen. I have 8 ship pictures with similair dimensions and every one make game sloppy.
Any idea why those pictures make everything sloppy and first one doesn't?
You are doing that on the UI thread. You shouldn't!
I'd recommend not to scale Bitmap dynamically, as bitmap scaling is bit expensive, and if you are doing that in onDraw() which would be triggered every ~17ms, doing redundant/expensive work is bad. I'd suggest you to preprocess the image and keep a scaled version of it and just use it to draw every time.
you are not optimizing your images and this is a background work. try to do this in background.
also this post may help you.

Java StdDraw animation

So I need to make a simple game for a project, and the StdDraw from princeton library was provided.
From looking through the source code it seems like the library handles animation by drawing on two (for double buffering) BufferedImages and then displays on on a JFrame. To increase the window size the BufferedImage is simply increased in size and the drawings scaled up. But this causes severe lag and screen tear when going above say 700x700 pixels.
I don't know enough about Java to know if this is the most efficient method of doing animation but there must be a way to animate a simple triangle fullscreen without lag. I can easily edit the library or even just write my own if someone could give me some pointers on what is the best way to handle animation.
Google drive for StdDraw.java if anyone wants to look

Trying to make animation on SurfaceView's canvas

I've made a game with a player that looks like this:
I want to make him blink. For this, I've made, with photoshop, a few images that corresponds to the flutter:
Depending on the time, I draw an image or I draw another one, but this takes a lot of memory and makes my app go slow, any alternative to this method? Thanks in advance !
Depending on task it exist few solutions.
1) AnimationDrawable you could just add few your drawable resources. Then you will be able to use this Drawable with ImageView, ProgresBar, etc.
2) If you really want to use SurfaceView, you could merge your res into one mipmap file, and then draw different parts of this mipmap.
3) Performance solution. you could use your mipmap as texture on gl es scene. There is a lot topics about it.
Try to create a unique image containing all the animation states.
It is more expensive to open and modify several small files than one big file.

Create an Image out of a larger Image

For a game I'm making I am storing all the sprites for the map inside one large(er) image. I want to be able to create an instance of Image for each image inside of the larger image that has all the sprites.
So how would I create an instance of Image from a set position of another Image.
The basic solution (if all tiles in your tilesheet have the same size) is to use the getSubimage(xpos, ypos, XSIZE, YSIZE) method from the class BufferedImage.
Otherwise, you'll have to store a set of position and size for all sprites in another file.
Your question is similar to this others one.
Since you are using the java.awt.Image-class i am guessing you are trying to create a reasonable game using AWT? You really should take a look at a different technology like http://en.wikipedia.org/wiki/Java_OpenGL. The problem you ran into (partitioning a sprite-sheet) is typical for a lot of other problems (rotation...) you will run into if you try to develop a game without something like Open-GL.
Considering only the problem at-hand: you can easily solve this in Open-GL by binding the whole sprite-sheet as texture (glBindTexture()) and giving for each glVertex() a glTexCoord2f(), no need to cut-out parts of the sprite-sheet.

Categories

Resources