1.配置ImageLoader,在清单文件中配置App的name
1 package com.jingle.imageloaderstudy.app; 2 3 import com.nostra13.universalimageloader.core.ImageLoader; 4 import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 5 import com.nostra13.universalimageloader.core.assist.QueueProcessingType; 6 7 import android.app.Application; 8 import android.content.res.Configuration; 9 import android.util.Log; 10 11 public class MyApp extends Application { 12 public static ImageLoader imgLoader; 13 14 @Override 15 public void onCreate() { 16 // TODO Auto-generated method stub 17 super.onCreate(); 18 Log.i("yy", "app create!!!"); 19 20 //1.未缓存图片 21 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( 22 getApplicationContext()) 23 .threadPriority(Thread.NORM_PRIORITY - 2) 24 .denyCacheImageMultipleSizesInMemory() 25 .tasksProcessingOrder(QueueProcessingType.LIFO).build(); 26 imgLoader = ImageLoader.getInstance(); 27 imgLoader.init(config); 28 } 29 30 }
2.加载网络图片
1 package com.jingle.imageloaderstudy.activity; 2 3 import com.jingle.imageloaderstudy.R; 4 import com.jingle.imageloaderstudy.app.MyApp; 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.util.Log; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.ImageView; 12 13 public class MainActivity extends Activity { 14 private ImageView img; 15 16 String uri = "http://img13.360buyimg.com/da/jfs/t790/337/331050216/13395/5d79dbdb/55152d13Na6544d7d.jpg"; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 Log.i("yy", "activity create"); 24 initImg(); 25 initBtnLoadImg(); 26 } 27 28 private void initImg() { 29 img = (ImageView) findViewById(R.id.img); 30 } 31 32 private void initBtnLoadImg() { 33 Button btn = (Button) findViewById(R.id.btn); 34 btn.setOnClickListener(new OnClickListener() { 35 36 @Override 37 public void onClick(View v) { 38 LoadImg(); 39 } 40 }); 41 } 42 43 private void LoadImg() { 44 img.setBackground(null); 45 MyApp.imgLoader.displayImage(uri, img); 46 47 } 48 49 }