NathanEvans
NathanEvans

Reputation: 25

Android scaling down a Bitmap based on SurfaceView

So I am trying to scale down a bitmap based on the size of a surface view I have written the following code

public Bitmap scaleBitMap(int width, int height, Bitmap b){

    // scale down based on the size of the Surface view.
    // width and height = canvas.getHeight(), canvas.getWidth();
    // this should set the height and width of the bitmap based on a percentage 
    // the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
    Bitmap bitmap = null;
    try{

       bitmap =  Bitmap.createScaledBitmap(b, (height/100 * 10), (width/100 * 10), true);

    }catch(Exception e){

    }

    return bitmap;
}

Ok so this code works! returns a scaled bitmap.

Upvotes: 0

Views: 476

Answers (2)

NathanEvans
NathanEvans

Reputation: 25

It works.. but perhaps i should check my math! here is the working code....

public Bitmap scaleBitMap(int width, int height, Bitmap b){

    // scale down based on the size of the Surface view.
    // width and height = canvas.getHeight(), canvas.getWidth();
    // this should set the height and width of the bitmap based on a percentage
    // the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
    Bitmap bitmap = null;

    try{

        bitmap = (Bitmap) createScaledBitmap(b, (height/100 * 22), (width/100 * 22), false);

    }catch(Exception e){
        Log.d("Bitmap","Issue: "+e);
    }

    return bitmap;
}

Upvotes: 0

user1888162
user1888162

Reputation: 1735

I am not sure what are you trying to do, but i have done similar thing before. This code scales bitmap to match screensize. Maybe it will help you.

public Bitmap scaleBitMap(int width, int height, Bitmap b){

    // scale down based on the size of the Surface view.
    // width and height = canvas.getHeight(), canvas.getWidth();
    // this should set the height and width of the bitmap based on a percentage 
    // the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
    Bitmap bitmap = null;
    try{
        float scaledvalues[] = scale(b.getWidth(), b.getHeight(), width, height);
        bitmap =  Bitmap.createScaledBitmap(b, scaledvalues[1], scaledvalues[0], true);

    }catch(Exception e){

    }

    return bitmap;
}
    public float[] scale(int bitmapWidth, int bitmapHeight, int viewWidth, int viewHeight){
        float scaledheight = -1f;
        float scaledwidth = -1f;
        float scaledheightpros = -1f;
        float scaledwidthpros = -1f;
        float finalheight = -1f;
        float finalwidth = -1f;
        if(bitmapHeight > viewHeight){
            scaledheight = bitmapHeight - viewHeight;
            float s = scaledheight*100f;
            scaledheightpros = s / 100f;
        }
        if(bitmapWidth > viewWidth){
            scaledwidth = bitmapWidth - viewWidth;
            float z = scaledwidth * 100f;
            scaledwidthpros = z / bitmapWidth;
        }
            if(scaledheightpros > scaledwidthpros){
                float a = bitmapHeight/100f;
                float b = bitmapWidth/100f;
                finalheight = bitmapHeight - (a * scaledheightpros);
                finalwidth = bitmapWidth - (b * scaledheightpros);
            }
            else{
                float a = bitmapHeight/100f;
                float b = bitmapWidth/100f;
                finalheight = bitmapHeight - (a * scaledwidthpros);
                finalwidth = bitmapWidth - (b * scaledwidthpros);
            }

        float array[] = {finalwidth, finalheight};
        return array;
    }

Upvotes: 1

Related Questions