Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.original_image); Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, originalBitmap.getWidth()*2, originalBitmap.getHeight()*2, false); imageView.setImageBitmap(scaledBitmap);
int reqWidth = 500; int reqHeight = 300; Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.original_image); float widthRatio = (float) reqWidth / originalBitmap.getWidth(); float heightRatio = (float) reqHeight / originalBitmap.getHeight(); float scaleFactor = Math.min(widthRatio, heightRatio); Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, (int)(originalBitmap.getWidth()*scaleFactor), (int)(originalBitmap.getHeight()*scaleFactor), false); imageView.setImageBitmap(scaledBitmap);In this example, the code loads an image from resources and scales it to fit a required width and height. The width and height ratio of the original image are calculated, and the minimum of the two is used as a scale factor. The scaled width and height are calculated based on the original ratios and the scale factor, and the bitmap is set on an ImageView component. The android.graphics package library provides a wide range of classes and methods for performing various graphics operations in Android applications.