Android-Universal-Image-Loader框架的基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Chen_dSir/article/details/81349466

首先在官网下载:  https://github.com/nostra13/Android-Universal-Image-Loader  下载它的jar文件 

然后打开我们的Android Studio新建一个工程Imagetest. 将当前的工程结构android切换到project,在app目录下面会有一个libs目录,然后将我们下载好的jar文件复制到libs里面。然后鼠标右键点击这个复制到libs目录下的jar文件,选择add as library,它会自动给我们添加依赖并且同步。

然后创建一个类myApplication继承自我们的Application,这里面会完成一些Image_loader的初始化和配置:

public class MyApplication extends Application {

     private static ImageLoader mimageLoader;
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
        mimageLoader = ImageLoader.getInstance();

    }
    public static ImageLoader getMimageLoader() {
        return mimageLoader;
    }
}

然后在MainActivity中,调用它的方法显示一个图片: 

public class MainActivity extends AppCompatActivity {
    private String imageUrl = "http://img.ivsky.com/img/tupian/pre/201411/01/mingzhentan_kenan-007.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageLoader imageLoader = MyApplication.getMimageLoader();
        ImageView imageView = findViewById(R.id.imageView);
        imageLoader.displayImage(imageUrl,imageView);
    }
}

当然我们的activity_main.xml文件中仅仅需要设置一个简单的ImageView就可以了。

最关键的一点,因为我们设置的是自定义的Application,因此需要在AndroidManifest文件中设置Application的name属性为我们自定义的Application属性!!!

最后运行如下:

猜你喜欢

转载自blog.csdn.net/Chen_dSir/article/details/81349466