<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="60dp" android:background="@android:color/holo_blue_light" android:gravity="center" android:text="ImagleLoager_Gridview" android:textColor="@android:color/white" android:textSize="25sp" /> <android.support.v4.view.ViewPager android:id="@+id/viewPage" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="60dp" android:layout_height="60dp" android:orientation="vertical"> <ImageView android:src="@drawable/ic_launcher" android:id="@+id/image" android:layout_width="50dp" android:layout_height="50dp" /> <ProgressBar android:id="@+id/loading" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_light" /> </LinearLayout>
package application.weiyuan.com.listviewimageloader; /** * Created by Administrator on 2017/7/26. */ public class Contants { public static String[] imageUrls = new String[] {"http://h.hiphotos.baidu.com/image/w%3D230/sign=6e399656d343ad4ba62e41c3b2035a89/a08b87d6277f9e2fb00276fb1d30e924b899f30a.jpg" ,"http://h.hiphotos.baidu.com/image/w%3D230/sign=2c133914cdfc1e17fdbf8b327a91f67c/a8ec8a13632762d098b92abda2ec08fa513dc629.jpg" ,"http://g.hiphotos.baidu.com/image/w%3D230/sign=776ceec6c1cec3fd8b3ea076e689d4b6/faedab64034f78f0394889f97b310a55b3191c7c.jpg" ,"http://a.hiphotos.baidu.com/image/w%3D230/sign=a97edd106259252da3171a07049a032c/7dd98d1001e9390151b8b23479ec54e736d19605.jpg" ,"http://c.hiphotos.baidu.com/image/w%3D230/sign=5889f92579f40ad115e4c0e0672d1151/1b4c510fd9f9d72a32abde00d62a2834359bbbe4.jpg" ,"http://imgt8.bdstatic.com/it/u=2,926638203&fm=25&gp=0.jpg" ,"http://imgt8.bdstatic.com/it/u=2,926635772&fm=25&gp=0.jpg" ,"http://imgt9.bdstatic.com/it/u=2,926635207&fm=25&gp=0.jpg" ,"http://imgt8.bdstatic.com/it/u=2,926635826&fm=25&gp=0.jpg" ,"http://imgt7.bdstatic.com/it/u=2,767513653&fm=25&gp=0.jpg" ,"http://imgt9.bdstatic.com/it/u=2,861946843&fm=25&gp=0.jpg" ,"http://imgt7.bdstatic.com/it/u=2,956835248&fm=25&gp=0.jpg" ,"http://f.hiphotos.baidu.com/image/w%3D230/sign=1d32d901b9a1cd1105b675238913c8b0/d01373f082025aaf41e143faf8edab64034f1a2b.jpg" ,"http://imgt6.bdstatic.com/it/u=2,926633844&fm=25&gp=0.jpg" ,"http://imgt6.bdstatic.com/it/u=2,975762047&fm=25&gp=0.jpg" ,"http://imgt7.bdstatic.com/it/u=2,851741187&fm=25&gp=0.jpg" ,"http://imgt9.bdstatic.com/it/u=2,851680066&fm=25&gp=0.jpg" ,"http://imgt6.bdstatic.com/it/u=2,851165675&fm=25&gp=0.jpg" ,"http://imgt8.bdstatic.com/it/u=2,771262254&fm=25&gp=0.jpg" ,"http://imgt8.bdstatic.com/it/u=2,829083522&fm=25&gp=0.jpg"}; }
package application.weiyuan.com.listviewimageloader; import android.app.Activity; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ViewParent; import android.widget.GridView; import android.widget.ListView; public class MainActivity extends Activity { private ViewPager viewPage; private ViewPagerAdapter listViewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initView() { viewPage = (ViewPager) findViewById(R.id.viewPage); } private void initData() { // 创建适配器 listViewAdapter = new ViewPagerAdapter(MainActivity.this); // 添加适配器到listview中 viewPage.setAdapter(listViewAdapter); } }
package application.weiyuan.com.listviewimageloader; import android.app.Application; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.QueueProcessingType; /** * Created by Administrator on 2017/7/26. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // 初始化参数 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .threadPriority(Thread.NORM_PRIORITY - 2) // 线程优先级 .denyCacheImageMultipleSizesInMemory() // 当同一个Uri获取不同大小的图片,缓存到内存时,只缓存一个。默认会缓存多个不同的大小的相同图片 .discCacheFileNameGenerator(new Md5FileNameGenerator()) // 将保存的时候的URI名称用MD5 .tasksProcessingOrder(QueueProcessingType.LIFO) // 设置图片下载和显示的工作队列排序 .writeDebugLogs() // 打印debug log .build(); ImageLoader.getInstance().init(config); } }
package application.weiyuan.com.listviewimageloader; import android.content.Context; import android.graphics.Bitmap; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.FailReason; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; /** * Created by Administrator on 2017/7/27. */ public class ViewPagerAdapter extends PagerAdapter{ private final DisplayImageOptions options; private final ImageLoader imageLoader; private final Context mContext; public ViewPagerAdapter(Context context){ // 获取上下文 mContext = context; // 获取ImageLoader实例 imageLoader = ImageLoader.getInstance(); // 配置图片加载参数 options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片 .resetViewBeforeLoading(true) // 设置图片在下载前是否重置,复位 .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中 .imageScaleType(ImageScaleType.EXACTLY) // 设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型 .displayer(new FadeInBitmapDisplayer(300)) // 设置图片渐变显示 .build(); } @Override public int getCount() { return Contants.imageUrls.length; } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); ((ViewPager) container).removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { View imageLayout = View.inflate(mContext, R.layout.listview_item, null); ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image); final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading); // 加载图片 imageLoader.displayImage(Contants.imageUrls[position], imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { // 显示加载进度条 spinner.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { String message = null; // 获取图片失败类型 switch (failReason.getType()) { case IO_ERROR: // 文件I/O错误 message = "Input/Output error"; break; case DECODING_ERROR: // 解码错误 message = "Image can't be decoded"; break; case NETWORK_DENIED: // 网络延迟 message = "Downloads are denied"; break; case OUT_OF_MEMORY: // 内存不足 message = "Out Of Memory error"; break; case UNKNOWN: // 原因不明 message = "Unknown error"; break; } Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show(); // 隐藏加载进度条 spinner.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { // 隐藏加载进度条 spinner.setVisibility(View.GONE); // 不显示圆形进度条 } }); ((ViewPager) container).addView(imageLayout, 0); // 将图片增加到ViewPager return imageLayout; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="application.weiyuan.com.listviewimageloader"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>