private void setPauseImageFromUrl() { Picasso.with(this).load(pauseADInfo.getAd().get(0).getMaterial()) .skipMemoryCache().transform(new Transformation() { @Override public Bitmap transform(Bitmap arg0) { pauseHeight = arg0.getHeight(); pauseWidth = arg0.getWidth(); PlayerUtil.resizeAdView(ADMediaPlayActivity.this, wm, ivPauseView, pauseWidth, pauseHeight); return arg0; } @Override public String key() { return "pause"; } }).into(ivPauseView); }
private void setFrontImageFromUrl() { Picasso.with(this).load(frontADInfo.getAd().get(0).getMaterial()) .skipMemoryCache().transform(new Transformation() { @Override public Bitmap transform(Bitmap arg0) { frontHeight = arg0.getHeight(); frontWidth = arg0.getWidth(); PlayerUtil.resizeAdView(ADMediaPlayActivity.this, wm, ivAd, frontWidth, frontHeight); return arg0; } @Override public String key() { return "front"; } }).into(ivAd); }
public static Bitmap loadRoundedImage(Context context, String url, @DrawableRes int ifErrorOrEmpty) { final Context app = context.getApplicationContext(); Transformation transformation = CurrentTheme.createTransformationForAvatar(app); int size = (int) Utils.dpToPx(64, app); if(nonEmpty(url)){ try { return PicassoInstance.with() .load(url) .resize(size, size) .centerCrop() .transform(transformation) .config(Bitmap.Config.RGB_565) .get(); } catch (IOException e) { return loadRoundedImageFromResources(app, ifErrorOrEmpty, transformation, size); } } else { return loadRoundedImageFromResources(app, ifErrorOrEmpty, transformation, size); } }
private void refreshHeader(User user) { if (!isAdded()) return; String avaUrl = user.getMaxSquareAvatar(); String fullName = user.getFullName(); Transformation transformation = new RoundTransformation(); if (nonNull(avaUrl) && !avaUrl.contains("camera")) { PicassoInstance.with() .load(avaUrl) .transform(transformation) .into(ivHeaderAvatar); } else { PicassoInstance.with() .load(user.getSex() == Sex.WOMAN ? R.drawable.ic_sex_woman : R.drawable.ic_sex_man) .transform(transformation) .into(ivHeaderAvatar); } tvUserName.setText(fullName); if (tvDomain != null) { String domailText = "@" + user.getDomain(); tvDomain.setText(domailText); } }
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); }
private static Bitmap createBitmap(Context mContext, String url) throws IOException { int appIconSize = getLauncherIconSize(mContext); Bitmap bm; Transformation transformation = new RoundTransformation(); if (isEmpty(url)) { bm = PicassoInstance.with() .load(R.drawable.ic_avatar_unknown) .transform(transformation) .resize(appIconSize, appIconSize) .get(); } else { bm = PicassoInstance.with() .load(url) .transform(transformation) .resize(appIconSize, appIconSize) .get(); } return bm; }
public Transformation build() { return new Transformation() { public Bitmap transform(Bitmap source) { Bitmap transformed = RoundedDrawable.fromBitmap(source).setScaleType (RoundedTransformationBuilder.this.mScaleType).setCornerRadius (RoundedTransformationBuilder.this.mCornerRadii[0], RoundedTransformationBuilder.this.mCornerRadii[1], RoundedTransformationBuilder.this.mCornerRadii[2], RoundedTransformationBuilder.this.mCornerRadii[3]).setBorderWidth (RoundedTransformationBuilder.this.mBorderWidth).setBorderColor (RoundedTransformationBuilder.this.mBorderColor).setOval (RoundedTransformationBuilder.this.mOval).toBitmap(); if (!source.equals(transformed)) { source.recycle(); } return transformed; } public String key() { return "r:" + Arrays.toString(RoundedTransformationBuilder.this.mCornerRadii) + "b:" + RoundedTransformationBuilder.this.mBorderWidth + "c:" + RoundedTransformationBuilder.this.mBorderColor + "o:" + RoundedTransformationBuilder.this.mOval; } }; }
/** * Creates a {@link Transformation} for use with picasso. * * @return the {@link Transformation} */ public Transformation build() { return new Transformation() { @Override public Bitmap transform(Bitmap source) { Bitmap transformed = RoundedDrawable.fromBitmap(source) .setScaleType(mScaleType) .setCornerRadius(mCornerRadii[0], mCornerRadii[1], mCornerRadii[2], mCornerRadii[3]) .setBorderWidth(mBorderWidth) .setBorderColor(mBorderColor) .setOval(mOval) .toBitmap(); if (!source.equals(transformed)) { source.recycle(); } return transformed; } @Override public String key() { return "r:" + Arrays.toString(mCornerRadii) + "b:" + mBorderWidth + "c:" + mBorderColor + "o:" + mOval; } }; }
private void loadLoadingUrl(@NonNull final String url, @NonNull final String loadingUrl, final boolean loadingBlur) { final List<Transformation> transformations = new ArrayList<>(); if (loadingBlur) { transformations.add(new BlurTransformation(getContext())); } Picasso.with(getContext()).load(loadingUrl).transform(transformations).into(new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { loadUrl(url, bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }); }
private Transformation getTransformation(final String t) { return new Transformation() { @Override public Bitmap transform(Bitmap b) { Palette.from(b).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(final Palette palette) { int defaultColor = rootView.getResources().getColor (android.R.color.white); int titleColor = palette.getLightVibrantColor (defaultColor); CollapsingToolbarLayout collapsingToolbar = get(R.id .collapsing_toolbar); collapsingToolbar.setExpandedTitleColor(titleColor); } }); return b; } @Override public String key() { return t; } }; }
private void loadBackgroundImage() { if (headerViewImageReference.size() == 0 || headerViewImageReference.get(currentPageIndex) == null || headerViewImageReference.get(currentPageIndex).size() == 0) { loadFallBackBackground(); return; } if (headerViewImageReference.get(currentPageIndex).size() <= currentImageIndex + 1) { currentImageIndex = -1; } currentImageIndex++; final Transformation tintTransformation = new ColorTintingGrayScaleTransformation(getColorByPosition(currentPageIndex)); picasso.load(headerViewImageReference.get(currentPageIndex).get(currentImageIndex)) .transform(tintTransformation) .into(panningImage); transitionsCount = 0; }
@Override public Transformation blurTransformation(final String fileName) { return new Transformation() { private Bitmap blurredImage; @Override public Bitmap transform(Bitmap source) { return getBlurredImage(source, fileName, 20); } @Override public String key() { return fileName; } }; }
public Transformation build() { return new Transformation() { @Override public Bitmap transform(Bitmap source) { Bitmap transformed = RoundedDrawable.fromBitmap(source) .setScaleType(mScaleType) .setCornerRadius(mCornerRadius) .setBorderWidth(mBorderWidth) .setBorderColor(mBorderColor) .setOval(mOval) .toBitmap(); if (!source.equals(transformed)) { source.recycle(); } return transformed; } @Override public String key() { return "r:" + mCornerRadius + "b:" + mBorderWidth + "c:" + mBorderColor + "o:" + mOval; } }; }
private static Transformation getTransformation(Context context) { if (PICASSO_CIRCLE_TRANSFORMATION == null) { final int color; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { color = context.getResources().getColor(R.color.black); } else { color = context.getColor(R.color.black); } PICASSO_CIRCLE_TRANSFORMATION = new RoundedTransformationBuilder() .borderColor(color) .borderWidthDp(2) .scaleType(ImageView.ScaleType.CENTER_CROP) .oval(true) .build(); } return PICASSO_CIRCLE_TRANSFORMATION; }
private void drawVerseImage() { List<Transformation> transformations = new ArrayList<>(); transformations.add(new BlurTransformation(getActivity(), 25, 4)); Picasso.with(getActivity()).load(R.drawable.at_the_beach) .fit() .centerCrop() .into(verseImage); Picasso.with(getActivity()).load(R.drawable.at_the_beach) .fit() .centerCrop() .transform(transformations) .into(verseImageReflection); }
public static void setDynamicImage(Context context, ImageView target) { File currentBackgroundImage = new File(context.getCacheDir(), Constants.CURRENT_BACKGROUND_IMAGE); List<Transformation> transformations = new ArrayList<>(); transformations.add(new BlurTransformation(context, 25, 4)); if (currentBackgroundImage.exists()) { Picasso.with(context).load(currentBackgroundImage) .placeholder(R.drawable.placeholder_rectangle_scene) .fit() .centerCrop() .transform(transformations) .into(target); } else { Picasso.with(context).load(R.drawable.at_the_beach) .placeholder(R.drawable.placeholder_rectangle_scene) .fit() .centerCrop() .transform(transformations) .into(target); } }
private static Bitmap loadRoundedImageFromResources(Context context, @DrawableRes int res, Transformation transformation, int size) { try { return PicassoInstance.with() .load(res) .resize(size, size) .transform(transformation) .centerCrop() .get(); } catch (IOException e){ e.printStackTrace(); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res); return ImageHelper.getRoundedBitmap(bitmap); } }
public static Transformation createTransformationForAvatar(Context context) { int style = Settings.get() .ui() .getAvatarStyle(); switch (style) { case AvatarStyle.OVAL: return new MaskTransformation(context, R.drawable.avatar_mask); case AvatarStyle.CIRCLE: return new RoundTransformation(); default: return new RoundTransformation(); } }
@Override public ImageLoaderBuilder transform(Object... bitmapTransformations) { bitmapTransformation = new ArrayList<>(); for (int i = 0; i < bitmapTransformations.length; i++) { if (bitmapTransformations[i] instanceof Transformation) { bitmapTransformation.add((Transformation) bitmapTransformations[i]); } } return this; }
public static RequestCreator getPicassoCorned(RequestCreator request) { final int radius = 5; final int margin = 5; final Transformation transformation = new RoundedCornersTransformation(radius, margin); request.transform(transformation); return request; }
/** * Get circle transformation * @param borderWidth in dp * @param borderColor color of the border */ public static Transformation getCircleTransformation(int borderWidth, int borderColor) { return new RoundedTransformationBuilder() .oval(true) .borderWidthDp(borderWidth) .borderColor(borderColor) .build(); }
private void doUser() { // 读取数据,得到内存中的User对象 User user = ((BaseActivity) this.getActivity()).readUser(); // 一方面,显示用户名 textView11.setText(user.UF_ACC); // 另一方面,加载显示用户头像 Picasso.with(getActivity()).load(user.UF_AVATAR_URL).transform(new Transformation() { @Override public Bitmap transform(Bitmap source) { // 对Bitmap进行压缩处理 Bitmap zoom = BitmapUtils.zoom(source, UIUtils.dp2px(62), UIUtils.dp2px(62)); // 对Bitmap进行圆形处理 Bitmap circleBitmap = BitmapUtils.circleBitmap(zoom); source.recycle(); // 回收,否则会出现内存泄漏 return circleBitmap; } @Override public String key() { return ""; // 此方法不能返回null否则报异常 } }).into(imageView1); // 如果在本地发现了用户设置了手势密码,则在此需要验证 boolean isOpen = sp.getBoolean("isOpen", false); if (isOpen) { ((BaseActivity) this.getActivity()).goToActivity(GestureVerifyActivity.class, null); } }
@Override public Transformation roundedTransformation(final String filename, final float radius) { return new Transformation() { @Override public Bitmap transform(final Bitmap source) { final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); canvas.drawRoundRect(new RectF(0.0f, 0.0f, source.getWidth(), source.getHeight()), radius, radius, paint); if (source != output) { source.recycle(); } return output; } @Override public String key() { return "rounded transf " + filename; } }; }
@Override public Transformation circleTransformation(final String filename) { return new Transformation() { @Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); paint.setShader(shader); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); squaredBitmap.recycle(); return bitmap; } @Override public String key() { return "circle transf " + filename; } }; }
public static Transformation getRoundedWithBordersTransformation(int borderColor, int borderWidthDP, int cornerRadiusDP) { return new RoundedTransformationBuilder() .borderColor(borderColor) .borderWidthDp(borderWidthDP) .cornerRadiusDp(cornerRadiusDP) .oval(false) .build(); }
private void loadImageWithMultipleTransformation() { List<Transformation> transformations = new ArrayList<>(); transformations.add(new GrayscaleTransformation(Picasso.with(context))); transformations.add(new BlurTransformation(context)); Picasso .with(context) .load(UsageExampleListView.eatFoodyImages[0]) .transform(transformations) .into(imageViewTransformationsMultiple); }
protected void addTransformation(View view, Transformation transformation) { List<Transformation> list = transformations.get(view); if (list == null) { list = new ArrayList<>(); transformations.put(view, list); } list.add(transformation); }
public void loadBackground(SongBean bean) { if (bean == null ) { backgroundImage.setImageBitmap(null); return; } final Uri image = Uri.parse(MediaLibrary.getStaticInstance(getApplicationContext()) .getCoverUriByAlbumId(bean.getAlbumId())); Picasso.with(this).load(image).resize(windowSize.x, windowSize.y) .centerCrop() .transform(new Transformation() { @Override public Bitmap transform(Bitmap source) { BlurFactor factor = new BlurFactor(); factor.radius = 25; factor.sampling = 4; factor.width = windowSize.x; factor.height = windowSize.y; factor.color = ColorUtils.setAlphaComponent( ThemeManager.getInstance(getApplicationContext()) .loadCurrentTheme().getBackgroundColor(), 20); Bitmap ret = Blur.of(getApplicationContext(), source, factor); source.recycle(); return ret; } @Override public String key() { return image.toString() + "/blured"; } }) .config(Bitmap.Config.RGB_565).into(backgroundImage); }
@UiThread public final void setTransforms(@Nullable final List<? extends Transformation> transformations) { mTransforms.clear(); if (transformations != null) { mTransforms.addAll(transformations); } triggerUpdate(); }
@Override public void loadImage(String url,final ImageView imageView) { Picasso.with(imageView.getContext()).load(url).transform(new Transformation() { @Override public Bitmap transform(Bitmap source) { Bitmap combinedBitmap; combinedBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight() / 3 + source.getHeight(), source.getConfig()); Canvas combinedCanvas = new Canvas(combinedBitmap); combinedCanvas.drawBitmap(source, 0f, 0f, null); Matrix matrix = new Matrix(); matrix.postRotate(180); matrix.preScale(-1, 1); matrix.postTranslate(0, source.getHeight() * 2); BlurTransformation blurTransformation = new BlurTransformation(imageView.getContext(), 15, 1); Bitmap bottom = blurTransformation.transform(source); combinedCanvas.setMatrix(matrix); combinedCanvas.drawBitmap(bottom, 0f, 0f, null); return combinedBitmap; } @Override public String key() { return ImageLoader.class.getName() + ".Transformation"; } }).error(android.R.drawable.sym_contact_card).placeholder(android.R.drawable.sym_contact_card). into(imageView); }
private Transformation getTransform(Context context) { if (mTransform == null) { float radius = context.getResources().getDimension(com.layer.atlas.R.dimen.atlas_message_item_cell_radius); mTransform = new RoundedTransform(radius); } return mTransform; }
public void loadTransform(String path, Target target, Transformation transformer) { picasso.load(path) .placeholder(onLoadDrawable) .error(onErrorDrawable) .transform(transformer) .into(target); }
/** * Uses the configuration previously applied using this ImageLoader builder to download a * resource asynchronously and notify the result to the listener. */ private void loadImage() { List<Transformation> transformations = getTransformations(); boolean hasUrl = url != null; boolean hasResourceId = resourceId != null; boolean hasPlaceholder = placeholderId != null; ListenerTarget listenerTarget = getLinearTarget(listener); if (hasUrl) { RequestCreator bitmapRequest = Picasso.with(context).load(url).tag(PICASSO_IMAGE_LOADER_TAG); applyPlaceholder(bitmapRequest).resize(size, size) .transform(transformations) .into(listenerTarget); } else if (hasResourceId || hasPlaceholder) { Resources resources = context.getResources(); Drawable placeholder = null; Drawable drawable = null; if (hasPlaceholder) { placeholder = resources.getDrawable(placeholderId); listenerTarget.onPrepareLoad(placeholder); } if (hasResourceId) { drawable = resources.getDrawable(resourceId); listenerTarget.onDrawableLoad(drawable); } } else { throw new IllegalArgumentException( "Review your request, you are trying to load an image without a url or a resource id."); } }
/** * Lazy instantiation of the list of transformations used during the image download. This method * returns a List<Transformation> because Picasso doesn't support a null instance as * transformation. */ private List<Transformation> getTransformations() { if (transformations == null) { transformations = new LinkedList<Transformation>(); if (useCircularTransformation) { transformations.add(new CircleTransformation()); } } return transformations; }
@Override public void onBindViewHolder(final ViewHolder holder, int position) { final StatusModel model = mList.get(position); holder.detailUrl = model.detail; holder.author.setText(model.author); if (!TextUtils.isEmpty(model.avatar)) { Picasso.with(mContext).load(model.avatar).noFade().placeholder(android.R.color.darker_gray).into(holder.avatar); holder.avatar.setVisibility(View.VISIBLE); } holder.cover.setOriginalSize(model.width, model.height); Picasso.with(mContext).load(model.cover).config(Bitmap.Config.RGB_565).placeholder(android.R.color.darker_gray).transform(new Transformation() { @Override public Bitmap transform(Bitmap source) { if (!(model.width > 0 && model.height > 0)) { model.width = source.getWidth(); model.height = source.getHeight(); holder.cover.setOriginalSize(source.getWidth(), source.getHeight()); } return source; } @Override public String key() { return "setCoverViewSize"; } }).into(holder.cover); }