簡體   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