2017-09-22 1 views
1

Ich bin ein ImageView auf eine Datei von einem Remote-Server heruntergeladen dann im lokalen Speicher des Geräts gespeichert. Aus irgendeinem Grund wird das Bild verzerrt. Von meiner Forschung, wenn ich die scaleType, adjustViewBounds, maxWidth und maxHeight Eigenschaften der ImageView setze, dann wird Android das Bild für mich skalieren. Das ist merkwürdig, weil ich in meiner anderen, nicht-Android-Entwicklung, immer zuerst das Bild programmatisch anpassen musste, bevor ich es anzeigen konnte. Allerdings sagt this post Android wird es tun.Programmatisch Einstellung ImageView verursacht Verzerrung

Dies ist das Bild, das ich bin zu Anzeige versuchen: Image

Verzerrtes Bild:

enter image description here

Code:

<ImageView 
     android:id="@+id/title_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxWidth="60dp" 
     android:maxHeight="60dp" 
     android:scaleType="fitCenter" 
     android:adjustViewBounds="true" 
     /> 

    String path = getExternalStorageDirectory() + "/MyApp/Images"; 
    String fileName = "Test.jpg"; 
    File imageFile = new File(path, fileName); 

    if (imageFile.exists()) 
    { 
     Uri imageUri = Uri.fromFile(imageFile); 
     ((ImageView)findViewById(R.id.title_image)).setImageURI(thumbnail); 
    } 

Antwort

0

Wenn es jemand hilft, ich Ändern der Größe endete das Bild mit Code unter Verwendung dieses:

  final URLConnection ucon = new URL(url).openConnection(); 
      final InputStream is = ucon.getInputStream(); 
      final Bitmap b = BitmapFactory.decodeStream(is); 

      int origWidth = b.getWidth(); 
      int origHeight = b.getHeight(); 
      float scaleWidth = ((float) width)/origWidth; 
      float scaleHeight = ((float) width)/origHeight; 

      final Matrix matrix = new Matrix(); 
      matrix.postScale(scaleWidth, scaleHeight); 

      Bitmap b2 = Bitmap.createBitmap(b, 0, 0, origWidth, origHeight, matrix, false); 

      ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
      b2.compress(Bitmap.CompressFormat.PNG, 100, outStream); 

      FileOutputStream fo = new FileOutputStream(file); 
      fo.write(outStream.toByteArray()); 
      fo.flush(); 
      fo.close(); 
      outStream.flush(); 
      outStream.close(); 
      b.recycle(); 
      b2.recycle(); 
Verwandte Themen