Android圖片ImageLoader的基本使用
阿新 • • 發佈:2018-11-21
Mainactivity
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.example.day6_imageloader.image.ImageUrl; import com.example.day6_imageloader.util.ImageOptionsUtil; import com.nostra13.universalimageloader.core.ImageLoader; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button All_btn; private ImageView Get_Img; private ImageLoader mImageLoader=ImageLoader.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { All_btn = findViewById(R.id.All_btn); Get_Img = findViewById(R.id.Get_Img); All_btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.All_btn: mImageLoader.displayImage(ImageUrl.imageUrls[3],Get_Img,ImageOptionsUtil.getDisplay()); break; } }}
AppLica類註冊
import android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; public class MyAppLica extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration loaderConfiguration=ImageLoaderConfiguration.createDefault(this); ImageLoader.getInstance().init(loaderConfiguration); } } ------注意要在AndroidManifest.xml中註冊 <application android:name=".applica.MyAppLica" </application>
util工具類
import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; public class ImageOptionsUtil { public static DisplayImageOptions getDisplay(){ DisplayImageOptions displayImageOptions=new DisplayImageOptions.Builder() .showStubImage(0) /載入的地址為空時顯示 .showImageForEmptyUri(0) //載入時顯示的圖片 .showImageOnLoading(R.mipmap.ic_launcher) //載入錯誤時顯示 .showImageOnFail(0) //配置是否磁碟快取 .cacheOnDisk(true) //default //配置是否記憶體快取 .cacheInMemory(true) //default //配置圖片如何縮放 .imageScaleType(ImageScaleType.IN_SAMPLE_INT) //default //配置色彩模式 .bitmapConfig(android.graphics.Bitmap.Config.RGB_565) //default //顯示效果:圓形 //.displayer(new CircleBitmapDisplayer()) //顯示效果:圓角 .displayer(new RoundedBitmapDisplayer(20)) //淡入效果 .displayer(new FadeInBitmapDisplayer(2000)) .build(); return displayImageOptions; } }
圖片資源
public class ImageUrl {
public final static String[] imageUrls = new String[]{
"http://img.mukewang.com/54780ea90001f3b406000338.jpg",
"http://img.mukewang.com/547ed1c9000150cc06000338.jpg",
"http://img.mukewang.com/54214727000160e306000338.jpg",
"http://img.mukewang.com/54125edc0001ce6306000338.jpg",
"http://img.mukewang.com/548165820001b4b006000338.jpg",
"http://img.mukewang.com/53d74f960001ae9d06000338.jpg",
"http://img.mukewang.com/547d5a45000156f406000338.jpg",
"http://img.mukewang.com/549bda090001c53e06000338.jpg",
"http://img.mukewang.com/530f0ef700019b5906000338.jpg",
"http://img.mukewang.com/550a87da000168db06000338.jpg",
"http://img.mukewang.com/550a836c0001236606000338.jpg",
"http://img.mukewang.com/550a78720001f37a06000338.jpg",
"http://img.mukewang.com/5513e20600017c1806000338.jpg",
"http://img.mukewang.com/5513a1b50001752806000338.jpg",
"http://img.mukewang.com/550a33b00001738a06000338.jpg",
"http://img.mukewang.com/551380400001da9b06000338.jpg",
"http://img.mukewang.com/54c87c73000150cf06000338.jpg",
"http://img.mukewang.com/5518bbe30001c32006000338.jpg",
"http://img.mukewang.com/5518ecf20001cb4e06000338.jpg",
"http://img.mukewang.com/551916790001125706000338.jpg",
"http://img.mukewang.com/550b86560001009406000338.jpg",
"http://img.mukewang.com/551b98ae0001e57906000338.jpg",
"http://img.mukewang.com/5518c3d7000175af06000338.jpg",
"http://img.mukewang.com/551b92340001c9f206000338.jpg",
"http://img.mukewang.com/552640c300018a9606000338.jpg",
"http://img.mukewang.com/551de0570001134f06000338.jpg",
"http://img.mukewang.com/551e470500018dd806000338.jpg",
"http://img.mukewang.com/5523711700016d1606000338.jpg",
"http://img.mukewang.com/55249cf30001ae8a06000338.jpg",
"http://img.mukewang.com/55237dcc0001128c06000338.jpg"
};
}
佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="300dp"
android:layout_gravity="center"
android:layout_height="50dp"
android:text="合成"
android:id="@+id/All_btn"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher_background"
android:id="@+id/Get_Img"/>
</LinearLayout>