繁体   English   中英

将位图设置为图像src

[英]Set bitmap to an image src

即时通讯试图设置一个位图来成像类似:

android:src="img"

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

android:background="img"

所以无论imageview中的图像是不是黑色,我都不想用黑色背景,这是我得到的不好的图像: Bad 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>

我的位图对于所有图像都工作良好,除了像这样的图像,其背景填充为黑色。

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

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

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

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

imageView.setBackgroundColor(color); imageView.setImageBitmap(bitmap);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM