在listView Lags中显示来自SQlite的图像

时间:2017-02-23 12:32:05

标签: android sqlite listview imageview lag

当用户捕获他的图像时,我在列表视图中显示图像和一些文本我将其作为blob存储在db中并在listview中显示它,但是在崩溃时它会滞后太多。

这是我的DbUtitlity类,用于在byte []和位图

中转换图像
public class DbBitmapUtility{
       public static byte[] getImageBytes(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }

    public static Bitmap getImage(byte[] image) {
        return BitmapFactory.decodeByteArray(image,0,image.length);
    }

    public static byte[] getBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        return byteBuffer.toByteArray();
    }


}

这是我的CustomerAdapter类

class CustomerAdapter extends CursorAdapter {
private LayoutInflater mInflater;
private Bitmap bitmap=null;

CustomerAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = mInflater.inflate(R.layout.custom_list, parent, false);
    ViewHolder holder = new ViewHolder();
    holder.txtFname = (TextView) v.findViewById(R.id.txtFname);
    holder.txtLname = (TextView) v.findViewById(R.id.txtLname);
    holder.txtPaid = (TextView) v.findViewById(R.id.txtPaidBill);
    holder.txtUnPaid = (TextView) v.findViewById(R.id.txtUnpaid);
    holder.txtTotalUnPaid = (TextView) v.findViewById(R.id.txtTotalUnpaid);
    holder.txtCustomerID = (TextView)v.findViewById(R.id.txtCustomerID);
    holder.txtMobileNo = (TextView)v.findViewById(R.id.txtMobileNo);
    holder.imageView = (CircleImageView) v.findViewById(R.id.user_pic);
    v.setTag(holder);
    return v;
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    final ViewHolder holder = (ViewHolder) view.getTag();

    //getting image byte[] from db
    byte[] img = cursor.getBlob(cursor.getColumnIndex(Customer.USERAVATAR));
    if(img !=null) {
        //getting bitmap from byte[]
        bitmap = DbBitmapUtility.getImage(img);
    }
    if (bitmap != null) {
        //setting image to imageView
        holder.imageView.setImageBitmap(bitmap);
    }

    holder.txtFname.setText(cursor.getString(cursor.getColumnIndex(Customer.FNAME)));
    holder.txtLname.setText(cursor.getString(cursor.getColumnIndex(Customer.LNAME)));
    holder.txtPaid.setText(cursor.getString(cursor.getColumnIndex(Customer.PAID)));
    holder.txtUnPaid.setText(cursor.getString(cursor.getColumnIndex(Customer.UNPAID)));
    holder.txtTotalUnPaid.setText(cursor.getString(cursor.getColumnIndex(Customer.UNPAID))+" ₹");
    holder.txtCustomerID.setText(String.valueOf(cursor.getInt(cursor.getColumnIndex(Customer.KEYID))));
    holder.txtMobileNo.setText(cursor.getString(cursor.getColumnIndex(Customer.MOBILENO)));

    //imageView Listener
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(context,UpdateCustomer.class);
            i.putExtra("ID", holder.txtCustomerID.getText().toString());
            i.putExtra("FNAME",holder.txtFname.getText().toString());
            i.putExtra("MOBILENO", holder.txtMobileNo.getText().toString());
            context.startActivity(i);

        }
    });

}

private class ViewHolder {
    TextView txtFname;
    TextView txtLname;
    TextView txtPaid;
    TextView txtUnPaid;
    TextView txtTotalUnPaid;
    TextView txtCustomerID;
    TextView txtMobileNo;
    CircleImageView imageView;

  }
}

logcat的 02-23 18:07:16.017 20945-1125/shazdroid.com.raviapp V/FA: Using measurement service 02-23 18:07:16.022 20945-1125/shazdroid.com.raviapp V/FA: Connecting to remote service 02-23 18:07:16.027 20945-20945/shazdroid.com.raviapp W/ActivityThread: AppLock checkAppLockState isAppLocked = false pkgName = shazdroid.com.raviapp showWhenLocked = false 02-23 18:07:16.052 20945-20945/shazdroid.com.raviapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@383cc74 time:368732045 02-23 18:07:16.087 20945-1125/shazdroid.com.raviapp V/FA: Activity resumed, time: 1145561340 02-23 18:07:16.097 20945-1125/shazdroid.com.raviapp D/FA: Connected to remote service 02-23 18:07:16.097 20945-1125/shazdroid.com.raviapp V/FA: Processing queued up service tasks: 1 02-23 18:07:17.862 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0 02-23 18:07:17.982 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1 02-23 18:07:18.782 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0 02-23 18:07:18.817 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1 02-23 18:07:19.177 20945-20945/shazdroid.com.raviapp W/CursorWindow: Window is full: requested allocation 367444 bytes, free space 339159 bytes, window size 2097152 bytes 02-23 18:07:19.592 20945-20955/shazdroid.com.raviapp I/art: Background partial concurrent mark sweep GC freed 1157(61KB) AllocSpace objects, 5(19MB) LOS objects, 20% free, 61MB/77MB, paused 811us total 129.950ms 02-23 18:07:19.822 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0 02-23 18:07:19.832 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1 02-23 18:07:20.932 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0 02-23 18:07:21.047 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1 02-23 18:07:21.117 20945-1125/shazdroid.com.raviapp V/FA: Inactivity, disconnecting from the service 02-23 18:07:21.892 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 0 02-23 18:07:22.017 20945-20945/shazdroid.com.raviapp D/ViewRootImpl: ViewPostImeInputStage processPointer 1

我是android的新手,请帮助我,我听说过缓存,但不知道如何实现它。 如果我将图像设置为一个客户,但是在将图像设置给所有客户之后,即使所有imageView在整个列表中都显示相同的图像,它还可以吗?

0 个答案:

没有答案