2

I'm trying to write a simple slide show program. I would like it to display pictures that are different sizes, then scale then , so there width fills the screen. I can change the size of the image view, and in the debugger it was getting set to the new width, but it does not scale the bitmap, is thier a way to do this?????

Code:

ImageView picImage = (ImageView) findViewById(0);// R.id.viewpic);
try {
    String FileName = "canon20.png";
    AssetManager assetManager = getAssets();
    InputStream inputStream;
    inputStream = assetManager.open(FileName);
    Bitmap icon = BitmapFactory.decodeStream(inputStream);

    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    int bw = icon.getWidth();
    float t = (float) screenWidth / (float) bw;

    picImage.setImageBitmap(icon);
    picImage.getLayoutParams().width = screenWidth;
    picImage.getLayoutParams().height = (int) (icon.getHeight() * t);
} catch (IOException e) {
}
zapl
  • 63,179
  • 10
  • 123
  • 154
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • See http://stackoverflow.com/questions/6410364/how-to-scale-bitmap-to-screen-size for scaling a bitmap. – DigitalGhost Aug 28 '12 at 00:18
  • It looks like the picImage's layout params' problem. Can you show the XML code? If you are concerning only fill up available space, consider setting image view's `setScaleType` (http://developer.android.com/reference/android/widget/ImageView.html#setScaleType(android.widget.ImageView.ScaleType) using FILL_XY or MATRIX (for more control). You don't really need to handle the RAW image property in this case. – xandy Aug 28 '12 at 00:44

1 Answers1

2

Yes, there's an extremely easy way to do this:

Just use the following code to rescale your bitmap:

ImageView picImage = (ImageView) findViewById(0);// R.id.viewpic);
try {
String FileName = "canon20.png";
AssetManager assetManager = getAssets();
InputStream inputStream;
inputStream = assetManager.open(FileName);
Bitmap icon = BitmapFactory.decodeStream(inputStream);

int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int bw = icon.getWidth();
float t = (float) screenWidth / (float) bw;

picImage.setImageBitmap(icon);
picImage.getLayoutParams().width = screenWidth;
picImage.getLayoutParams().height = (int) (icon.getHeight() * t);
// The following line is the one that scales your bitmap.
Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, screenWidth, (int) icon.getHeight() * t, false);
} catch (IOException e) {
}
A Random Android Dev
  • 2,691
  • 1
  • 18
  • 17
  • Thank you for your help, guess I only had to add one line of code :) – Ted pottel Aug 28 '12 at 17:05
  • For me, it should be the right answer to this other questions: http://stackoverflow.com/questions/5554682/android-imageview-adjusting-parents-height-and-fitting-width http://stackoverflow.com/questions/2991110/android-how-to-stretch-an-image-to-the-screen-width-while-maintaining-aspect-ra – marceloquinta Sep 27 '13 at 22:00