base64图像的recyclelerview - 滚动缓慢和滞后

时间:2017-02-06 12:33:52

标签: android optimization scroll android-asynctask android-recyclerview

我正在使用一个回收站视图,其中我有一些图像列表,我从服务器获取base64字符串。下面是我正在使用的适配器代码,我将base64字符串转换为asynctask中的位图。当我滚动图像列表时,滚动缓慢且滞后。如何优化代码以使其顺利进行?

 public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.MyViewHolder> {

        private ArrayList<Message> imagesEncodedList;
        private Context context;

        public ImageListAdapter(Context context, ArrayList<Message> imagesEncodedList) {
            this.imagesEncodedList = imagesEncodedList;
            this.context = context;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_image_list, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {

            if (imagesEncodedList.get(position).getImage() != null)
            {
                DownloadImageTask downloadImageTask=new DownloadImageTask(holder.imageView);
                downloadImageTask.execute(imagesEncodedList.get(position).getImage());
            }
            else
            {
                DownloadImageTask downloadImageTask=new DownloadImageTask(holder.imageView);
                downloadImageTask.execute(UriToBase64(Uri.parse(imagesEncodedList.get(position).getUri())));
            }

            if(imagesEncodedList.get(position).getPosted())
                holder.item.setBackgroundResource(R.drawable.greenbordercolour);
            else
                holder.item.setBackgroundResource(R.drawable.redbordercolour);
        }

        @Override
        public int getItemCount() {
            return imagesEncodedList.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            private ImageView imageView;
            private LinearLayout item;


            public MyViewHolder(View view) {
                super(view);
                imageView = (ImageView) view.findViewById(R.id.imageItem);
                item=(LinearLayout)view.findViewById(R.id.item);
            }
        }

        private Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {
            return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true);
        }

        private String bitmapToBase64(Bitmap bitmap) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            return Base64.encodeToString(byteArray, Base64.DEFAULT);
        }



        private String UriToBase64(Uri uri) {
            Bitmap bm = null;
            try {
                bm = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
            } catch (IOException e) {
                Log.e("BHUVNESH", "IOException" + e);
                e.printStackTrace();
            }


            Bitmap resizedBitmap = getResizedBitmap(bm, 300, 300);
            return bitmapToBase64(resizedBitmap);
        }

        private class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {
            ImageView imageView;
            DownloadImageTask(ImageView imageView)
            {
               this.imageView=imageView;
            }
            protected Bitmap doInBackground(String... urls) {
                byte[] imageAsBytes = Base64.decode(urls[0].getBytes(), Base64.DEFAULT);
                return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

            }

            protected void onProgressUpdate(Integer... progress) {
            }

            protected void onPostExecute(Bitmap result) {
                imageView.setImageBitmap(result);
            }
        }

    }

2 个答案:

答案 0 :(得分:2)

始终使用picasso / glide库在recyclerview中设置图像。

答案 1 :(得分:0)

删除DownloadImageTask有两个原因:

  1. 将图像转换为base64需要花费太多,稍后您需要将其转换为位图,这样才能完成两倍的工作。

  2. asynctask对这种操作没有好处。

  3. 您应该使用

    等库

    https://github.com/koush/ionhttp://square.github.io/picasso

    或者使用三行代码进行某种操作,您将获得从服务器到imageview的图像并具有质量速度。

    享受。