线程位图解码&将位图分配给ImageView

时间:2014-04-02 10:20:35

标签: android bitmap xamarin.android xamarin threadpool

下面的代码没有显示图像,如果我删除了它运行的线程但是加载时间增加了。我该如何解决这个问题?

的活动:

_imgFoo = new ImageView(this);
//layoutparams are set

ThreadPool.QueueUserWorkItem (o => {
    using (var bmp = ImageResizer.DecodeSampledBitmapFromResource (Resources, 
    Resource.Drawable.exampleimage, width, height)) {
        RunOnUiThread (() => _imgFoo.SetImageBitmap (bmp));
        bmp.Dispose ();
    }
});

Image Resizer Class:

public static Bitmap DecodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
{
    // First decode with inJustDecodeBounds=true to check dimensions
    var options = new BitmapFactory.Options {
        InJustDecodeBounds = true,
    };
    using (var dispose = BitmapFactory.DecodeResource(res, resId, options)) {
    }
    // Calculate inSampleSize
    options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.InJustDecodeBounds = false;
    return BitmapFactory.DecodeResource(res, resId, options);
}

1 个答案:

答案 0 :(得分:0)

使用AsyncTask ...

上找到它的工作原理

http://developer.android.com/training/displaying-bitmaps/index.html

重要的是,请阅读4个子主题。

的Lukas