ThomQ
ThomQ

Reputation: 605

Bitmap.createScaledBitmap won't scale to screen size

Got some problems with createScaledBitmap. It won't fit my screen size, i still have room to the left and right.

Why does this code

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    Resources res = getResources();

    Bitmap source = BitmapFactory.decodeResource(res, R.drawable.red);
    Bitmap overlay = Bitmap.createScaledBitmap(source, width, height, false);

result in this screen:

enter image description here

Any ideas? Thanks in advance!

Upvotes: 0

Views: 2488

Answers (4)

Simon
Simon

Reputation: 11190

Your bitmap is actually larger in height than your ImageView because its the screen size and not the view's size which means the ImageView scale type is being enforced and scaling your bitmap down. Set the scale type on your view in XML or on in code to Matrix. Which will not perform any scaling because the Matrix is set to the identity matrix by default.

Upvotes: 1

orium
orium

Reputation: 3813

I'm not sure, but maybe it's because you not consider action bar height

try this code

    //this for getting actionbar height
    TypedValue tv = new TypedValue();
    int actionBarHeight = 0;
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
    {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
    }
    ////////
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    //
    int height = size.y - actionBarHeight;

    Resources res = getResources();

    Bitmap source = BitmapFactory.decodeResource(res, R.drawable.red);
    Bitmap overlay = Bitmap.createScaledBitmap(source, width, height, false);

Upvotes: 0

Loures
Loures

Reputation: 433

Try this to Decode the Bitmap :

Where imagefilepath is the path name of image,it will be in String covert that to File by using

File photos= new File(imageFilePath); Where photo is the File name of the Image,Now you set your height and width according t your requirements.

Bitmap bitmap = decodeFile(photo);
            bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
            imageView.setImageBitmap(bitmap);

    private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);              
        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale++;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

Upvotes: 0

karvoynistas
karvoynistas

Reputation: 1285

You can use the following code in any activity to get the screen dimensions :

    public static int displayWidth,displayHeight;



    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    displayWidth = dm.widthPixels;
    displayHeight = dm.heightPixels;

and pass the displayWidth and height variables to

Bitmap.createScaledBitmap

method

Upvotes: 0

Related Questions