@Override public void displayRaw(@NonNull ImageView img, @NonNull String absPath, int width, int height, final IBoxingCallback callback) { String path = "file://" + absPath; RequestCreator creator = Picasso.with(img.getContext()) .load(path); if (width > 0 && height > 0) { creator.transform(new BitmapTransform(width, height)); } creator.into(img, new Callback() { @Override public void onSuccess() { if (callback != null) { callback.onSuccess(); } } @Override public void onError() { if (callback != null) { callback.onFail(null); } } }); }
public static void displayAvatar(@NonNull ImageView dest, Transformation transformation, String url, String tag, @DrawableRes int ifEmpty) { Picasso picasso = PicassoInstance.with(); RequestCreator requestCreator; if (nonEmpty(url)) { requestCreator = picasso.load(url); } else { requestCreator = picasso.load(ifEmpty); } if (transformation != null) { requestCreator.transform(transformation); } if (tag != null) { requestCreator.tag(tag); } requestCreator.into(dest); }
/** * Loads an image into a target view. * @param context context * @param image image * @param centerCrop boolean * @param errorResourceId image of error * @param target imageView * @param callback callback */ public static void loadImage(final Context context, final String image, final boolean centerCrop, final int errorResourceId, final ImageView target, final Callback callback) { Picasso pic = Picasso.with(context); RequestCreator request; int errorRId = errorResourceId; if (image != null ) request = pic.load(image).fit(); else { request = pic.load(errorResourceId).fit(); errorRId = 0; } if (centerCrop) request = request.centerCrop(); if (errorRId != 0) request = request.error(errorResourceId); if (callback != null) request.into(target, callback); else request.into(target); }
@NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View entry = convertView != null ? convertView : inflater.inflate(R.layout.flair_entry, parent, false); String id = ids[position]; ((TextView) entry.findViewById(R.id.flairText)).setText(id); ImageView imageView = (ImageView) entry.findViewById(R.id.flair); Picasso.with(context).cancelRequest(imageView); RequestCreator request = flairStylesheet.loadFlairById(id, context); if(request != null) request.into(imageView); imageView.getLayoutParams().width = (int) (flairStylesheet.prevDimension.width * context.getResources().getDisplayMetrics().density); imageView.getLayoutParams().height = (int) (flairStylesheet.prevDimension.height * context.getResources().getDisplayMetrics().density); return entry; }
@Override public void displayImage(Context context, String path, FixImageView imageView, Drawable defaultDrawable, Bitmap.Config config, boolean resize, boolean isGif, int width, int height, int rotate) { RequestCreator creator = Picasso.with(context) .load(new File(path)) .placeholder(defaultDrawable) .error(defaultDrawable) .rotate(rotate) .networkPolicy(NetworkPolicy.NO_STORE) .config(config) .tag(context); if (resize) { creator = creator.resize(width, height) .centerCrop(); } creator.into(imageView); }
@Override public ILoaderProxy loadImage(View view, String path) { if (view instanceof ImageView) { ImageView imageView = (ImageView) view; //判断缓存中是否已经缓存过该图片,有则直接拿Bitmap,没有则直接调用Glide加载并缓存Bitmap Bitmap bitmap = LruCacheUtils.getInstance().getBitmapFromMemCache(path); if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { RequestCreator requestCreator = getPicasso().load(path); loadOptions(requestCreator).into(imageView); } } return obtain(); }
private RequestCreator loadOptions(RequestCreator requestCreator) { if (options == null) { return requestCreator; } if (options.targetHeight > 0 && options.targetWidth > 0) { requestCreator.resize(options.targetWidth, options.targetHeight); } if (options.isCenterInside) { requestCreator.centerInside(); } else if (options.isCenterCrop) { requestCreator.centerCrop(); } if (options.config != null) { requestCreator.config(options.config); } if (options.errorResId != 0) { requestCreator.error(options.errorResId); } if (options.placeholderResId != 0) { requestCreator.placeholder(options.placeholderResId); } if (options.bitmapAngle != 0) { requestCreator.transform(new PicassoTransformation(options.bitmapAngle)); } return requestCreator; }
@Override public Drawable getDrawable(String source) { RichTextView.URLDrawable urlDrawable = new RichTextView.URLDrawable(); RichTextView.ImageTarget target = new RichTextView.ImageTarget(urlDrawable); RichTextView.this.addTarget(target); RichTextView.ImageHolder holder = mImages.get(source); RequestCreator load = Picasso.with(getContext()).load(source); if ((onImageFixListener != null) && (holder != null)) { onImageFixListener.onFix(holder); if ((holder.width != -1) && (holder != null)) load.resize(holder.width, holder.height); if (holder.scaleType == 1) load.centerCrop(); else load.centerInside(); } load.placeholder(placeImage).error(errorImage).into(target); return urlDrawable; }
/** * Load an agent's or visitor's avatar into an {@link ImageView}. * <br> * Images get transformed into circular shaped. If there's no or no valid * url to the image {@code R.drawable.ic_chat_default_avatar} will be displayed. * * @param imageView the {@link ImageView} * @param avatarUri uri as a {@link String} to avatar image */ static void loadAvatarImage(@NonNull final ImageView imageView, @Nullable final String avatarUri) { final Picasso picasso = Picasso.with(imageView.getContext()); final RequestCreator requestCreator; if(StringUtils.hasLength(avatarUri)) { requestCreator = picasso .load(avatarUri).error(DEFAULT_AVATAR) .error(DEFAULT_AVATAR) .placeholder(DEFAULT_AVATAR); } else { requestCreator = picasso .load(DEFAULT_AVATAR); } requestCreator .transform(new CircleTransform()) .into(imageView); }
public static void setPicasso(Context context, Uri url, ImageView imageView, @DrawableRes int placeHolder) { Picasso picasso = Picasso.with(context); if (BuildConfig.DEBUG) picasso.setLoggingEnabled(true); RequestCreator requestCreator = picasso.load(url); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { requestCreator.placeholder(placeHolder); requestCreator.error(placeHolder); } else { Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, placeHolder); requestCreator.placeholder(drawable); requestCreator.error(drawable); } requestCreator.into(imageView); }
/** * 处理ScaleType * * @param uri * @param view * @return */ private static RequestCreator processScaleType(String uri, ImageView view) { if (view.getScaleType() == ImageView.ScaleType.CENTER) { view.setScaleType(ImageView.ScaleType.CENTER); return initWithResize(uri, view); } else if (view.getScaleType() == ImageView.ScaleType.CENTER_INSIDE) { return initWithResize(uri, view).centerInside(); } else if (view.getScaleType() == ImageView.ScaleType.CENTER_CROP) { return initWithResize(uri, view).centerCrop(); } else if (view.getScaleType() == ImageView.ScaleType.FIT_CENTER) { view.setScaleType(ImageView.ScaleType.FIT_CENTER); return init(uri, view); } else if (view.getScaleType() == ImageView.ScaleType.FIT_START) { view.setScaleType(ImageView.ScaleType.FIT_START); return init(uri, view); } else if (view.getScaleType() == ImageView.ScaleType.FIT_END) { view.setScaleType(ImageView.ScaleType.FIT_END); return init(uri, view); } else { return init(uri, view).fit(); } }
private @Nullable Bitmap fetchBitmap(final @Nullable String url, final boolean transformIntoCircle) { if (url == null) { return null; } try { RequestCreator requestCreator = Picasso.with(this.context).load(url).transform(new CropSquareTransformation()); if (transformIntoCircle) { requestCreator = requestCreator.transform(new CircleTransformation()); } return requestCreator.get(); } catch (IOException e) { Timber.e("Failed to load large icon: %s", e); return null; } }
@Override public void bind(@NonNull final RepoEntity poRepo) { mTextView.setText(poRepo.url); final RequestCreator loRequest = mPicasso.load(poRepo.avatarUrl); if (loRequest != null) { loRequest .placeholder(R.drawable.git_icon) .error(R.drawable.git_icon) .into(mImageViewAvatar); } setOnClickListener((final View poView) -> notifyItemAction(ROW_PRESSED) ); }
private void loadWallpaperFromSD() { String path = ThemeManager.getWallpaperPath(this); ImageView ivWallpaper = (ImageView) findViewById(R.id.ivWallpaper); if (!TextUtils.isEmpty(path)) { int blurRadius = PrefManager.getInt(SettingsFragment.KEY_BLUR_RADIUS); boolean applyBlur = PrefManager.getBoolean(SettingsFragment.KEY_BLUR_WALLPAPER); ivWallpaper.setVisibility(View.VISIBLE); final RequestCreator creator = Picasso.with(this) .load(new File(path)); if (applyBlur) { creator.transform(new AndroidUtils.PicassoBlurTransform(blurRadius)); } creator.into(ivWallpaper); } else { ivWallpaper.setVisibility(View.GONE); } }
private void loadWallpaper() { String wallpaperPath = ThemeManager.getWallpaperPath(this); if (TextUtils.isEmpty(wallpaperPath)) { return; } ImageView ivWallpaper = (ImageView) findViewById(R.id.ivWallpaper); ivWallpaper.setVisibility(View.VISIBLE); boolean applyBlur = PrefManager.getBoolean(SettingsFragment.KEY_BLUR_WALLPAPER); RequestCreator creator = Picasso.with(this) .load(new File(wallpaperPath)); if (applyBlur) { creator.transform(new AndroidUtils.PicassoBlurTransform(PrefManager.getInt(SettingsFragment.KEY_BLUR_RADIUS))); } creator.into(ivWallpaper); }
@Test public void loadDescription() { String path = "http://"; AppCompatActivity activity = Mockito.mock(AppCompatActivity.class); Character character = Mockito.mock(Character.class); Thumbnail thumbnail = Mockito.mock(Thumbnail.class); TextView textView = Mockito.mock(TextView.class); Picasso picasso = Mockito.mock(Picasso.class); Mockito.when(mResources.getPicasso()).thenReturn(picasso); RequestCreator requestCreator = Mockito.mock(RequestCreator.class); Mockito.when(picasso.load(Mockito.any(Integer.class))).thenReturn(requestCreator); Mockito.when(picasso.load(path)).thenReturn(requestCreator); Mockito.when(requestCreator.fit()).thenReturn(requestCreator); Mockito.when(requestCreator.centerCrop()).thenReturn(requestCreator); Mockito.when(character.getThumbnail()).thenReturn(thumbnail); Mockito.when(thumbnail.getUrl()).thenReturn(path); Mockito.when(activity.findViewById(R.id.cover)).thenReturn(Mockito.mock(ImageView.class)); Mockito.when(activity.findViewById(R.id.name)).thenReturn(textView); Mockito.when(activity.findViewById(R.id.character_description)).thenReturn(textView); service.loadDescription(character, activity); Mockito.verify(picasso).load(path); Mockito.verify(activity).findViewById(R.id.cover); Mockito.verify(activity).findViewById(R.id.name); Mockito.verify(activity).findViewById(R.id.character_description); }
@Override public void bind(@NonNull final RepoEntity poRepo) { mTextView.setText(poRepo.getUrl()); final RequestCreator loRequest = mPicasso.load(poRepo.getAvatarUrl()); if (loRequest != null) { loRequest .placeholder(R.drawable.git_icon) .error(R.drawable.git_icon) .into(mImageViewAvatar); } setOnClickListener((final View poView) -> notifyItemAction(ROW_PRESSED) ); }
@Override public void load(Context context,ImageView targetView, Object res) { if (res == null) { return; } Picasso picasso = Picasso.with(context); RequestCreator requestCreator = null; if (res instanceof String) { requestCreator = picasso.load((String) res); } else if (res instanceof File) { requestCreator = picasso.load((File) res); } else if (res instanceof Integer) { requestCreator = picasso.load((Integer) res); } requestCreator .fit() .tag(context) .into(targetView); }
@Override public void display(Context context, String path, ImageView imageView, Drawable defaultDrawable, Bitmap.Config config, boolean resize, int width, int height) { RequestCreator creator = Picasso.with(context) .load(new File(path)) .placeholder(defaultDrawable) .error(defaultDrawable) // .rotate(rotate) .networkPolicy(NetworkPolicy.NO_STORE) .config(config) .tag(context); if (resize) { creator = creator.resize(width, height); } creator.into(imageView); }
@Override public void displayCenterCrop(Context context, String path, ImageView imageView, Drawable defaultDrawable, Bitmap.Config config, boolean resize, int width, int height) { RequestCreator creator = Picasso.with(context) .load(new File(path)) .placeholder(defaultDrawable) .error(defaultDrawable) // .rotate(rotate) .networkPolicy(NetworkPolicy.NO_STORE) .config(config) .tag(context); if (resize) { creator = creator.resize(width, height); } creator.centerCrop(); creator.into(imageView); }
@Override public void onViewCreated(View view, Bundle savedInstanceState) { boolean fitXY = getArguments().getBoolean(FIT_XY); if (getActivity() instanceof HelloActivity) { if (!((HelloActivity) getActivity()).isNetworkAvailable()) { if (fitXY) { Toast.makeText(getActivity(), R.string.network_unavailable, Toast.LENGTH_SHORT).show(); mFantasy.setImageResource(R.drawable.default_fantasy); return; // 没有网络,直接结束第一张fantasy } } } RequestCreator creator = Picasso.with(getActivity()).load(mUrl); if (fitXY) { creator.placeholder(R.drawable.default_fantasy); } creator.error(R.drawable.error) .into(target); }
@Override public void initializeView(NewsItem newsItem) { if (!isEmpty(newsItem.getAbstract())) { tvDescription.setText(newsItem.getAbstract()); } if (!isEmpty(newsItem.getTitle())) { tvTitle.setText(newsItem.getTitle()); } Picasso picasso = Picasso.with(this); RequestCreator requestCreator; if (!isEmpty(presenter.getMainImageURL())) { requestCreator = picasso.load(presenter.getMainImageURL()); } else { requestCreator = picasso.load(R.drawable.news); } requestCreator.placeholder(R.drawable.news).into(ivMainImage); }
public void displayImage(ImageView view, String url) { if(url == null) { view.setImageResource(R.mipmap.ic_launcher); return; } else if (TextUtils.isEmpty(url)) {//空图片显示 view.setImageResource(R.mipmap.ic_launcher); return; } RequestCreator creator = imageLoader .load(url) .placeholder(R.mipmap.ic_launcher) .error(R.mipmap.ic_launcher) .config(Bitmap.Config.RGB_565);//不透明的图片使用减少内存 if (view.getWidth() == 0 && view.getHeight() == 0){ } else { creator.centerCrop() .resize(view.getWidth(), view.getHeight()); } creator.into(view); }
@Override public Drawable getDrawable(String source) { final URLDrawable urlDrawable = new URLDrawable(); ImageTarget target = new ImageTarget(urlDrawable); addTarget(target); ImageHolder holder = mImages.get(source); RequestCreator load = Picasso.with(getContext()) .load(source); if (mImageFixListener != null && holder != null) { mImageFixListener.onFix(holder); if (holder.width != -1 && holder.height != -1) { load.resize(holder.width, holder.height); } if (holder.scaleType == ImageHolder.CENTER_CROP) { load.centerCrop(); } else if (holder.scaleType == ImageHolder.CENTER_INSIDE) { load.centerInside(); } } load.placeholder(placeHolder) .error(errorImage).into(target); return urlDrawable; }
@Override public void addImageToContent(String link) { final ImageView view = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.article_content_img_item, mContentLayout, false); mContentLayout.addView(view); view.setTag(link); final ImageListener l = new ImageListener(view, link); view.setOnClickListener(l); RequestCreator r = Picasso.with(getActivity()).load(link).placeholder(R.drawable.default_content_image_loading); if (PreferenceHelper.getInstance().inSafeDataMode()) { l.setLoadCacheForFirst(true); r.networkPolicy(NetworkPolicy.OFFLINE); } r.into(view, l); }
protected void swapImage() { if (this.urls != null && this.urls.size() > 0) { super.swapImage(); if (activeImageView != null) { activeUrl++; if (activeUrl >= this.urls.size()) activeUrl = 0; String newUrl = this.urls.get(activeUrl); if (newUrl != null) { RequestCreator rq = Picasso.with(getContext()).load(newUrl); if (blur) rq = rq.transform(new BlurTransformation()); rq.into(activeImageView); } } } }
@Override public void load(@Nullable Object model, @NonNull ImageView imageView) { final RequestCreator requestCreator; if (model instanceof Uri || model == null) { requestCreator = picasso.load((Uri) model); } else if (model instanceof String) { requestCreator = picasso.load((String) model); } else if (model instanceof File) { requestCreator = picasso.load((File) model); } else if (model instanceof Integer) { requestCreator = picasso.load((Integer) model); } else { throw new IllegalArgumentException("Unsupported model " + model); } requestCreator .skipMemoryCache() .transform(transformation) .into(imageView); }
public static void displayNoScaling(ImageView imageView, String uri, boolean fadeIn, int stubImage, ImageLoaderListener listener) { if (uri == null || uri.length() == 0) uri = FAKE_URI; Picasso picasso = Picasso.with(imageView.getContext()); RequestCreator requestCreator = picasso.load(uri); if (stubImage != 0) { requestCreator.placeholder(stubImage); requestCreator.error(stubImage); } if (!(fadeIn && FADE_ENABLED)) requestCreator.noFade(); requestCreator.into(imageView, listener); }
private void updateBackground(String url) { SharedPreferences sharedPrefs = PreferenceManager .getDefaultSharedPreferences(getActivity().getApplicationContext()); RequestCreator requestCreator = Picasso.with(getActivity()) .load(url) .placeholder(R.drawable.placeholder) .resize(mMetrics.widthPixels, mMetrics.heightPixels) .centerCrop() .skipMemoryCache(); switch(Enums.BlurState.valueOf(sharedPrefs.getString(Constants.BACKGROUND_BLUR, ""))) { case ON: requestCreator = requestCreator.transform(mBlurTransformation); break; } requestCreator.into(mBackgroundTarget); }
private void loadSimpleView() { RequestCreator requestCreator = Picasso.with(getActivity()).load(mImageEntry.getSrc()); switch (mImageScale) { case DOUBLE: Display display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x * 2; int height = size.y * 2; requestCreator.resize(width, height); break; case FIT_TO_SCREEN: requestCreator.fit().centerInside(); break; } requestCreator.into(mImageView, this); }
public static RequestCreator buildPicasso (Context context, String url) { return Picasso.with(context) .load(url) .placeholder(R.mipmap.ic_avatar) .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) .error(R.mipmap.ic_avatar); }
@Nullable public static Bitmap fromCache (Context context, String url) { RequestCreator creator = buildPicasso(context, url) .networkPolicy(NetworkPolicy.OFFLINE, NetworkPolicy.OFFLINE) .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE); try { return creator.get(); } catch (IOException e) { e.printStackTrace(); return null; } }
@Override public ILoaderProxy loadImage(View view, int drawable) { if (view instanceof ImageView) { ImageView imageView = (ImageView) view; RequestCreator requestCreator = getPicasso().load(drawable); loadOptions(requestCreator).into(imageView); } return obtain(); }
@Override public ILoaderProxy loadImage(View view, File file) { if (view instanceof ImageView) { ImageView imageView = (ImageView) view; //判断缓存中是否已经缓存过该图片,有则直接拿Bitmap,没有则直接调用Glide加载并缓存Bitmap Bitmap bitmap = LruCacheUtils.getInstance().getBitmapFromMemCache(file); if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { RequestCreator requestCreator = getPicasso().load(file); loadOptions(requestCreator).into(imageView); } } return obtain(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo); ButterKnife.inject(this); final String picName = getIntent().getStringExtra("picName"); RequestCreator requestCreator = Picasso.with(this) .load(IMAGE_HOST + picName + ".jpg"); requestCreator.into(mImageView); requestCreator.fetch(new Callback() { @Override public void onSuccess() { // 一开始为wrap_content使其居中,成功加载后match_parent使其放缩效果更佳 mImageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); } @Override public void onError() { Toast.makeText(PhotoActivity.this, "加载图片失败", Toast.LENGTH_SHORT).show(); } }); mAttacher = new PhotoViewAttacher(mImageView); mAttacher.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this); builder.setItems(new String[]{"保存"}, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { savePhoto(picName); } }); builder.create().show(); return false; } }); }