2017-09-22 11 views
1

ImageViewを、リモートサーバーからダウンロードしてデバイスのローカルストレージに保存したファイルに設定します。何らかの理由で、画像が歪んでいます。私の研究でscaleTypeadjustViewBoundsmaxWidthmaxHeightというプロパティをImageViewに設定すると、アンドロイドは画像をスケールします。 Android以外のAndroid開発では、画像を表示する前に必ずプログラムのサイズを変更する必要があったため、奇妙に思えます。しかし、this postはAndroidがそれを行うと言います。Progammatically ImageViewによる歪みの設定

これは私が表示しようとしているイメージです。Image

歪んだ画像:

enter image description here

コード:

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

答えて

0

それが誰を助けている場合、私はリサイズ終わりましたこれを使ったコードの画像:

  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(); 
関連する問題