URL 中的 setimageBitmap 不符合我想要的尺寸

use*_*575 2 url android image bitmap image-size

我正在尝试从 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);
Run Code Online (Sandbox Code Playgroud)

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

sun*_*nil 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);
Run Code Online (Sandbox Code Playgroud)