Search code examples
androidbitmapimageviewscale

how to scale image in android


this is my code i want to scale my drawable image and set on image view please help me here in my code i want to scale bitmap1 please check my code

         protected Bitmap murge() 
         { 
        Murge_Bitmap = null;
        Murge_Bitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(Murge_Bitmap);
        Resources res = getResources(); 
        bitmap1 = BitmapFactory.decodeResource(res, R.drawable.sample_pic); 
        Bitmap bitmap2=Bitmap.createScaledBitmap(bitmap1, 600, 600, true);

        Drawable drawable1 = new BitmapDrawable(Bitmap_recieve);
        Drawable drawable2 = new BitmapDrawable(bitmap2);
         //   c.drawBitmap(Murge_Bitmap, 500,500, null);
        drawable1.setBounds(200, 200, 400, 400);
        drawable2.setBounds(200, 240, 400, 400);
        drawable1.draw(c);
        drawable2.draw(c);


        return Murge_Bitmap;
             }

Solution

  • You can use this: Decode the resource and pass it to this function with scalingfactor

    public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
        int scaleHeight = (int) (bm.getHeight() * scalingFactor);
        int scaleWidth = (int) (bm.getWidth() * scalingFactor);
        return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
    }
    

    for scaling factor refer to this Scale factor for xxhdpi android?