2016-08-04 60 views
0

Ich habe eine Aktivität erstellt, bei der ein Bild mit festen Größen ein Bild von einem benutzerdefinierten Ziel anzeigt. Die Skalierung und Ausrichtung funktionieren bereits, aber wenn ich die App starte, zeigt die Bildansicht zuerst ein kleines graues Kästchen an. Wenn ich es aber wieder lade, zeigt es das Bild wie es soll. Kann mir bitte jemand sagen, was hier passiert und wie ich es beheben kann? Ich googeln schon das Problem, aber ich denke, dass es etwas mit der Sequenz zu tun hat, in dem mein Code ausführt .. Hier ist mein Code, um das Bild zu zeigen:Imageview zeigt kein Bild beim ersten Versuch

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; 

Antwort

0

Warum all dieser Code? Die Bildansicht kann sich um das Laden und Skalieren kümmern!

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

Ich weiß, aber dann lädt das Imageview das Bild in voller Größe, bevor Sie es an die Skalierung der Bildansicht anpassen. Dies macht das Laden des Bildes sehr langsam und deshalb habe ich es so gemacht, aber danke trotzdem. –

+0

Sie sollten dann die Subsampling-scale-image-view-Bibliothek verwenden. Es macht all dies und mehr. Holen Sie es bei https://github.com/davemorrissey/subsampling-scale-image-view – lionscribe