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