2

我正在尝试编写一个简单的幻灯片放映程序。我希望它显示不同尺寸的图片,然后缩放,这样宽度就会填满屏幕。我可以更改图像视图的大小,并在调试器中将其设置为新宽度,但它不会缩放位图,这是一种方法吗??????

代码:

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) {
}
4

1 回答 1

2

是的,有一个非常简单的方法可以做到这一点:

只需使用以下代码重新缩放您的位图:

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) {
}
于 2012-08-28T00:53:30.213 回答