Code Monkey home page Code Monkey logo

imageloadersample's Introduction

ImageLoaderSample

android端用于异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画。

注意:

需要添加LibraryAndroidBucket项目的支持(日志、线程、util等)的依赖,ImageLoader使用的ThreadPool,修改配置文件代码

初始化配置

创建MyApplication 继承Application,在onCreate中增加如下代码:

        /**
         * Created with IntelliJ IDEA.
         * Author: wangjie  email:[email protected]
         * Date: 14-2-27
         * Time: 上午11:25
         */
        public class MyApplication extends ABApplication{

        @Override
            protected void initImageLoader() {
                ImageLoader.init(getApplicationContext(),
                        new CacheConfig()
                                .setDefRequiredSize(600) // 设置默认的加载图片尺寸(表示宽高任一不超过该值,默认是70px)
                                .setDefaultResId(R.drawable.ic_launcher) // 设置显示的默认图片(默认是0,即空白图片)
                                .setBitmapConfig(Bitmap.Config.ARGB_8888) // 设置图片位图模式(默认是Bitmap.CacheConfig.ARGB_8888)
                                .setMemoryCachelimit(Runtime.getRuntime().maxMemory() / 3) // 设置图片内存缓存大小(默认是Runtime.getRuntime().maxMemory() / 4)
        //                    .setFileCachePath(Environment.getExternalStorageDirectory().toString() + "/mycache") // 设置文件缓存保存目录
                );
            }
        ......
    }

###然后再AndroidManifest.xml中添加:
......

加载图片的调用方式如下:

    holder.progress.setText("0%");
    holder.progress.setVisibility(View.VISIBLE);
    final ViewHolder vhr = holder;
    ImageLoader.getInstances().displayImage(list.get(position), holder.image, new ImageLoader.OnImageLoaderListener() {
        @Override
        public void onProgressImageLoader(ImageView imageView, int currentSize, int totalSize) {
            vhr.progress.setText(currentSize * 100 / totalSize + "%");
        }

        @Override
        public void onFinishedImageLoader(ImageView imageView, Bitmap bitmap) {
            vhr.progress.setVisibility(View.GONE);
        }
    });
    或者:
    ImageLoader.getInstances().displayImage(url, imageIv);
    或者
    ImageLoader.getInstances().displayImage(url, imageIv, 100);

备注

例子中,用到了一部分注解(与ImageLoader功能无关,但是可以简化代码的编写) 可以点下面连接进入AndroidInject AndroidInject

imageloadersample's People

Contributors

wangjiegulu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.