android 異步加載圖片縮略圖
https://www.cnblogs.com/oldfeel/archive/2012/05/24/2515972.html
建一個AsyncLoadedImage類繼承AsyncTask異步加載類,調用publishProgress方法更新onProgressUpdate貯存縮略圖信息到Adapter。監聽Adapter Change實現異步加載縮略圖。
main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" /> </FrameLayout>
MFile.java
public class MFile extends Activity { private GridView sdcardImages; private FileAdapter fileAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); setupViews(); setProgressBarIndeterminateVisibility(true); new AsyncLoadedImage().execute(); } /* * 釋放Bitmap內存 */ protected void onDestroy() { super.onDestroy(); final GridView grid = sdcardImages; final int count = grid.getChildCount(); ImageView v = null; for (int i = 0; i < count; i++) { v = (ImageView) grid.getChildAt(i); ((BitmapDrawable) v.getDrawable()).setCallback(null); } } /* * 初始化文件瀏覽View */ private void setupViews() { sdcardImages = (GridView) findViewById(R.id.sdcard); sdcardImages.setOnItemClickListener(new fileListener()); fileAdapter = new FileAdapter(getApplicationContext()); sdcardImages.setAdapter(fileAdapter); } /* * 刷新Adapter */ private void addImage(LoadedImage... value) { for (LoadedImage image : value) { fileAdapter.addPhoto(image); fileAdapter.notifyDataSetChanged(); } } /* * 點擊監聽 */ class fileListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) { } } /* * 異步加載縮略圖到LoadedImage然後調用addImage方法更新Adapter */ class AsyncLoadedImage extends AsyncTask<Object, LoadedImage, Object> { @Override protected Object doInBackground(Object... params) { String path = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/hfdatabase/3/img"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } else { File[] files = file.listFiles(); String[] paths = new String[files.length]; Bitmap bitmap; Bitmap newBitmap; for (int i = 0; i < files.length; i++) { paths[i] = files[i].getPath(); try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 10; bitmap = BitmapFactory.decodeFile(paths[i], options); newBitmap = ThumbnailUtils.extractThumbnail(bitmap, 80, ); bitmap.recycle(); if (newBitmap != null) { publishProgress(new LoadedImage(newBitmap)); } } catch (Exception e) { e.printStackTrace(); } } } return null; } @Override public void onProgressUpdate(LoadedImage... value) { addImage(value); } @Override protected void onPostExecute(Object result) { setProgressBarIndeterminateVisibility(false); } } /* * Adapter */ class FileAdapter extends BaseAdapter { private Context mContext; private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>(); public FileAdapter(Context context) { mContext = context; } public void addPhoto(LoadedImage photo) { photos.add(photo); } public int getCount() { return photos.size(); } public Object getItem(int position) { return photos.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { final ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); } else { imageView = (ImageView) convertView; } imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setPadding(8, 8, 8, 8); imageView.setImageBitmap(photos.get(position).getBitmap()); return imageView; } } /* * 這是個保存bitmap的類,加入Adapter的ArrayList中,隨著addImage更新Adapter */ private static class LoadedImage { Bitmap mBitmap; LoadedImage(Bitmap bitmap) { mBitmap = bitmap; } public Bitmap getBitmap() { return mBitmap; } } /* * 消息提示 */ private Toast toast; public void showMsg(String arg) { if (toast == null) { toast = Toast.makeText(this, arg, Toast.LENGTH_SHORT); } else { toast.cancel(); toast.setText(arg); } toast.show(); } }
最後更新:2017-04-02 17:09:25