一般项目中图片加载用的比较多的是ImageLoader 但是需求自己配置一些参数 上手有些复杂
对于手机图库中有上千张图片需要加载时 一个使用性能很好的库Glide可以解决 效果图如下
滑动非常流畅 加载速度也很快 而且使用也非常简单 个人感觉比手机自带的图库效果更佳
上图上代码
Code
public class MainActivity extends Activity { ArrayList<String> fileNames = new ArrayList<String>(); //本地图片路径 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); GridView listView = (GridView) findViewById(R.id.gridview); //ImageAdapter imageAdapter = new ImageAdapter(getApplicationContext(),Images.imageUrls); ImageAdapter imageAdapter = new ImageAdapter(getApplicationContext(), fileNames); listView.setAdapter(imageAdapter); } private void initData() { fileNames.clear(); Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); //图片的保存位置的数据 fileNames.add(new String(data, 0, data.length - 1)); } } }
activity_main
<RelativeLayout 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" tools:context="com.example.imagelist.MainActivity" > <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="3" android:horizontalSpacing="2dp" android:verticalSpacing="2dp" > </GridView> </RelativeLayout>
ImageAdapter
public class ImageAdapter extends BaseAdapter { private Context context; private String[] imageUrls; ArrayList<String> fileNames; private LinearLayout.LayoutParams mImageViewLayoutParams; /* * public ImageAdapter(Context context, String[] imageUrls) { super(); * this.context = context; this.imageUrls = imageUrls; } */ public ImageAdapter(Context context, ArrayList<String> fileNames) { super(); this.context = context; this.fileNames = fileNames; DisplayMetrics dm = context.getResources().getDisplayMetrics(); int wh = dm.widthPixels; int w = (wh - context.getResources().getDimensionPixelSize(R.dimen.test) * 2) / 3; mImageViewLayoutParams = new LinearLayout.LayoutParams(w, w); } @Override public int getCount() { return fileNames.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = LayoutInflater.from(context).inflate(R.layout.item, null); } else { view = convertView; } ImageView image = (ImageView) view.findViewById(R.id.image); image.setLayoutParams(mImageViewLayoutParams); Glide.with(context).load(fileNames.get(position)).centerCrop().placeholder(R.color.test).crossFade() .into(image); return view; } }
item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" /> </LinearLayout>
Demo见github