【问题标题】:Android: Trying to scale a ImageView, so image fills width and scaled to heightAndroid:尝试缩放 ImageView,因此图像填充宽度并缩放到高度
【发布时间】:2012-08-28 00:08:33
【问题描述】:

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

代码:

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

【问题讨论】:

标签: android


【解决方案1】:

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

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
相关资源
最近更新 更多