我正在做一个项目,用户最多可以添加10张图像。当用户单击“添加新图像”按钮时,将创建一个图像视图。我已经在水平滚动的imageview中实现了此功能,以便用户可以水平滚动以查看图像。单击图像后,用户将能够从相机或图库中添加图像。现在的问题是,即使我单击了其他图像视图,图像也总是在最后一个图像视图上设置。我不知道如何在选定的imageview中设置图像。
我已引用此(http://sunil-android.blogspot.in/2013/03/insert-imageview-dynamically-using-java.html)链接来在水平滚动视图中动态创建imageview。
以下是我的代码:
在单击按钮时,将动态添加图像视图:
btn_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addImageView(image_layout);
}
});
这是addImageView函数:
private void addImageView(LinearLayout layout) {
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.gallery);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(220, 220);
imageView.setLayoutParams(layoutParams);
imageView.setPadding(0, 0, 10, 0);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setId(temp);
layout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_id = v.getId();
showDialog(CONTEXT_MENU_ID);
}
});
}
这是用于从相机或图库中选择图像的代码:
protected Dialog onCreateDialog(int id) {
if (id == CONTEXT_MENU_ID) {
return iconContextMenu.createMenu();
}
return super.onCreateDialog(id);
}
@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
SelectedImage = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SelectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
image = Base64.encodeBytes(imageData);
filename = "img_" + System.currentTimeMillis();
imageView.setImageBitmap(SelectedImage); //setting the image
} else {
image = "";
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists())
f.delete();
break;
}
}
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Cannot find image cropping application", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent = new Intent(intent);
co.appIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter(adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
startActivityForResult(
cropOptions.get(item).appIntent,
CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (mImageCaptureUri != null) {
getContentResolver().delete(mImageCaptureUri, null, null);
mImageCaptureUri = null;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
拿一个ImageView
像这样的类变量
ImageView addImage;
像这样更改您的点击方法
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_id = v.getId();
addImage = (ImageView)v // added code
showDialog(CONTEXT_MENU_ID);
}
});
最后在onActivityResult
替换这条线
imageView.setImageBitmap(SelectedImage); //setting the image
和
addImage.setImageBitmap(SelectedImage); //setting the image
希望你能了解动手问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句