0

This is my code.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.resizedView);
        BitmapFactory.Options options = new BitmapFactory.Options ();
        options.inScaled = false;
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.tutimage1, options);
        Bitmap bmp = getResizedBitmap(largeIcon, 200);
        img.setImageBitmap(bmp);
    }
 public Bitmap getResizedBitmap(Bitmap image, int maxSize)
    {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }

       // return getResizedBitmapWithQuality(image, width, height);
        return  Bitmap.createScaledBitmap(image, width, height, false);
    }

So I tried to research and tested but the result is still the same.

enter image description here enter image description here

Is that because of the size needed is too small?

Original size is 1120 x 2048, the size of the result is 110 x 200.

Nikhil PV
  • 1,014
  • 2
  • 16
  • 29
Eric Chan
  • 1,192
  • 4
  • 16
  • 30
  • Because you are decreasing height of image with 200. getResizedBitmap(largeIcon, 200); – Paresh Mayani Mar 09 '17 at 05:33
  • is there a way to fix this issue? Or should I force to increase height? I need really size of 200. Help me!!! – Eric Chan Mar 09 '17 at 05:43
  • [Compress your image without losing quality](http://stackoverflow.com/questions/28424942/decrease-image-size-without-losing-its-quality-in-android) I hope it's help full to you.. – Sanjay Chauhan Mar 09 '17 at 05:51

1 Answers1

0

Use this function to improve the image bitmap quality

public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}
Jon Stark
  • 215
  • 2
  • 17