【问题标题】:Image from Android expansion file appears too smallAndroid 扩展文件中的图像显示太小
【发布时间】:2013-06-04 22:13:04
【问题描述】:

我正在为我的 Android 应用实施扩展文件。

到目前为止,我已经将几个图像和音频文件放入主扩展文件中(我没有使用补丁扩展文件)。没有任何文件被压缩。

通过应用程序,我可以下载扩展文件,播放音频文件没有任何问题。在播放扩展文件中的音频文件的同时,我还显示了扩展文件中的图像。但是,图像比我预期的要小得多。

图片为 320x400 像素。在实现扩展文件之前,它在我的应用程序中按预期显示。但是,实施后,图像看起来缩小到大约 50px 宽(高度按比例缩小)。

然后我尝试了How to create a drawable from a stream without resizing it 中提供的解决方案。虽然图像确实看起来稍大,但它仍然比我想要的要小得多(现在看起来大约是 100x125 像素)。目前,我显示图像的代码如下所示:

public void displayImageFromExpansionFile(){
    Bitmap b = BitmapFactory.decodeStream(fileStream);
    b.setDensity(Bitmap.DENSITY_NONE);
    Drawable d = new BitmapDrawable(this.getResources(), b);
    imageToDisplay.setImageDrawable(d);
}

public void showImg(int imgNum){
    switch(imgNum){
        case(1):
            try{
                if((getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL){
                    fileStream = expansionFile.getInputStream("filepath inside expansion file for small image");
                }
                else if((getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL){                       
                    fileStream = expansionFile.getInputStream("filepath inside expansion file for normal image");
                }
                else if((getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE){
                    fileStream = expansionFile.getInputStream("filepath inside expansion file for large image");
                }
                else{
                    fileStream = expansionFile.getInputStream("filepath inside expansion file for xlarge image");
                }
                displayImageFromExpansionFile();
            } catch (Exception e) {
                e.printStackTrace();
            }           
            break;
        // more cases here
}

图像似乎仍然没有以其实际大小显示。当我检查扩展文件中的图像时,我可以看到它仍然是 320x400 像素。但是,该应用不会以这些尺寸显示图像。

我可以做些什么来让应用程序以正确的尺寸显示图像?

谢谢!

---更新---

我也试过下面的代码,结果没有区别。它看起来仍然是 100x125 像素,而不是 320x400 像素,就像它的原始大小一样。

public void displayImageFromExpansionFile(int bWidth, int bHeight){
    BitmapFactory.Options bfo = new BitmapFactory.Options();
    bfo.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap b = BitmapFactory.decodeStream(fileStream, null, bfo);
    b.setDensity(Bitmap.DENSITY_NONE);
    imageToDisplay.setImageBitmap(b);
}

目前唯一有效的是Bitmap.createScaledBitmap(b, bWidth, bHeight, true);

在我的手机上,使用上述方法将图像的原始尺寸加倍(到 640x800 像素)会使图像达到预期尺寸,但我想图像可能会在不同的手机上以不同的尺寸出现(可能是因为屏幕密度/尺寸)。当我尝试将 xlarge 图像尺寸加倍并在平板电脑上查看时,图像看起来比应有的要大。

【问题讨论】:

    标签: android android-drawable apk-expansion-files expansion-files


    【解决方案1】:

    最后,我摆弄了显示缩小图像的布局 XML 文件。我像这样更改了 imageView 的属性:

    • 宽度/高度:fill_parent(两者)

    • 比例类型:fitCenter

    现在,图像比我最初预期的要大一些,但我认为这样看起来更好。现在图像的大小一致。问题解决了!

    如果其他人对我遇到的问题有疑问,希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这对我有用:

      public static Bitmap getBitmapExt(Context context, String name){
      
          Bitmap bitmap = null;
      
          try {
      
              if(expansionFile == null){
                  PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
                  expansionFile = APKExpansionSupport.getAPKExpansionZipFile(context, pInfo.versionCode, 0);
              }
      
              InputStream is = expansionFile.getInputStream("images/" + name + ".png");
              bitmap = BitmapFactory.decodeStream(is);
          }
          catch (Exception e){
              e.printStackTrace();
          }
      
          return bitmap;
      }
      

      https://gist.github.com/uebi-belli/3c07323c8055a3b66ccac0d388b2b013

      希望对您有所帮助 =)

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-21
        • 2011-04-03
        • 2016-12-06
        相关资源
        最近更新 更多