来自URL的setimageBitmap不符合我想要的大小

时间:2012-06-29 06:15:10

标签: android image url bitmap image-size

我正在尝试从Android中的网址获取图片并将其设置为ImageView。我正在做的是以下内容:

image = new ImageView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MY_WIDTH, MY_HEIGHT);
params.leftMargin = lefMargin;
params.topMargin = topMargin;
image.setImageBitmap(BitmapFactory.decodeStream((InputStream)new URL(MY_URL).getContent());
relativeLayout.addView(image, params);

但是图像不符合我的尺寸(MY_WIDTH,MY_HEIGHT),它以自己的尺寸显示。怎么办?

3 个答案:

答案 0 :(得分:4)

你试过这个吗?

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(MY_URL).getContent();
Bitmap bitmapScaled = Bitmap.createScaledBitmap(bitmap, MY_WIDTH, MY_HEIGHT, true);
Drawable drawable = new BitmapDrawable(bitmapScaled);
image.setBackgroundDrawable(drawable);

答案 1 :(得分:3)

为你的imageview添加这一行

imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setAdjustViewBounds(true);

答案 2 :(得分:0)

这可能会尝试,

image = new ImageView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MY_WIDTH, MY_HEIGHT);
params.leftMargin = lefMargin;
params.topMargin = topMargin;
image.setLayoutParams(params );
image.setImageBitmap(BitmapFactory.decodeStream((InputStream)new URL(MY_URL).getContent());
relativeLayout.addView(image);