使用Android Developer中的“Bitmapfun项目”在GridView中加载drawable

时间:2013-12-24 10:56:30

标签: android eclipse gridview

我在Eclipse中有这个项目:PROJECT

我想从我的资源文件夹中加载drawable,而不是从Internet加载。

这个项目有我需要的方法来加载图像Asyncrounsly和缓存和diskcache。我需要所有这些方法用于我的项目,我不知道如何为负载drawables而不是Internet图像。

如果有人可以帮我下载名为BitmapFun的项目,将会对我有所帮助。

我在这里粘贴一些有趣的代码以获得简单的帮助:

mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView);

loadImge方法:

/**
 * Load an image specified by the data parameter into an ImageView (override
 * {@link ImageWorker#processBitmap(Object)} to define the processing logic). A memory and
 * disk cache will be used if an {@link ImageCache} has been added using
 * {@link ImageWorker#addImageCache(FragmentManager, ImageCache.ImageCacheParams)}. If the
 * image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask}
 * will be created to asynchronously load the bitmap.
 *
 * @param data The URL of the image to download.
 * @param imageView The ImageView to bind the downloaded image to.
 */
public void loadImage(Object data, ImageView imageView) {
    if (data == null) {
        return;
    }

    BitmapDrawable value = null;

    if (mImageCache != null) {
        value = mImageCache.getBitmapFromMemCache(String.valueOf(data));
    }

    if (value != null) {
        // Bitmap found in memory cache
        imageView.setImageDrawable(value);
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        final AsyncDrawable asyncDrawable =
                new AsyncDrawable(mResources, mLoadingBitmap, task);
        imageView.setImageDrawable(asyncDrawable);

        // NOTE: This uses a custom version of AsyncTask that has been pulled from the
        // framework and slightly modified. Refer to the docs at the top of the class
        // for more info on what was changed.
        task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, data);
    }
}

BitmapWorkerTask方法:

/**
 * The actual AsyncTask that will asynchronously process the image.
 */
private class BitmapWorkerTask extends AsyncTask<Object, Void, BitmapDrawable> {
    private Object data;
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    /**
     * Background processing.
     */
    @Override
    protected BitmapDrawable doInBackground(Object... params) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "doInBackground - starting work");
        }

        data = params[0];
        final String dataString = String.valueOf(data);
        Bitmap bitmap = null;
        BitmapDrawable drawable = null;

        // Wait here if work is paused and the task is not cancelled
        synchronized (mPauseWorkLock) {
            while (mPauseWork && !isCancelled()) {
                try {
                    mPauseWorkLock.wait();
                } catch (InterruptedException e) {}
            }
        }

        // If the image cache is available and this task has not been cancelled by another
        // thread and the ImageView that was originally bound to this task is still bound back
        // to this task and our "exit early" flag is not set then try and fetch the bitmap from
        // the cache
        if (mImageCache != null && !isCancelled() && getAttachedImageView() != null
                && !mExitTasksEarly) {
            bitmap = mImageCache.getBitmapFromDiskCache(dataString);
        }

        // If the bitmap was not found in the cache and this task has not been cancelled by
        // another thread and the ImageView that was originally bound to this task is still
        // bound back to this task and our "exit early" flag is not set, then call the main
        // process method (as implemented by a subclass)
        if (bitmap == null && !isCancelled() && getAttachedImageView() != null
                && !mExitTasksEarly) {
            bitmap = processBitmap(params[0]);
        }

        // If the bitmap was processed and the image cache is available, then add the processed
        // bitmap to the cache for future use. Note we don't check if the task was cancelled
        // here, if it was, and the thread is still running, we may as well add the processed
        // bitmap to our cache as it might be used again in the future
        if (bitmap != null) {
            if (Utils.hasHoneycomb()) {
                // Running on Honeycomb or newer, so wrap in a standard BitmapDrawable
                drawable = new BitmapDrawable(mResources, bitmap);
            } else {
                // Running on Gingerbread or older, so wrap in a RecyclingBitmapDrawable
                // which will recycle automagically
                drawable = new RecyclingBitmapDrawable(mResources, bitmap);
            }

            if (mImageCache != null) {
                mImageCache.addBitmapToCache(dataString, drawable);
            }
        }

        if (BuildConfig.DEBUG) {
            Log.d(TAG, "doInBackground - finished work");
        }

        return drawable;
    }

    /**
     * Once the image is processed, associates it to the imageView
     */
    @Override
    protected void onPostExecute(BitmapDrawable value) {
        // if cancel was called on this task or the "exit early" flag is set then we're done
        if (isCancelled() || mExitTasksEarly) {
            value = null;
        }

        final ImageView imageView = getAttachedImageView();
        if (value != null && imageView != null) {
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "onPostExecute - setting bitmap");
            }
            setImageDrawable(imageView, value);
        }
    }

    @Override
    protected void onCancelled(BitmapDrawable value) {
        super.onCancelled(value);
        synchronized (mPauseWorkLock) {
            mPauseWorkLock.notifyAll();
        }
    }

    /**
     * Returns the ImageView associated with this task as long as the ImageView's task still
     * points to this task as well. Returns null otherwise.
     */
    private ImageView getAttachedImageView() {
        final ImageView imageView = imageViewReference.get();
        final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);

        if (this == bitmapWorkerTask) {
            return imageView;
        }

        return null;
    }
}

任何人都可以说我,如果使用这个项目,我可以从drawables文件夹而不是从Interet加载图像吗?

提前致谢!!!

修改 我知道这个代码可以正常使用来自Internet的图像,这是我想要的,但是有文件夹drawables图像。 所以,我试图更改代码:

mImageFetcher.loadImage(***R.drawables.image233***, imageView);

但它不起作用,因为因为不是互联网图像而给出错误。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我知道如何在Eclipse ADT上运行这个项目!请按照以下步骤...

1 - 在eclipse adt中导入项目 2 - 将在java文件夹中启动“com”的所有文件夹复制到src Android项目 3 - 注意你的项目有一些错误,你需要更改android项目构建目标,要做到这一点,你必须右键点击android - &gt;属性 - &gt; Android - &gt;选择Android 4.4.2 - &gt;申请 - &gt;好 4 - 添加位于adt-bundle -...中的外部jar - &gt; sdk - &gt;额外 - &gt; android - &gt;支持 - &gt; v4 - &gt; Android的支持-V4。为此,右键单击android项目 - &gt;属性 - &gt; Java构建路径 - &gt;添加外部罐子。 5 - 清洁项目 6 - 运行它。

希望我有所帮助。

诗:.项目的结构不同,因为它是由Studio IDE

制作的