私は5つのImageViewと5つのボタンを持っています。今私が望むのは、ユーザーが3番目のボタンをクリックしてイメージをキャプチャし、そのイメージを3番目のイメージビューで設定し、他のイメージビューで同じイメージを設定する場合です。どのようにこれを達成することができますか?以下は私のコードです。Diffrent 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;
}
}
}