Ameya Kulkarni
Ameya Kulkarni

Reputation: 198

Set same image as background of imageview using picasso

I am using Picasso to take image using URL. I am trying to set the same image as background image of the imageview after darkening it a little bit. I'm able to get the image but there is no way to set the same image as background of imageview. My current code is as follows :

ActivityLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    >
    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            >

            <ImageView
                android:id="@+id/main.backdrop"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_gravity="center"
                android:scaleType="centerInside"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

...
</android.support.design.widget.CoordinatorLayout>

Implementation in Activity :

imageView = (ImageView)findViewById(R.id.main_backdrop);
Picasso.with(getActivity())
            .load("someurl")
            .placeholder(ContextCompat.getDrawable(getActivity(),R.drawable.mplaceholder))
            .error(ContextCompat.getDrawable(getActivity(),R.drawable.merrorimage))
            .into(imageView);

Upvotes: 1

Views: 830

Answers (1)

Ramy Sabry
Ramy Sabry

Reputation: 176

I think you create new Target https://stackoverflow.com/questions/20181491/use-picasso-to-get-a-callback-with-a-bitmap

private Target target = new Target() {
  @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
      imageView.setImageBitmap(bitmap);
      // edit your bitmap and set as background
      BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
      imageView.setBackgroundDrawable(ob);
  }

  @Override
  public void onBitmapFailed(Drawable errorDrawable) {
  }

  @Override
  public void onPrepareLoad(Drawable placeHolderDrawable) {
  }
}

Upvotes: 1

Related Questions