PIcture size 4 times actual image - android

I'm making an Android game.
I loaded a 128x240 PNG image with my sprites into a Bitmap object and tried to retrieve a 16x16 tile from it via createBitmap(bitmap, x, y, w, h), but got a 1/4 smaller section of the picture instead.
The width/height of the picture according to getWidth/getHeight is 512x960.
What am I doing wrong?

Probably you have the picture in your drawable folder and testing it on a device with high resolution? Android automatically scales up pictures according to http://developer.android.com/guide/practices/screens_support.html
I think the quick solution is to put the image to a drawable-xhdpi (or according other drawable folder).
EDIT: based on a comment at I don't want Android to resize my bitmap Automatically, you can add a folder "drawable-nodpi" to prevent the resizing

Related

Resize png icon inside Android Studio / IntellIJ

I want a way to resize png icon inside Android Studio, so that I have not resize it from external source. Please note I am not talking about to code. I want resize png icon. (Like 100*100 to 50*50)
Any plugin/ In AS way you know about?
If you mean resizing in code, then you need to get a bitmap from either bitmap drawable / resource stream directly.
Bitmap.createScaledBitmap(yourBitmap, yourWidth, yourHeight, false)
This will scale your bitmap.
If you wanted to get different image size for different density, you are looking for https://plugins.jetbrains.com/plugin/7658-android-drawable-importer, which allows you to batch import your image file and scale your image into different sizes for different dpi.
If you are looking for editing to raw png size, you need to use external tools.

Scaling image without losing quality

Is it possible to scale image without loosing its quality?I have seen posts that say to use Ninepatch images but how can we download an image and convert it to ninepatch image so that i can show it in ImageView.
Is there any other way so that my images with smaller size can be scaled like in whats app
Everytime you scale an image it's losing quality, the only thing you can do to keep the quality is keeping a copy of the original image (which doesn't make much sense since you can just load it again from resources).
If you mean the problem was that android was scaling your images (and changing the quality) you can put images on a folder named "raw" inside res.
Ninepatch images can be made manually by you, you just paint an image that has two extra pixels on width and two on height, and when you save it , name it something.9.png
if you want to know how to draw them correctly follow this: http://developer.android.com/tools/help/draw9patch.html

canvas size Increases the Bitmap Size android

Bitmap icon = BitmapFactory.decodeResource(this.getResources(),R.raw.book11);
Canvas c = new Canvas(icon);
using this line of code for making canvas actually increases the size of the canvas(150x177) from the bitmap(100x118). If I use predefining the size of canvas, the bitmap gets cropped. Help please.
thanks.
I solved mine with this equation:
event.getX()*3/2;
event.getY()*3/2;
the system will automatically up-scale your image asset when you use it for a 'larger/higher' screen resolution/density if that particular asset is missing for that particular configuration (i.e. you're referencing an image on a hdpi device while that image is not present in the drawable-hdpi but only in raw drawable directory).
Note that scaling factor depends on device's screen density. In your case that seems to be 1.5.
EDIT: If not done yet, please check the link.

Android drawing bitmap in lower quality than original image

I have an image which is intended to be a background image, which fills the whole screen. The problem I am having is that the image displayed on the phone is of a much poorer quality than the original image. I have saved the image as a 320x480 jpg image, as my phone's display resolution is 320x480 also, so it is not related to the re-sizing of the image.
Here is a picture of what I'm seeing. On the left is the image opened on my desktop, on the right is a screenshot taken from my android phone.
The image quality on the phone can be improved by adding dither, but it is still not ideal. I'm struggling to understand what is going on. It appears as though the image is being loaded with reduced fidelity. It's almost as if the image is being draw in RGB_565 format, even though the phone is capable of 16m colors and should be able to display the image fine
Lots of people have similar problems with gradients. Try putting this in your activity:
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
What directory contains your image?
You should be careful with densities.
For example, if density of your screen is low and you put image to the drawable-mdpi, it will be blurred even if it has size exactly like screen one.
Try to put image to drawable-nodpi.
It could be the result of the jpg compression, which is lossy, which means you lose quality everytime you save it as a jpg, try using a png instead.

Android - Setting a bitmap as the wallpaper

As part of this application I am making, I want to change my wallpaper on my android device based on a bitmap that the user has selected.
I am able to change the background. However, the background is a completely distorted version of the bitmap. Any idea as to why this would happen?Here is my code:
Bitmap bitmap = ((BitmapDrawable)currentDrawable).getBitmap();
this.setWallpaper(bitmap);
My bitmap has a width of 240px and height of 180px. My emulator is of a size 480px by 800px.
I can scale my bitmap to 480px. However, the bitmap still ends up being distorted on the android wallpaper. Any thoughts on how this can be fixed?
Thanks in advance
You should create multiple images, for each potential (and common) resolution. You don't want any scaling, or the image will be distorted.
UPDATE:
Here is a ton of good information on support multiple screen sizes:
http://developer.android.com/guide/practices/screens_support.html
The wallpaper used by the default Launcher is bigger than the actual screen, that's why you can scroll the content and the wallpaper too.

Categories

Resources