2

我正在尝试从 Android 中的 URL 获取图像并将其设置为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),它以自己的尺寸显示。该怎么办?

4

3 回答 3

5

你试过这个吗?

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);
于 2012-06-29T06:28:43.597 回答
3

为你的 imageview 添加这一行

imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setAdjustViewBounds(true);
于 2012-06-29T06:20:01.480 回答
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);
于 2012-06-29T06:26:14.330 回答