How to scale bitmap to screen size?

Solution 1:

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.

public void main(){
    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;
}

Solution 2:

I had a similar challenge where I wanted to stretch the width to 100% of screen, but keep the width/height ratio intact. So I did this..

Create a ScalableImageView class that extends ImageView:

public class ScalableImageView extends ImageView {
    public boolean isMeasured = true; 

    public ScalableImageView(Context context) {
        super(context);
    }

    public ScalableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScalableImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try
        {
            Drawable drawable = getDrawable();

            if (drawable == null)
            {
                setMeasuredDimension(0, 0);
            }
            else
            {
                int width = MeasureSpec.getSize(widthMeasureSpec);
                int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
                setMeasuredDimension(width, height);
            }
        }
        catch (Exception e)
        {
            isMeasured = false;
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

In the layout XML file, I have a placeholder for the image defined like this:

<ScalableImageView android:id="@+id/image1" 
    android:layout_centerHorizontal="true"
    android:src="@drawable/cam_image_placeholder" 
    android:scaleType="fitCenter" 
    android:layout_gravity="center_horizontal" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:adjustViewBounds="true"
    android:visibility="invisible" 
    android:layout_marginBottom="6px">
</ScalableImageView>

And then load/set it like this:

ScalableImageView imgView = null;
imgView = (ScalableImageView)findViewById(imgViewResource);
imgView.setImageDrawable(myDrawable);
imgView.setVisibility(ScalableImageView.VISIBLE);

Solution 3:

You can use Bitmap.createScaledBitmap(), but make sure to use the correct conversion formula when you use it:

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);

In Bitmap.createScaledBitmap() the units for the width and height are "pixels", while you should always set your size in density-independent pixels (dp units) as described here.

So, if you want to show your images maintaining density independence - i.e. your images will occupy the same portion of screen on any device - you need to use a code similar to the following:

Bitmap b = decodeScaledFile(f);

final float scale = mContext.getResources().getDisplayMetrics().density;
int p = (int) (SIZE_DP * scale + 0.5f);

Bitmap b2 = Bitmap.createScaledBitmap(b, p, p, true);

Solution 4:

first get the screen height and width:
Android: How to get screen dimensions 1: Get screen dimensions in pixels then
have a look at this: Resizing a Bitmap