我有五个ImageView和五个按钮,然后点击每个按钮相机打开点击图片。现在我想要的是,如果用户点击第三个按钮并捕获图像,则在第三个imageview中设置该图像,对其他图像设置相同。怎么能实现这个?以下是我的代码。
public class ConDetTenthFragment extends Fragment implements OnClickListener {
//keep track of cropping intent
final int PIC_CROP = 3;
final int CAMERA_CAPTURE = 1;
Bitmap thePic;
Uri picUri;
public ImageView imageView1, imageView2, imageView3, imageView4, imageView5;
public Button camera1, camera2, camera3, camera4, camera5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.con_det_tenth_fragment, container, false);
camera1 = (Button) rootView.findViewById(R.id.camera1);
camera2 = (Button) rootView.findViewById(R.id.camera2);
camera3 = (Button) rootView.findViewById(R.id.camera3);
camera4 = (Button) rootView.findViewById(R.id.camera4);
camera5 = (Button) rootView.findViewById(R.id.camera5);
imageView1 = (ImageView) rootView.findViewById(R.id.imageView1);
imageView2 = (ImageView) rootView.findViewById(R.id.imageView2);
imageView3 = (ImageView) rootView.findViewById(R.id.imageView3);
imageView4 = (ImageView) rootView.findViewById(R.id.imageView4);
imageView5 = (ImageView) rootView.findViewById(R.id.imageView5);
//Button Click Event
camera1.setOnClickListener(this);
camera2.setOnClickListener(this);
camera3.setOnClickListener(this);
camera4.setOnClickListener(this);
camera5.setOnClickListener(this);
return rootView;
}
public static ConDetTenthFragment newInstance(String text) {
ConDetTenthFragment f = new ConDetTenthFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK) {
//user is returning from capturing an image using the camera
if (requestCode == CAMERA_CAPTURE) {
//get the Uri for the captured image
Uri uri = picUri;
Log.d("picUri", uri.toString());
//carry out the crop operation
performCrop();
} else if (requestCode == PIC_CROP) {
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
thePic = (Bitmap) extras.get("data");
//display the returned cropped image
imageView1.setImageBitmap(thePic);
}
}
}
public void selectImage1() {
try {
//use standard intent to capture an image
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
File imageFile = new File(imageFilePath);
picUri = Uri.fromFile(imageFile); // convert path to Uri
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(takePictureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.camera1:
// code
selectImage1();
break;
case R.id.camera2:
// code
selectImage1();
break;
case R.id.camera3:
// code
selectImage1();
break;
case R.id.camera4:
// code
selectImage1();
break;
case R.id.camera5:
// code
selectImage1();
break;
default:
break;
}
}
}
答案 0 :(得分:1)
使用一些字符串或int来存储要保存在哪个imageview中的图像。
String setInImageView="1";
public void onClick(View v) {
switch (v.getId()) {
case R.id.camera1:
// code
setInImageView="1";
selectImage1();
break;
case R.id.camera2:
// code
setInImageView="2";
selectImage1();
break;
case R.id.camera3:
// code
setInImageView="3";
selectImage1();
break;
case R.id.camera4:
// code
setInImageView="4";
selectImage1();
break;
case R.id.camera5:
// code
setInImageView="5";
selectImage1();
break;
default:
break;
}
}
在onActivityResult中,
if(setInImageView.equals("1")
imageView1.setImageBitmap(thePic);
else if(setInImageView.equals("2")
imageView2.setImageBitmap(thePic);
else if(setInImageView.equals("3")
imageView3.setImageBitmap(thePic);
else if(setInImageView.equals("4")
imageView4.setImageBitmap(thePic);
else if(setInImageView.equals("5")
imageView5.setImageBitmap(thePic);
答案 1 :(得分:0)
enter code herepublic void onClick(View v) {
image1=false;image2=false;.......image5=false;
switch (v.getId()) {
case R.id.camera1:
// code
image1=true; ///Boolean value
selectImage1();
break;
case R.id.camera2:
// code
image2=true; ///Boolean value
selectImage1();
break;
case R.id.camera3:
// code
image3=true; ///Boolean value
selectImage1();
break;
case R.id.camera4:
// code
image4=true; ///Boolean value
selectImage1();
break;
case R.id.camera5:
// code
image5=true; ///Boolean value
selectImage1();
break;
default:
break;
}
}
为每次点击设置布尔值,以识别要设置的图像....并全局声明所有布尔变量。
并在设置如下图像时检查条件
public void onActivityResult(int requestCode,int resultCode,Intent data){ if(resultCode == getActivity()。RESULT_OK){
//user is returning from capturing an image using the camera
if (requestCode == CAMERA_CAPTURE) {
//get the Uri for the captured image
Uri uri = picUri;
Log.d("picUri", uri.toString());
//carry out the crop operation
performCrop();
} else if (requestCode == PIC_CROP) {
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
thePic = (Bitmap) extras.get("data");
//display the returned cropped image
if(image1){
imageView1.setImageBitmap(thePic);
}
else if(image2){
imageView2.setImageBitmap(thePic);
}
//// like wise
}
}
希望你能...让我知道如果解决了。