ImageView imageView = findViewById(R.id.my_image_view); imageView.setImageResource(R.drawable.my_image); imageView.setScaleType(ScaleType.CENTER);
ImageView imageView = new ImageView(context); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_image); imageView.setImageBitmap(bitmap); imageView.setScaleType(ScaleType.FIT_CENTER);This code creates a new ImageView programmatically, sets its image to "my_image" using a Bitmap, and sets the scaling type to "FIT_CENTER." This scales the image so that it fits within the bounds of the ImageView, but maintains aspect ratio and centers it. This method is part of the android.widget package library.