edi233
edi233

Reputation: 3031

Size of bitmaps in android

I have a bitmap :

    private Bitmap bitmap;

    public newStar(Context context) {
        super(context);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_bez_nog);
        canvas.drawBitmap(bitmap, 100, 100, null);
    }
}

How I can change size this bitmap and afer that draw in my activity?

Upvotes: 1

Views: 784

Answers (2)

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

Use the below code::
Bitmap b = returnBitmap(mIcon_val,150,150);////where mIcon_val is bitmap to resize  


private Bitmap  returnBitmap(Bitmap mIcon_val,int width,int height){
Matrix matrix = new Matrix();   
if(width==0)
width = mIcon_val.getWidth();
if(height==0)
height = mIcon_val.getHeight();
matrix.postScale((float)width/mIcon_val.getWidth(), (float)height/mIcon_val.getHeight()); 
Bitmap resizedBitmap = Bitmap.createBitmap(mIcon_val, 0, 0, mIcon_val.getWidth(),  mIcon_val.getHeight(), matrix, true);
return resizedBitmap
}

Upvotes: 2

Bhavin
Bhavin

Reputation: 6010

Give Width and Height and call the Function

Bitmap bm = ReduceSizeBitmap(imagefile, 150, 150);

Now Call the Following Function

Bitmap ReduceSizeBitmap(String file, int width, int height){

 BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

    if (heightRatio > 1 || widthRatio > 1)
    {
     if (heightRatio > widthRatio)
     {
      bmpFactoryOptions.inSampleSize = heightRatio;
     } else {
      bmpFactoryOptions.inSampleSize = widthRatio; 
     }
    }

    bmpFactoryOptions.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
 return bitmap;
}

Other References One & Two

Upvotes: 0

Related Questions