Last active
August 29, 2015 14:01
-
-
Save Phonbopit/b7d34920c72a88e42e53 to your computer and use it in GitHub Desktop.
Universal Image Loader Basic Article here : http://devahoy.com/2014/05/getting-started-with-usiversal-image-loader/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.devahoy.sample.uil" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-sdk | |
android:minSdkVersion="10" | |
android:targetSdkVersion="18" /> | |
<application | |
android:name=".MyApplication" | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView); | |
// เรียก global ImageLoader จากเมธอด static getInstance() | |
ImageLoader imageLoader = ImageLoader.getInstance(); | |
// ให้ ImageLoader โหลดรูปใส่ ImageView ของเรา | |
// parameter แรกเป็น URI ของรูป, อันสองเป็น ImageView | |
// ตัวอย่างผมโหลดรูปหุ่นเขียวของ Android | |
String imageUri = "https://developer.android.com/images/brand/Android_Robot_100.png"; | |
imageLoader.displayImage(imageUri, imageView); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imageLoader.displayImage(imageUri, imageView, new ImageLoadingListener() { | |
@Override | |
public void onLoadingStarted(String imageUri, View view) { | |
} | |
@Override | |
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { | |
imageView.setImageResource(R.drawable.image_fail); | |
} | |
@Override | |
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { | |
Animation anim = new AlphaAnimation(0, 1); | |
anim.setDuration(1000); | |
imageView.setAnimation(anim); | |
} | |
@Override | |
public void onLoadingCancelled(String imageUri, View view) { | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application | |
android:name="MyApplication" | |
... > | |
<activity> | |
... | |
</activity> | |
</application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devahoy.sample.uil; | |
import android.app.Application; | |
import com.nostra13.universalimageloader.core.DisplayImageOptions; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; | |
public class MyApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
// ทำการสร้าง Builder ขึ้นมา | |
DisplayImageOptions.Builder optionBuilder = new DisplayImageOptions.Builder(); | |
// จากนั้นก็เซ็ตออปชันต่างๆ ตามรายการข้างล่างนี้ | |
// ตั้งว่าจะโชว์รูปอะไร เมื่อ ImageView ไม่มีภาพ | |
optionBuilder.showImageForEmptyUri(R.drawable.ic_launcher); | |
// จะให้โชว์รูปอะไร ถ้าโหลดรูปภาพมาแสดงไม่ได้ | |
optionBuilder.showImageOnFail(R.drawable.ic_launcher); | |
// ตั้งให้ cache รูปลง memory | |
optionBuilder.cacheInMemory(true); | |
// ตั้งให้ cache รูปลงเครื่อง | |
optionBuilder.cacheOnDisk(true); | |
// จากนั้นก็ build ค่าทั้งหมด ใส่ตัวแปร options | |
DisplayImageOptions options = optionBuilder.build(); | |
ImageLoaderConfiguration.Builder loaderBuilder = | |
new ImageLoaderConfiguration.Builder(getApplicationContext()); | |
// ตั้งค่า option โดยใช้ options ที่ได้ตั้งค่าไว้ด้านบน | |
loaderBuilder.defaultDisplayImageOptions(options); | |
// ตั้งค่าให้ cache รูปมีขนาด 240x240 | |
loaderBuilder.diskCacheExtraOptions(240, 240, null); | |
ImageLoaderConfiguration config = loaderBuilder.build(); | |
// ตั้งค่าออปชันเริ่มต้นที่ได้ประกาศไว้ทั้งหมด ให้กับ ImageLoader | |
ImageLoader.getInstance().init(config); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment