0

我得到一個URL並在Imageview中顯示它。我的設備的自動循環打開。我希望imageview可以根據設備寬度進行縮放。自動旋轉縮放Imageview

這是可能的,當我從一個網址獲取圖像?

+0

集imageview的寬度參數match_parent和scaleType到fitXY –

+0

它伸展我的形象 – user1619306

+0

如果你的規模型行不通的,因爲你要保持縱橫比,看看此http://開發商.android.com/reference/android/widget/ImageView.ScaleType.html如果這也不起作用,你將不得不自己做一些縮放;) –

回答

0

你可以在代碼中做到這一點,如果圖像被調整了,看起來不正確,或者你只是想更多地控制旋轉發生的事情。

首先獲得設備的寬度和高度:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

然後你就可以使用這些信息來調整圖像大小。

您可以設置o.inJustDecodeBounds = true以在不加載圖像的情況下獲取圖像大小。如果圖像很大,您可以調整它的大小。下面的示例代碼。

private Bitmap getBitmap(String path) { 

Uri uri = getImageUri(path); 
InputStream in = null; 
try { 
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP 
in = mContentResolver.openInputStream(uri); 

// Decode image size 
BitmapFactory.Options o = new BitmapFactory.Options(); 
o.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(in, null, o); 
in.close(); 



int scale = 1; 
while ((o.outWidth * o.outHeight) * (1/Math.pow(scale, 2)) > 
     IMAGE_MAX_SIZE) { 
    scale++; 
} 
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", 
    orig-height: " + o.outHeight); 

Bitmap b = null; 
in = mContentResolver.openInputStream(uri); 
if (scale > 1) { 
    scale--; 
    // scale to max possible inSampleSize that still yields an image 
    // larger than target 
    o = new BitmapFactory.Options(); 
    o.inSampleSize = scale; 
    b = BitmapFactory.decodeStream(in, null, o); 

    // resize to desired dimensions 
    int height = b.getHeight(); 
    int width = b.getWidth(); 
    Log.d(TAG, "1th scale operation dimenions - width: " + width + ", 
     height: " + height); 

    double y = Math.sqrt(IMAGE_MAX_SIZE 
      /(((double) width)/height)); 
    double x = (y/height) * width; 

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
     (int) y, true); 
    b.recycle(); 
    b = scaledBitmap; 

    System.gc(); 
} else { 
    b = BitmapFactory.decodeStream(in); 
} 
in.close(); 

Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
    b.getHeight()); 
return b; 
} catch (IOException e) { 
Log.e(TAG, e.getMessage(),e); 
return null; 
}