【Android图片加载】浅析ImageLoader

    xiaoxiao2025-01-21  1

    ImageLoader

    一、项目地址

    https://github.com/nostra13/Android-Universal-Image-Loader

    二、Gradle dependency

    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

    三、使用

    1、Application

    public class App extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(this); //Initialize ImageLoader with configuration. ImageLoader.getInstance().init(config); } }

    2、AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="你的包名"> <uses-permission android:name="android.permission.INTERNET" /> <!-- Include next permission if you want to allow UIL to cache images on SD card --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:name=".App" android:……> </application> </manifest>

    3、Layout and Activity

    3.1、activity_main.xml

    <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@mipmap/ic_launcher" /> </FrameLayout>

    3.2、MainActivity.java

    public class MainActivity extends AppCompatActivity { ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.image_view); String imageUrl = "https://img-blog.csdn.net/20160809001416757"; ImageLoader.getInstance().loadImage(imageUrl, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { mImageView.setImageBitmap(loadedImage); } @Override public void onLoadingCancelled(String imageUri, View view) { } }); } }
    转载请注明原文地址: https://ju.6miu.com/read-1295711.html
    最新回复(0)