2016-07-22 4 views
0

私は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; 
     } 
    } 

} 

答えて

1

画像ビューに保存する画像を保存するには、文字列またはintを使用します。各セット....とグローバルすべてのブール変数を宣言するために特定された画像のためにクリックするためのブール値を設定し

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); 
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; 
    } 
} 

そして

公共ボイドonActivityResult以下(INT requestCode、INT resultCodeが、インテント・データ)のように画像を設定する際の状態をチェック{ (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 

     } 
    } 

願っていますが...解決すれば教えてください。

関連する問題