Last active
December 21, 2015 03:38
-
-
Save donnfelker/6243215 to your computer and use it in GitHub Desktop.
UrlImageView.java - Abstracts Picasso behind a custom ImageView and injects Picasso via Dagger.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import dagger.Module; | |
| import dagger.Provides; | |
| @Module | |
| ( | |
| complete = false | |
| ) | |
| public class BootstrapModule { | |
| @Provides | |
| Picasso providesPicasso(Context context) { | |
| return Picasso.with(context); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FunActivity extends FragmentActivity { | |
| @Override | |
| protected void onCreate(final Bundle savedInstanceState) | |
| { | |
| super.onCreate(savedInstanceState); | |
| // ... other stuff, get data model,etc | |
| // We'll call the data model "UserModel" in this example | |
| UrlImageView avatar = (UrlImageView) findViewById(R.id.user_avatar); | |
| // Load the Remote Image into the URL, thats it! | |
| avatar.setUrl(userModel.getAvatarUrl()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Picasso.with(getContext()).load("http://path/to/some/file.png").into(myImageView); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.donnfelker.view; | |
| import android.content.Context; | |
| import android.util.AttributeSet; | |
| import android.widget.ImageView; | |
| import com.donnfelker.android.R; | |
| import com.donnfelker.app.BootstrapApplication; | |
| import com.squareup.picasso.Picasso; | |
| import javax.inject.Inject; | |
| public class UrlImageView extends ImageView | |
| { | |
| @Inject Picasso picasso; | |
| private int defaultImageId = R.drawable.ic_placeholder; | |
| private String url; | |
| public UrlImageView(final Context context) | |
| { | |
| this(context, null); | |
| } | |
| public UrlImageView(final Context context, final AttributeSet attrs) | |
| { | |
| this(context, attrs, 0); | |
| } | |
| public UrlImageView(final Context context, final AttributeSet attrs, final int defStyle) | |
| { | |
| super(context, attrs, defStyle); | |
| BootstrapApplication.getInstance().inject(this); | |
| } | |
| public String getUrl() { | |
| return url; | |
| } | |
| public int getDefaultImageId() | |
| { | |
| return defaultImageId; | |
| } | |
| public void setDefaultImageId(final int defaultImageId) | |
| { | |
| this.defaultImageId = defaultImageId; | |
| } | |
| public void setUrl(final String url) | |
| { | |
| this.url = url; | |
| picasso.load(getUrl()).placeholder(getDefaultImageId()).into(this); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment