简体   繁体   中英

Set bitmap to an image src

im trying to set a bitmap to image something like:

android:src="img"

but only from code , and i get the img from the server. but when im doing the setImageBitmap() method it displays it like its in the background,like i would do this:

android:background="img"

so whatever isnt the image in the imageview becomes black and i dont want black background, this is the bad image i get: Bad Image

and the image i want to get is: Good Image

how can i set the image from bitmap in my code to be like src in xml ? ty alot!

here is my 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>

my bitmap is working good with all the images except images like this one its filling their background with black..

The problem is that you're setting a transparent image and the background is filled black.

Check this answer to a similar question: https://stackoverflow.com/a/20402227/1281775

Another way is to create another ImageView below the current one with a desired background color.

The black portion you view in your image is transparent. Either change your image to non-transparent background or change the background color.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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