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

本文介绍了一种使用GridView自定义Adapter结合AsyncTask实现图片异步加载的方法,通过在线程中加载图片并更新UI,有效避免了因加载大量图片导致的界面卡顿问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
               
        }
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值