您的位置:首页 > 产品设计 > UI/UE

使用GirdView异步加载图片,并对图片进行颜色矩阵变化

2011-12-13 09:28 323 查看
http://www.eoeandroid.com/thread-148172-1-1.html

实现异步加载

原理:

GridViewAdapt 里面设置List<Bitmap>,线程更新此List并通知Adapt实现动态加载功能

核心代码 :

adapt:

public View getView(int position, View convertView, ViewGroup parent) {

                ImageView imageView = new ImageView(context);               

                GridView.LayoutParams params = new GridView.LayoutParams(90, 90);

                imageView.setLayoutParams(params);

                imageView.setImageBitmap(bitmapList.get(position));

               

                return imageView;

        }

AsyncTask:

public class GridViewAsyncTask extends AsyncTask<String, ImageBean, Bitmap>

//此方法用来取到所有的图片路径

        protected Bitmap doInBackground(String... params) {

     for(int position=0; position<params.length;position++)

     {

               Bitmap bitmap = null;

//更新图片

               bitmap = MapHandleUtil.bitmap_Out_Of_Memory_Handle(params[position],90,90);

               if (bitmap != null) {  

//通知线程进行UI更新

               publishProgress(new ImageBean(bitmap,position));

           }  

     }

        return null;

        }

//对UI线程更新

  public void onProgressUpdate(ImageBean... value) {  

             for (ImageBean image : value)

             {                   //修改Adapt中List<Bitmap>

                    gridViewAdapt.getBitmapList().add(image.getBitmap());

                                                  //通知adapt进行更新

                        gridViewAdapt.notifyDataSetChanged();

             }  

    }  

对Bitmap过大产生的oo异常进行优化

//处理图片内存溢出处理//并添加缩略图

//本方法并不是最好的方法,在图片过大时还是会溢出,欢迎大家拍砖修改

        public static Bitmap bitmap_Out_Of_Memory_Handle(String bitmapPath,int w,int h)

        {

                                         //取得图片Bitmap数组

                        InputStream inputStream;

                        byte[] len = null ;

                        try {

                                inputStream = new FileInputStream(bitmapPath);

                                len = DataUtil.streamToBytes(inputStream);

                               

                        } catch (FileNotFoundException e) {

                                e.printStackTrace();

                        }

                        //为取得图片宽高又不至于过多的占用内存采用修改options的方法

                                                   //  这样取得的图片占用内存较小

                BitmapFactory.Options options = new BitmapFactory.Options();  

            options.inJustDecodeBounds = true;  

                        Bitmap bm =  BitmapFactory.decodeByteArray(len, 0, len.length);

            options.inJustDecodeBounds = false;  

                        if(bm ==null)

                        {

                                return null;

                        }

                        int  src_w = bm.getWidth();

                        int  src_h = bm.getHeight();

                        float scale_w = ((float)w)/src_w;

                        float  scale_h = ((float)h)/src_h;

                        Matrix  matrix = new Matrix();//设置图片宽高矩阵

                        matrix.postScale(scale_w, scale_h);

                                                 Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, src_w, src_h, matrix, true);

                       

                        bm.recycle();

                       

                return bitmap;

               

        }

       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matrix null float image string ui
相关文章推荐