负载可绘制在GridView控件与" Bitmapfun项目"从Android开发者 [英] Load drawables in GridView with "Bitmapfun project" from Android Developer

查看:131
本文介绍了负载可绘制在GridView控件与" Bitmapfun项目"从Android开发者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的项目我的Eclipse:项目

I have this project in my Eclipse: PROJECT

和我想从我的资源文件夹中加载可绘制,而不是来自互联网。

and I want to load drawables from my resources folders and not from Internet.

这个项目是我所需要载入图像Asyncrounsly和缓存和diskcache的方法。我需要这一切方法为我的项目,我不知道该怎么做负载可绘制,而不是互联网的图像。

This project have the method I need for load images Asyncrounsly and cache and diskcache. I need all this methods for my project and I don't know how to do it for load drawables and not Internet Images.

如果有人能帮助我下载名为BitmapFun该项目将帮助我很多。

If anyone can help me downloading the project called BitmapFun will help me a lot.

我在这里贴一些有趣的code,便于帮助:

I paste here some interesting code for easy help:

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;
    }
}

任何人都可以说我,如果这个项目我可以加载可绘制文件夹,而不是从Interet图片?

Anyone can say me if with this project I can load images from drawables folder and not from Interet?

在此先感谢!

编辑: 我知道,这code正常工作与互联网的图像,这就是我想要的,但与文件夹可绘制的图像。 所以,我试图改变code这样的:

I know that this code works correctly with Images from Internet and it's that I want but with folder drawables images. So, I tried to change code like:

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

但它没有工作,因为给一个错误,因为不是一个互联网的形象。

but it not work because give a error because isn't a Internet image.

任何建议?

推荐答案

我知道你可以运行这个项目的Eclipse ADT!按照此步骤...

I know how you can run this project on Eclipse ADT! Follow this steps...

1 - 在Eclipse ADT导入项目 2 - 复制启动COM在java文件夹到src Android项目的所有文件夹 3 - 请注意,您的项目中有一些错误,你需要改变Android项目建设目标,使这个,你必须右键点击机器人 - >属性 - > Android的 - >选择安卓4.4.2 - >应用 - >确定 4 - 添加了位于ADT束外部JAR -... - > SDK - >附加功能 - >机器人 - >支持 - > V4 - > Android的支持-V4。要做到这一点,右键单击Android项目 - >属性 - > Java构建路径 - >添加外部JAR。 5 - 清洁​​工程 6 - 运行它

1 - Import project in eclipse adt 2 - copy all folders that initiate "com" in the java folders to the src Android project 3 - Note that your project have some errors, you need to change the android project build target, to make this, you have to right click on android -> Properties -> Android -> select Android 4.4.2 -> Apply -> Ok 4 - Add external jar that located in adt-bundle-... -> sdk -> extras -> android -> support -> v4 -> android-support-v4. To do this, right click on android project -> Properties -> Java Build Path -> Add External Jars. 5 - clean project 6 - run it.

希望我帮助。

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

Ps:. The structure of the project was different because it was made by Studio IDE

这篇关于负载可绘制在GridView控件与&QUOT; Bitmapfun项目&QUOT;从Android开发者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆