PS:这是之前提出的问题的更新。
我有一个自定义相机布局,我需要在单击图像时将图像设置为水平方向。我尝试为此使用 recyclerview。首先,单击的图像存储在名为result的位图中。
然后,我将图像的路径存储在一个名为path的变量中。path 变量通过 bundle 传递给 RecycleViewAdapter。
然后,使用 onBindViewHolder,我尝试在水平视图中显示图像。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
Bitmap result =
BitmapFactory.decodeByteArray(picture, 0, picture.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), result, "Title", null);
horizontalList.add(path);
recycleViewAdapter = new RecycleViewAdapter(horizontalList);
horizontal_rv.setAdapter(recycleViewAdapter);
recycleViewAdapter.notifyDataSetChanged();
mBundle = new Bundle();
CameraActivity.mBundle.putString("abc", path);
horizontalList.add(path);
}
});
camera.captureImage();
}
});
然后传递给适配器类。
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {
Uri uri;
String path;
private List<Bitmap> horizontalList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView image_result;
public MyViewHolder(View view) {
super(view);
image_result = (ImageView) view.findViewById(R.id.image_result);
}
}
public RecycleViewAdapter(ArrayList<Bitmap> horizontalList) {
this.horizontalList = horizontalList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
path= CameraActivity.mBundle.getString("abc");
uri=Uri.parse(path);
holder.image_result.setImageURI(uri);
holder.image_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return horizontalList.size();
}
}
问题:当我单击图像时,每次单击时图像中会显示 2 个副本。并且先前的视图被更改以显示新图像,使计数加倍。此外,该应用程序在最多 3 次点击后崩溃。
请帮忙。
//I assume you are looping here
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), result, "Title", null);
CameraActivity.mBundle.putString("abc",path);
horizontalList.add(result);
// finish loop....................
//notify the adapter of data changes
recycleViewAdapter.notifyDataSetChanged();
在循环内创建一个新的适配器意味着只能显示一个项目,同时效率也很低。循环并添加循环中的元素,并在循环后通知适配器数据更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句