【问题标题】:setimageBitmap from URL does not fit the size I want来自 URL 的 setimageBitmap 不适合我想要的大小
【发布时间】:2012-06-29 06:15:10
【问题描述】:

我正在尝试从 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),它以自己的尺寸显示。怎么办?

【问题讨论】:

    标签: android image url bitmap image-size


    【解决方案1】:

    你试过了吗?

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

    【讨论】:

    • 如果你在 AsyncTask 中执行它就不会。
    • 这对我有用,我从谷歌下载了一张静态地图,然后使用了 'Bitmap.createScaledBitmap()'。
    • @sunil 你能展示我的问题吗,因为我想关注你的回答,因为我想获取保存的搜索网址的图标,但这是我导致此代码出错。这是我的问题。 stackoverflow.com/questions/54233815/…
    【解决方案2】:

    为你的图像视图添加这一行

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

    【讨论】:

    • 成功了。与其他方法相比更可靠
    【解决方案3】:

    这个可以试试,

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

    【讨论】:

    • 没用!我想使用RelativeLayout,因为我想根据它们的X和Y坐标显示图像。
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多