2016-08-04 3 views
0

고정 된 크기의 이미지 뷰가 사용자 지정 대상의 그림을 보여주는 활동을했습니다. 크기 조정 및 방향 조정은 이미 작동하지만 앱을 시작하면 이미지보기에 작은 회색 상자가 표시됩니다. 그러나 다시로드 할 때 예상대로 그림이 표시됩니다. 아무도 나에게 여기에서 무슨 일이 일어나고 있는지 그리고 어떻게 고칠 수 있을지 말해 줄 수 있습니까? 난 이미 문제를 봤하지만 난 .. 여기 을 내 코드가 실행되는 순서 함께 할 수있는 뭔가가 이미지를 보여주는 내 코드 생각 :첫 번째 시도에서 이미지보기가 이미지를 표시하지 않습니다.

public void refreshImageView(String highlight){ 
    File image; 
    if(highlight==null){ 
     image=new File(mydb.getCityById(cityId).getPicture()); 
    }else{ 
     try{ 
     image=new File(mydb.getHighlightById(highlightId).getFile()); 
     }catch(Exception e){ 
      Log.i(TAG, "There is no file saved for this highlight"); 
      image=new File(""); 
     } 
    } 
    if(image.exists()&&image.toString().endsWith(".jpg")){ 
     mImagePreview.setVisibility(View.VISIBLE); 

     int maxWidth=mImagePreview.getWidth(), maxHeight=mImagePreview.getHeight(); 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inJustDecodeBounds=true; 
     BitmapFactory.decodeFile(image.getAbsolutePath(),options); 
     options.inSampleSize=calculateInSampleSize(options,maxWidth,maxHeight); 
     options.inJustDecodeBounds=false; 
     Bitmap mBitmap=BitmapFactory.decodeFile(image.getAbsolutePath(), options); 
     try{ 
      mBitmap=checkOrientation(image.getAbsolutePath(),mBitmap); 
     }catch(Exception e){ 
      Log.i(TAG, "Picture could not be rotated"); 
     } 
     Bitmap bitmapPrev=Bitmap.createBitmap(
       mBitmap.getWidth()+20, 
       mBitmap.getHeight()+20,Bitmap.Config.ARGB_8888); 

     Canvas canvas=new Canvas(bitmapPrev); 
     canvas.drawColor(R.color.black); 
     canvas.drawBitmap(mBitmap, 10,10,new Paint(Paint.FILTER_BITMAP_FLAG)); 
     mImagePreview.setImageBitmap(bitmapPrev); 
    } 
    else{ 
     mImagePreview.setVisibility(View.GONE); 
    } 
} 

//method for calculating sample size 
private static int calculateInSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight){ 
    final int height=options.outHeight,width=options.outWidth; 
    int inSampleSize=1; 

    if(height>maxHeight || width>maxWidth){ 
     final int heightRatio=Math.round((float)height/(float)maxHeight), 
       widthRatio=Math.round((float)width/(float)maxWidth); 
     inSampleSize=heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 
    return inSampleSize; 
} 

//method to check orientation of picture 
private Bitmap checkOrientation(String path, Bitmap bitmap) throws IOException{ 
    ExifInterface ei = new ExifInterface(path); 
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 
    switch(orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      return rotateImage(bitmap, 90); 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      return rotateImage(bitmap, 180); 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      return rotateImage(bitmap, 270); 
     default: 
      return bitmap; 
    } 
} 

//method to rotate image 
public static Bitmap rotateImage(Bitmap source, float angle) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    Bitmap rotated = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
    return rotated; 

답변

0

왜 모든 코드? 이미지 뷰는 모든 로딩 및 스케일링을 처리 할 수 ​​있습니다!

Uri uri = Uri.fromFile(file); 
imageView.setScaleType(...FIT_XY); 
imageView.setImageURI(uri); 
+0

알아요,하지만 이미지 뷰는 이미지 뷰의 크기로 조정하기 전에 전체 크기로 그림을로드합니다. 이것은 그림의 로딩을 매우 느리게 만들었습니다. 그래서 그것이 내가 좋아하는 이유입니다.하지만 어쨌든 고마워요. –

+0

그러면 subsampling-scale-image-view 라이브러리를 사용해야합니다. 그것은이 모든 것을합니다. https://github.com/davemorrissey/subsampling-scale-image-view에서 가져 오기 – lionscribe

관련 문제