Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: quality of the images resized in runtime

I want to show images using drawBitmap() method. But before I want to resize it according screen dimensions. I have a problem with quality of result image. You can see screenshots and test code below. How can I resize images in runtime with a good quality?

Thank you for solutions.

Original image: alt text http://dl.dropbox.com/u/709109/gradient.png

Result screenshot on device: alt text http://dl.dropbox.com/u/709109/device.png

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import java.io.IOException;
import java.io.InputStream;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout container = (LinearLayout) findViewById(R.id.container);
        container.addView(new View(this) {
            @Override
            protected void onDraw(Canvas canvas) {
                Bitmap unscaledBitmap;
                try {
                    InputStream in = getAssets().open("images/gradient.png");
                    unscaledBitmap = BitmapFactory.decodeStream(in);
                    in.close();
                    canvas.drawBitmap(unscaledBitmap, null, new Rect(0, 0, 320, 480), null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
like image 748
shinydev Avatar asked Jan 11 '10 10:01

shinydev


People also ask

Do images lose quality when resized?

Does resizing an image affect its quality? It definitely can! Typically, making an image smaller will not impact the quality, but an image can suffer quality loss when scaled beyond its original size.

Why do images lose quality when resized?

Resizing images can be tricky, because when you reduce the size of an image, you are also reducing the number of pixels. The more you reduce the number of pixels, the more you reduce the quality of the image.

Does PNG lose quality when resized?

Remember that GIF and JPEG codecs are “lossy,” meaning some resolution will be lost when scaling images. PNG files, on the other hand, will not degrade in quality. Note that the quality can be lost when resizing or re-formatting bitmap files such as GIF, JPEG, and even some PNGs.


1 Answers

Instead of resizing at drawing time (which is going to be very costly), try to resize in an offscreen bitmap and make sure that Bitmap is 32 bits (ARGB888).

like image 68
Romain Guy Avatar answered Oct 26 '22 23:10

Romain Guy