【问题标题】:Setting bitmap though setImageBitmap() results only in gifs being visible in view通过 setImageBitmap() 设置位图只会导致 gif 在视图中可见
【发布时间】:2013-05-02 02:34:19
【问题描述】:

所以我的问题正如标题所述。我正在下载许多不同的图像并缩放它们,然后将它们设置为视图。我的问题是在创建的视图中似乎只有.gif 图像可见,并且应该包含.jpg 图像的视图几乎是空白的。我说几乎是因为每个.jpg 不同形状的视图上似乎都有一个奇怪的小黑点,我所说的微小是指一个周期的大小,所以这些可能是图像,但尺寸缩小了太多。任何帮助... p.s 虽然输出我确信图像正在通过我的位图并进入 setImageBitmap()。

我的位图制作方法:

    @Override   
 protected Bitmap doInBackground(String... url) {

    String url1 = url[0];
    InputStream s = null;
    try {
        s = (new URL(url1)).openStream();
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    final BufferedInputStream is = new BufferedInputStream(s, 32*1024);

        try {
            final Options decodeBitmapOptions = new Options();
            // For further memory savings, you may want to consider using this option
            // decodeBitmapOptions.inPreferredConfig = Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel

            if( parent.getResources().getDisplayMetrics().widthPixels >0) {
                final Options decodeBoundsOptions = new Options();
                decodeBoundsOptions.inJustDecodeBounds = true;
                is.mark(32*1024); // 32k is probably overkill, but 8k is insufficient for some jpgs
                BitmapFactory.decodeStream(is,null,decodeBoundsOptions);
                is.reset();
                final int originalWidth = decodeBoundsOptions.outWidth;
                final int originalHeight = decodeBoundsOptions.outHeight;
                Debug.out("Inbound image preview for : "+url1);
                Debug.out(originalWidth);
                Debug.out(originalHeight);
                // inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
                decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / 2, originalHeight / 2));
            }

            return BitmapFactory.decodeStream(is,null,decodeBitmapOptions);
        } catch( IOException e ) {
            throw new RuntimeException(e); // this shouldn't happen
        } finally {
            try {
                is.close();
            } catch( IOException ignored ) {}
        }


    }

这里也是我下载后设置新图像的地方:

 protected void onPostExecute(Bitmap map) {
 //image is a object of type ImageView that has already been added to to the grand scheme of things
     image.setImageBitmap(map);
 }

我需要更新视图吗?如果是这样,为什么我的 gif 加载正常?

【问题讨论】:

    标签: android image bitmap jpeg gif


    【解决方案1】:

    问题出在我的缩放上,尤其是这一行decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / 2, originalHeight / 2));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2020-07-01
      • 2013-12-12
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多