One image works fine and other one slow code so much - java

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.

Related

Activity with transparent background and blur-filter

I'm currently working on an application where I at one point want to open an Activity that has a transparent background so that I can see the activity below it. This was simple enough and I solved it quick. However, now I want to also add some kind of blur filter so that the activity below my activity seems blurred out.
I have searched around and tried several different solutions but with no luck so I'm trying to add this as a question. Does anyone know of any good way to solve this?
Thanks
There are plenty of libraries that do this. Here's one list of them. Personally I've used BlurView (I hope linking to one of the libraries is not against site rules) but I've had to patch the library a bit, see this issue - the pull request is not in yet. I've also had performance issues with animations.
Basically all the libraries do the same things:
Create Canvas for a Bitmap, with scaled down dimensions (e.g. 4 times smaller), set the transformation matrix to apply this scaling
Render the background of the window to Canvas
Render the root view of the Activity to the Canvas
Blur the Canvas's Bitmap using ScriptIntrinsicBlur
Draw the Bitmap as the background of a View, scaled back up e.g. 4 times
Why the scaling? For performance reasons. It's much faster to draw everything 4 times smaller, and also the blur effect becomes "stronger" due to the upscaling - there's a limit how much ScriptIntrinsicBlur can blur with one pass, and multiple passes slow things down again.
For API < 14, you could use the flag WindowManager.LayoutParams.FLAG_BLUR_BEHIND.
For higher APIS this is no longer supported.
However, you could do this with a view.
Create a Bitmap from your activity's overall layout and Blur that bitmap with whatever method you want. Add(or unhide) a View in your layout that covers everything and you have your problem solved.

Simple way to put graphic on the screen in android

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.

opengl / JOGL - best way to draw textures

I thought about the best way to draw a picture in OpenGL / JOGL.
I currently program a Game and it is my goal to save the information about a picture in a text file instead of saving the picture.
My idea was to program a method that saves every pixel information (RGB) at the position of X and Y.
Then I draw every pixel and it is finished.
What you think about that idea?
You should simply use TextureIO to make a texture from your picture and use this texture with 4 vertices that have some texture coordinates while drawing. glReadPixels() is very slow, reading each pixel of a picture would take a lot of time, saving its content as a text file would require a lot of memory (saving it as a compressed image in a loss-less format like PNG might be worth a try), drawing each pixel one by one would be a lot slower than drawing a texture. derhass is right. You could vectorize your picture (make a SVG from it) but you would have to rasterize it after or you would have to implement some rendering of vectorized contents and it would be probably slower than using a texture. I'm not sure you really need an offscreen buffer.
I had a similar problem when I began working on my first person shooter. I wasn't using JOGL at the very beginning, I reused the source code of someone else, it relied on software rendering in an image, it was very slow. Then, I used JOGL to draw each pixel one by one instead of using Java2D, it was about 4 times faster on my machine but still very slow for me. At the end, I had to redesign the whole rendering to use OpenGL for what it is for as derhass would say, I used triangles, quads and textures. The performance became acceptable and this is what you should do, use OpenGL to draw primitives and clarify what you're trying to achieve so that we can help you a bit better.

Rescaling png bitmap in android

So i have this image of a green ball in png format(103x104px is the size), and im trying to resize it before i draw it on the screen. Im trying this code, which i found in many threads and also in the android documentation this function is specified as exactly what i should need, but it doesnt work.
Bitmap a=BitmapFactory.decodeResource(getResources(), R.drawable.example);
Bitmap b=Bitmap.createScaledBitmap(a,30,30,false);
and then i draw it with
canvas.drawBitmap(b, x, y ,paint);
But no matter which numbers i give into the createScaledBitmap() the image is still the same on the screen.
Ok, so the solution was in the end pretty simple :D i overlooked one line in the code that i forget to erase when i decided to resize it and that line always backed it up, so this code indeed works. My fault, should have searched my code longer before posting.

Jagged edges on images rendered in Android

I'm currently developing my first Android app and am having some issues rendering images. The image itself is great quality to begin with, but upon rendering it the quality drastically lowers. Edges become jagged and it just looks poorly done. Everyone I've showed it to thus far has almost immediately noticed it, without any prompting about it. [start on left, end on right:]
I'm trying everything I am aware of and every tip I've been able to find by looking around online, but nothing seems to fix it.
Currently, I get the image as a Bitmap and scale it:
Bitmap holeImage = BitmapFactory.decodeResource(res, R.drawable.hole_image);
Bitmap holeImageBMP = Bitmap.createScaledBitmap(holeImage, width, height, true);
Once I have the image, I create a Paint, set a few smoothing attributes to true, and then draw it on the canvas:
Paint smoothingPaint = new Paint();
smoothingPaint.setAntiAlias(true);
smoothingPaint.setFilterBitmap(true);
smoothingPaint.setDither(true);
canvas.drawBitmap(holeImageBMP, 0, 0, smoothingPaint);
Yet, as you can obviously see above, the image quality drastically decreases. I've seen plenty of images being rendered beautifully and I'm honestly just not sure what's going on so any advice would be great!
Other notes: I'm using a SurfaceView method to handle the drawing, similar in nature to the LunarLander example given in the SDK.
Thanks again!
If you aren't restricted to much less colors than the original picture has (Does Android have 256 color modes?), I'd suggest to disable dithering, if you zoom into your picture, it does have a visible effect that perhaps destroys a smooth look.
I think in your case, dithering infers with anti-aliasing by destroying the additional colors that anti-aliasing needs for a smooth look. A quick color count on your pictures (left one about 850, right one about 140) confirms this.
That is probably related to converting images from one format to another. Also, android screens vary from device to device. Try to use another device and it might look better... Almost for sure it will have a different tone.
Try to read this great article on this problem (and banding and dithering) and consider adapting the image you created for it to work better in android devices: http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/

Categories

Resources