将位图设置为图像src

时间:2016-05-17 17:27:03

标签: java android image background

我试图将位图设置为像:

android:background="img"

但仅限于代码,我从服务器获取img。 但是当我在做setImageBitmap()方法时 它像在后台一样显示它,就像我会这样做:

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

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

我想得的图像是: Good Image

如何将我的代码中的位图图像设置为xml中的src? 很多!

这是我的xml:

{{1}}

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

2 个答案:

答案 0 :(得分:0)

问题是您正在设置透明图像并且背景填充为黑色。

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

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

答案 1 :(得分:0)

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

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