2012-06-11 2 views
10

나는 실용적인 책장에서 터치 V2 예제를 사용하여로드 된 비트 맵 이미지로 RelativeLayout의이 갤러리의 이미지. 활동에 이미지가 RelativeLayout의에 비트 맵으로로드 결과 :view.getDrawingCache()를 한 번만 작동

public void getPictureFromFile(Uri targetUri){ 
    try { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = scale(getContentResolver() 
       .openInputStream(targetUri)); 
     workinprogress = BitmapFactory.decodeStream(
       getContentResolver().openInputStream(targetUri), 
       null, options); 
     view.setImageBitmap(workinprogress); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

한 다음 버튼을 클릭, 내가 사용하는 RelativeLayout의 이미지를 잡아 :

   thepicture.buildDrawingCache(true); 
      Bitmap bm = Bitmap.createBitmap(thepicture.getDrawingCache()); 

과정은 훌륭한 작동합니다 - 첫 번째 이미지 용. 다른 이미지를 다시로드하면 전달 된 비트 맵은 여전히 ​​원본과 동일합니다. 나는 thepicture.invalidate() 및 thepicture.resetDrawableState() getDrawingCache() 전에 시도했지만 어느 프레임 레이아웃을 올바른 이미지를 표시 있지만 새로로드 된 그림을 이미지를 업데이트 할 것으로 보인다.

로드하는 두 번째 이미지를 구현하는 데 필요한 drawingCache를 새로 고치는 것에 대해 이해할 수없는 것이 있습니까?

답변

38

한번 더 사용하려면 을 호출 한 후 매번 view.setDrawingCacheEnabled(true)view.setDrawingCacheEnabled(false)을 매번 사용해야합니다. 예 :

imageView.setDrawingCacheEnabled(true); 
imageView.buildDrawingCache(true); 
File imageFile = new File(Environment.getExternalStorageDirectory(), 
     "Pictures/image.jpg"); 
FileOutputStream fileOutputStream = new FileOutputStream(imageFile); 
imageView.getDrawingCache(true).compress(CompressFormat.JPEG, 100, 
     fileOutputStream); 
fileOutputStream.close(); 
imageView.setDrawingCacheEnabled(false); 
+2

+1 이렇게하면 시간 낭비를 막을 수 있습니다. nice 한 dmytro –

+5

드로잉 캐시를 가능하게하는 또 다른 방법으로'imageView.buildDrawingCache()'를 호출 한 다음 드로잉 캐시를 가져 와서'imageView.destroyDrawingCache()'를 호출 할 수도 있습니다. http://developer.android.com/reference/android/view/View.html#buildDrawingCache() – lilbyrdie

+0

이 작업을 완료했습니다. 감사합니다. –