繁体   English   中英

从位图将下载的图像转换为png / jpeg,并在imageView中显示-Android

[英]Convert downloaded image to png/jpeg from bitmap, and display in imageView - Android

我目前有以下代码,该代码转到指定的URL,将其中的任何图像下拉,将其转换为位图,然后在imageView中显示。

但是,我注意到当我滚动浏览包含这些位图的listView时; 滚动变得混乱(因为正在渲染多个位图)。

我的问题是。 给出以下代码,我将如何将下载的位图转换为png / jpeg格式并在imageView中显示? 这样listView的滚动将不再混乱。

我不想将其保存到文件中,然后从手机加载该文件,因为一次可能会下载100多个图像。

位图代码:

//This downloads the image from thr url passed, and converts into a bitmap format; so that android compilers can read it and then display it.
private Bitmap GetImageBitmapFromUrl(string url)
{
    Bitmap imageBitmap = null;

    // bmp.compress(Bitmap.CompressFormat.PNG, 100, out); //100-best quality

    using (var webClient = new WebClient())
    {
        var imageBytes = webClient.DownloadData(url);
        if (imageBytes != null && imageBytes.Length > 0)
        {
            imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);                   
        }
    }

    return imageBitmap;
}

我如何将其绑定到imageView:

var imageBitmap = GetImageBitmapFromUrl(item.imgURL);
view.FindViewById<ImageView>(Resource.Id.Image).SetImageBitmap((Bitmap.CreateScaledBitmap(imageBitmap, 100, 100, false)));

我一直在尝试使用loooooooooads,以使其高效运行,但除了保存它们之外,我仍然没有找到足够好的解决方案。 基本上,您可以尝试全部保存它们,然后在渲染发生后设置删除,或者在所有图像渲染后设置删除? 我将所有图像都保存在sd卡上,但每个图像仅100kb,但每个“用途”确实有400张图像,但在8gb sd卡上仍然只有40mb。

图像的抖动与显示图像所用的格式无关。 将图像设置为ImageView时,无论如何它们都会在内部转换为位图。 此处的核心是确保所使用的图像不超过所需大小。 例如,如果ImageView的宽度为100x100像素,而您正在下载1000x1000像素的图像,则将其设置到ImageView可以用于显示目的,但是会占用过多的内存。 相反,您应该下载图像,缩放图像以使其与ImageView的大小匹配,然后回收原始位图。

由于内存占用空间要小得多,因此混乱也不会那么明显。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM