Last active
July 3, 2016 08:36
-
-
Save wuapnjie/4e12f7cb9d82e685de734875fa45139e to your computer and use it in GitHub Desktop.
This file contains 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.playalot.play.util; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.net.Uri; | |
import android.support.annotation.DrawableRes; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.widget.ImageView; | |
import com.facebook.drawee.backends.pipeline.Fresco; | |
import com.facebook.drawee.backends.pipeline.PipelineDraweeController; | |
import com.facebook.drawee.view.SimpleDraweeView; | |
import com.facebook.imagepipeline.common.ResizeOptions; | |
import com.facebook.imagepipeline.request.ImageRequest; | |
import com.facebook.imagepipeline.request.ImageRequestBuilder; | |
import com.playalot.play.R; | |
import com.squareup.picasso.MemoryPolicy; | |
import com.squareup.picasso.Picasso; | |
import java.io.File; | |
/** | |
* Created by snowbean on 16-7-2. | |
*/ | |
public class PhotoUtil { | |
private static final String TAG = PhotoUtil.class.getSimpleName(); | |
private PhotoUtil() { | |
} | |
//Fresco | |
public static void display(SimpleDraweeView draweeView, String url) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
draweeView.setImageURI(url); | |
} | |
public static void display(SimpleDraweeView draweeView, File file) { | |
if (file == null) { | |
Log.e(TAG, "display: error the file is empty"); | |
return; | |
} | |
draweeView.setImageURI(Uri.fromFile(file)); | |
} | |
public static void display(SimpleDraweeView draweeView, Uri uri) { | |
if (uri == null) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
draweeView.setImageURI(uri); | |
} | |
public static void display(SimpleDraweeView draweeView, Uri uri, int width, int height) { | |
if (uri == null) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) | |
.setResizeOptions(new ResizeOptions(width, height)) | |
.setAutoRotateEnabled(true) | |
.setLocalThumbnailPreviewsEnabled(true) | |
.build(); | |
PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder() | |
.setOldController(draweeView.getController()) | |
.setImageRequest(request) | |
.build(); | |
draweeView.setController(controller); | |
} | |
public static void display(SimpleDraweeView draweeView, String url, int width, int height) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
Uri uri = Uri.parse(url); | |
display(draweeView, uri, width, height); | |
} | |
public static void prefetchPhoto(Context context, Uri uri, int width, int height) { | |
if (uri == null) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) | |
.setResizeOptions(new ResizeOptions(width, height)) | |
.setAutoRotateEnabled(true) | |
.setLocalThumbnailPreviewsEnabled(true) | |
.build(); | |
Fresco.getImagePipeline().prefetchToDiskCache(request, context); | |
} | |
public static void prefetchPhoto(Context context, String url, int width, int height) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
Uri uri = Uri.parse(url); | |
prefetchPhoto(context, uri, width, height); | |
} | |
//Picasso | |
public static void display(final String url, final ImageView imageView) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(R.color.light_gray_background_color) | |
.fit() | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
} | |
public static void display(final String url, final ImageView imageView, Object tag) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
if (tag != null) | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(R.color.light_gray_background_color) | |
.fit() | |
.tag(tag) | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
else | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(R.color.light_gray_background_color) | |
.fit() | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
} | |
public static void display(final String url, final ImageView imageView, @DrawableRes final int placeholder) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(placeholder) | |
.fit() | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
} | |
public static void display(final String url, final ImageView imageView, @DrawableRes final int placeholder, Object tag) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
if (tag != null) | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(placeholder) | |
.fit() | |
.tag(tag) | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
else | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(placeholder) | |
.fit() | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
} | |
public static void display(final File file, final ImageView imageView) { | |
if (file == null) { | |
Log.e(TAG, "display: error the file is null"); | |
return; | |
} | |
Picasso.with(imageView.getContext()) | |
.load(file) | |
.placeholder(R.color.light_gray_background_color) | |
.fit() | |
.config(Bitmap.Config.RGB_565) | |
.centerInside() | |
.into(imageView); | |
} | |
public static void displayBigPhoto(final String url, final ImageView imageView, @DrawableRes final int placeholder) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(placeholder) | |
.fit() | |
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) | |
.centerInside() | |
.into(imageView); | |
} | |
public static void displayBigPhoto(final String url, final ImageView imageView) { | |
if (TextUtils.isEmpty(url)) { | |
Log.e(TAG, "display: error the url is empty"); | |
return; | |
} | |
Picasso.with(imageView.getContext()) | |
.load(url) | |
.placeholder(R.color.light_gray_background_color) | |
.fit() | |
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) | |
.centerInside() | |
.into(imageView); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment