繁体   English   中英

从AsyncTask返回位图

[英]Returning a bitmap from AsyncTask

遵循本教程: http : //developer.android.com/training/displaying-bitmaps/process-bitmap.html

它说使用AsyncTask加载位图。 听起来不错,但在他们的示例中,它使用了ImageView。 我不想使用ImageView,而是创建了自己的View,将其吸引到其中。 在AsyncTask中加载位图后,如何将新加载的位图返回到主线程?

    private Bitmap mLoadedBitmap;

        public void loadBitmap(String path, int width, int height) {
            BitmapWorkerTask task = new BitmapWorkerTask(mLoadedBitmap);
            BitmapTaskParams params = new BitmapTaskParams(path, width, height);

            task.execute(params);

        }

        public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if(height > reqHeight || width > reqWidth) {
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }

            return inSampleSize;
        }

        public static Bitmap decodeSampledBitmapFromFile(String file, int reqWidth, int reqHeight) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(file, options);

            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(file, options);
        }


        private static class BitmapTaskParams {
            private String path;
            private int width; 
            private int height;

            BitmapTaskParams(String path, int width, int height) {
                this.path = path;
                this.width = width;
                this.height = height;
            }
        }


        class BitmapWorkerTask extends AsyncTask<BitmapTaskParams, Void, Bitmap> {
            private final WeakReference<Bitmap> bitmapReference;

            private String path;
            private int width;
            private int height;

            public BitmapWorkerTask(Bitmap bmp) {
               bitmapReference = new WeakReference<Bitmap>(bmp);
            }

            // Decode image in background.
            @Override
            protected Bitmap doInBackground(BitmapTaskParams... params) {
                path = (String)params[0].path;
                width = (Integer)params[0].width;
                height = (Integer)params[0].height;

                return decodeSampledBitmapFromFile(path, width, height);
            }

            // Once complete, see if ImageView is still around and set bitmap.
            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if(bitmap != null) {
                    Bitmap bmp = bitmapReference.get();
                    if(bmp != null) {
                        bmp = bitmap;
//I copied this from the example. How can I actually return the bitmap to a bitmap in my main class / main thread?

                    }
                }
            }
        }

如何在主类/主线程中实际将位图返回到位图?

此代码块:

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(bitmap != null) {
                Bitmap bmp = bitmapReference.get();
                if(bmp != null) {
                    bmp = bitmap;
                }
            }
        }

正在主线程上运行,您已完成,请继续下一步:-)

暂无
暂无

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

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