zoukankan      html  css  js  c++  java
  • android 异步加载图片缩略图

    建一个AsyncLoadedImage类继承AsyncTask异步加载类,调用publishProgress方法更新onProgressUpdate贮存缩略图信息到Adapter。监听Adapter Change实现异步加载缩略图。

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <GridView
            android:id="@+id/sdcard"
            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,
                                    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();
        }
    }
  • 相关阅读:
    命名空间
    XML
    关于命名空间
    gitbook 入门教程之 gitbook 简介
    git 入门教程之备忘录[译]
    git 入门教程之知识速查
    git 入门教程之忽略文件
    git 入门教程之个性化 git
    git 入门教程之里程碑式标签
    git 入门教程之本地和远程仓库的本质
  • 原文地址:https://www.cnblogs.com/oldfeel/p/2515972.html
Copyright © 2011-2022 走看看