@Override public void bindView(View view, Context context, Cursor cursor) { Uri contactUri = Contacts.getLookupUri(cursor.getLong(ContactsQuery.ID), cursor.getString(ContactsQuery.LOOKUP_KEY)); ViewHolder holder = (ViewHolder) view.getTag(); holder.text1.setText(cursor.getString(ContactsQuery.DISPLAY_NAME)); holder.icon.assignContactUri(contactUri); PicassoProvider.get() .load(contactUri) .placeholder(R.drawable.contact_picture_placeholder) .tag(context) .into(holder.icon); }
@Override public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.sample_widget); // Load image for all appWidgetIds. Picasso picasso = PicassoProvider.get(); picasso.load(Data.URLS[new Random().nextInt(Data.URLS.length)]) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .transform(new GrayscaleTransformation(picasso)) // .into(updateViews, R.id.image, appWidgetIds); } }
@Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.sample_list_detail_item, parent, false); holder = new ViewHolder(); holder.image = view.findViewById(R.id.photo); holder.text = view.findViewById(R.id.url); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Get the image URL for the current position. String url = getItem(position); holder.text.setText(url); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() .load(url) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) .centerInside() .tag(context) .into(holder.image); return view; }
@Test public void invalidPlaceholderImage() { try { new RequestCreator().placeholder(0); fail("Resource ID of zero should throw exception."); } catch (IllegalArgumentException ignored) { } try { new RequestCreator().placeholder(1).placeholder(new ColorDrawable(0)); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator().placeholder(new ColorDrawable(0)).placeholder(1); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void intoImageViewSetsPlaceholderDrawable() { PlatformLruCache cache = new PlatformLruCache(0); Picasso picasso = spy(new Picasso(RuntimeEnvironment.application, mock(Dispatcher.class), UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, NO_HANDLERS, mock(Stats.class), ARGB_8888, false, false)); ImageView target = mockImageViewTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, URI_1, 0).placeholder(placeHolderDrawable).into(target); verify(target).setImageDrawable(placeHolderDrawable); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(ImageViewAction.class); }
@Test public void intoTargetAndNotInCacheSubmitsTargetRequest() { BitmapTarget target = mockTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, URI_1, 0).placeholder(placeHolderDrawable).into(target); verify(target).onPrepareLoad(placeHolderDrawable); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(BitmapTargetAction.class); }
@Test public void intoTargetWithNullUriAndResourceIdSkipsAndCancels() { BitmapTarget target = mockTarget(); Drawable placeHolderDrawable = mock(Drawable.class); new RequestCreator(picasso, null, 0).placeholder(placeHolderDrawable).into(target); verify(picasso).cancelRequest(target); verify(target).onPrepareLoad(placeHolderDrawable); verifyNoMoreInteractions(picasso); }
@Test public void intoImageViewSetsPlaceholderWithResourceId() { PlatformLruCache cache = new PlatformLruCache(0); Picasso picasso = spy(new Picasso(RuntimeEnvironment.application, mock(Dispatcher.class), UNUSED_CALL_FACTORY, null, cache, null, NO_TRANSFORMERS, NO_HANDLERS, mock(Stats.class), ARGB_8888, false, false)); ImageView target = mockImageViewTarget(); new RequestCreator(picasso, URI_1, 0).placeholder(android.R.drawable.picture_frame) .into(target); ArgumentCaptor<Drawable> drawableCaptor = ArgumentCaptor.forClass(Drawable.class); verify(target).setImageDrawable(drawableCaptor.capture()); assertThat(shadowOf(drawableCaptor.getValue()).getCreatedFromResId()) .isEqualTo(android.R.drawable.picture_frame); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); assertThat(actionCaptor.getValue()).isInstanceOf(ImageViewAction.class); }
@Test public void intoRemoteViewsWidgetWithPlaceholderDrawableThrows() { try { new RequestCreator(picasso, URI_1, 0).placeholder(new ColorDrawable(0)) .into(mockRemoteViews(), 0, new int[] { 1, 2, 3 }); fail("Calling into() with placeholder drawable should throw exception"); } catch (IllegalArgumentException ignored) { } }
@Test public void invalidNoPlaceholder() { try { new RequestCreator().noPlaceholder().placeholder(new ColorDrawable(0)); fail("Placeholder after no placeholder should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator().noPlaceholder().placeholder(1); fail("Placeholder after no placeholder should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator().placeholder(1).noPlaceholder(); fail("No placeholder after placeholder should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator().placeholder(new ColorDrawable(0)).noPlaceholder(); fail("No placeholder after placeholder should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void intoRemoteViewsNotificationWithPlaceholderDrawableThrows() { try { new RequestCreator(picasso, URI_1, 0).placeholder(new ColorDrawable(0)) .into(mockRemoteViews(), 0, 0, mockNotification()); fail("Calling into() with error drawable should throw exception"); } catch (IllegalArgumentException ignored) { } }
@Override public View getView(int position, View convertView, ViewGroup parent) { SquaredImageView view = (SquaredImageView) convertView; if (view == null) { view = new SquaredImageView(context); view.setScaleType(CENTER_CROP); } // Get the image URL for the current position. String url = getItem(position); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() // .load(url) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .fit() // .tag(context) // .into(view); return view; }