【问题标题】:Set bitmap to an image src将位图设置为图像 src
【发布时间】:2016-05-18 02:33:07
【问题描述】:

我试图设置一个位图来显示类似的图像:

android:src="img"

但仅来自代码,我从服务器获取 img。 但是当我在做 setImageBitmap() 方法时 它像在背景中一样显示它,就像我会这样做:

android:background="img"

所以无论图像视图中的图像不是黑色,我不想要黑色背景,这是我得到的坏图像: Bad Image

我想得到的图像是: Good Image

如何将代码中位图的图像设置为 xml 中的 src? 太棒了!

这是我的 xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:id="@+id/profile_image"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_centerHorizontal="true"
    app:civ_border_width="5dp"
    app:civ_border_color="#c5eaf8"
    android:elevation="10dp"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:layout_centerInParent="true"/>

    <ProgressBar
        android:id="@+id/progress_bar_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_centerInParent="true"
        android:visibility="gone"/>
</RelativeLayout>

我的位图适用于所有图像,除了像这样的图像,它用黑色填充背景..

【问题讨论】:

  • 请提供minimal reproducible example。很可能,您的问题在于如何创建与setImageBitmap() 一起使用的位图。您似乎正在丢失 Alpha 通道。如果我们能看到您的代码,我们只能提供帮助。

标签: java android image background


【解决方案1】:

问题是你设置的是透明图片,背景是黑色的。

查看类似问题的答案:https://stackoverflow.com/a/20402227/1281775

另一种方法是在当前图像下方创建另一个具有所需背景颜色的 ImageView。

【讨论】:

  • 我试过了,没用,除非你真的想用彩色图片?像我的情况下的蓝色图片?无论如何检查我的代码
  • 你试过什么?尝试我分享的链接中提供的解决方案,或者只使用 setBackground 方法。
【解决方案2】:

您在图像中看到的黑色部分是透明的。将您的图像更改为非透明背景或更改背景颜色。

imageView.setBackgroundColor(颜色); imageView.setImageBitmap(位图);

【讨论】:

  • 请检查您的图像一次,它的背景颜色是什么。其他一切似乎都很好。
猜你喜欢
  • 2013-10-08
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2012-11-23
  • 2013-04-23
  • 1970-01-01
  • 2013-10-28
  • 2018-12-09
相关资源
最近更新 更多