下面列出了com.bumptech.glide.load.resource.bitmap.CenterCrop#com.bumptech.glide.load.resource.drawable.GlideDrawable 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setImageViewUri(MasterSecret masterSecret, Uri uri) {
subsamplingImageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
Glide.with(getContext())
.load(new DecryptableUri(masterSecret, uri))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.dontTransform()
.dontAnimate()
.into(new GlideDrawableImageViewTarget(imageView) {
@Override protected void setResource(GlideDrawable resource) {
super.setResource(resource);
imageViewAttacher.update();
}
});
}
public void showCirImage(String url, final ImageView image) {
if (url == null || url.isEmpty() || "".equals(url)) {
image.setImageResource(R.mipmap.defeat_person_img);
return;
}
Glide.with(getApplicationContext())
.load(url)
.error(R.mipmap.ic_launcher_round)
.into(new SimpleTarget<GlideDrawable>() { // 加上这段代码 可以解决
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable> glideAnimation) {
image.setImageDrawable(resource); //显示图片
}
});
}
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView.setAdapter(new SimpleUrlAdapter(Glide.with(this), Arrays.asList(
// original url, already url-encoded
URL_BASE + "s%C3%BCt%C3%A9s+1146.jpg",
// parse url-encoded url and toString encodes it again
Uri.parse(URL_BASE + "s%C3%BCt%C3%A9s+1146.jpg").toString(),
// raw url, nothing is escaped "sütés"
Uri.decode(URL_BASE + "s%C3%BCt%C3%A9s+1146.jpg"),
// illegally decoded url, utf-8 is not parsed properly
URL_BASE + "sütés+1146.jpg"
)) {
@Override protected void load(Context context, RequestManager glide, String url, ImageView imageView) {
glide.load(url).listener(new LoggingListener<String, GlideDrawable>()).into(imageView);
}
});
}
protected void updateBackground(String uri) {
int width = mMetrics.widthPixels;
int height = mMetrics.heightPixels;
Glide.with(getActivity())
.load(uri)
.centerCrop()
.error(mDefaultBackground)
.into(new SimpleTarget<GlideDrawable>(width, height) {
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable>
glideAnimation) {
mBackgroundManager.setDrawable(resource);
}
});
mBackgroundTimer.cancel();
}
/**
* 加载gif
*/
private void loadGif() {
Glide.with(ImageBrowseActivity.this)
.load(mImageUrl)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new GlideDrawableImageViewTarget(pvPic) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<?
super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
//在这里添加一些图片加载完成的操作
pbPicBrowse.setVisibility(View.GONE);
}
});
}
/**
* 显示加载进度
*
* @param path 图片地址
* @param mImageView 图片控件
* @param loadView 加载view
*/
public static void loadImageWithProgress(String path, final ImageView mImageView, final View loadView, int errorRes) {
Glide.with(mImageView.getContext()).load(path).error(errorRes).into(new GlideDrawableImageViewTarget(mImageView) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
loadView.setVisibility(View.GONE);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
loadView.setVisibility(View.GONE);
}
});
}
@NonNull
private ImageView getGifLoadedUsingGlide() {
ImageView animatedImageView = new ImageView(getActivity());
animatedImageView.setMaxHeight(900);
Glide.with(getActivity())
.load(R.drawable.animated_image)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(Glide.with(getActivity())
.load(R.drawable.animated_image)
.asBitmap()
.transcode(new BitmapToGlideDrawableTranscoder(getActivity()), GlideDrawable.class)
.diskCacheStrategy(DiskCacheStrategy.ALL)
)
.into(animatedImageView);
return animatedImageView;
}
@Override
public void showImagePreview(@NonNull String imageUrl) {
checkState(!TextUtils.isEmpty(imageUrl), "imageUrl cannot be null or empty!");
mImageThumbnail.setVisibility(View.VISIBLE);
// The image is loaded in a different thread so in order to UI-test this, an idling resource
// is used to specify when the app is idle.
EspressoIdlingResource.increment(); // App is busy until further notice.
// This app uses Glide for image loading
Glide.with(this)
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.into(new GlideDrawableImageViewTarget(mImageThumbnail) {
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
EspressoIdlingResource.decrement(); // Set app as idle.
}
});
}
@Override protected void load(final Context context) throws Exception {
String url = "http://www.kizoa.com/img/e8nZC.gif";
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.github_1261_nine_to_five);
if (drawable instanceof Animatable) {
((Animatable)drawable).stop();
((Animatable)drawable).start();
}
Glide
.with(this)
.load(url)
.placeholder(drawable)
.crossFade(3000)
.skipMemoryCache(true) // make sure the image is reloaded so the placeholder has a chance
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.bitmapTransform(DelayTransformation.<Bitmap>create(2000)) // debug lengthen decode to see placeholder
.listener(new LoggingListener<String, GlideDrawable>())
.into(new LoggingTarget<>(new GlideDrawableImageViewTarget(imageView)))
;
}
protected void updateBackground(String uri) {
int width = mMetrics.widthPixels;
int height = mMetrics.heightPixels;
Glide.with(getActivity())
.load(uri)
.centerCrop()
.error(mDefaultBackground)
.into(new SimpleTarget<GlideDrawable>(width, height) {
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable>
glideAnimation) {
mBackgroundManager.setDrawable(resource);
}
});
mBackgroundTimer.cancel();
}
@Nullable
public File saveImage(){
Drawable drawable = getDrawable();
Bitmap bitmap = null;
if(drawable instanceof BitmapDrawable){
bitmap = ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof GlideDrawable){
bitmap = ((GlideBitmapDrawable)drawable.getCurrent()).getBitmap();
}
File output = new ImageSaver(getContext())
.setExternal(true)
.setDirectoryName(Configuration.DOWNLOAD_FOLDER)
.setFileName(getContext().getString(R.string.app_name) + "-" + System.currentTimeMillis() + ".jpg")
.save(bitmap);
if(output != null){
return output;
}
return null;
}
@Override
public boolean onException(Exception e,
String model,
Target<GlideDrawable> target,
boolean isFirstResource) {
maybeStartPostponedEnterTransition();
return true;
}
@Override
public Drawable mutate() {
if (!mutated && super.mutate() == this) {
wrapped = (GlideDrawable) wrapped.mutate();
state = new State(state);
mutated = true;
}
return this;
}
@Override
public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) {
if (!activityIsAlive()) {
return;
}
DrawableWrapper drawableWrapper = urlDrawableWeakReference.get();
if (drawableWrapper == null) {
return;
}
holder.setImageState(ImageHolder.ImageState.READY);
gifDrawableSoftReference = new SoftReference<>(resource);
Bitmap first = resource.getFirstFrame();
holder.setSize(first.getWidth(), first.getHeight());
drawableWrapper.setDrawable(resource);
if (rect != null) {
drawableWrapper.setBounds(rect);
} else {
if (!config.autoFix && config.imageFixCallback != null) {
config.imageFixCallback.onImageReady(holder, first.getWidth(), first.getHeight());
}
if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
int width = getRealWidth();
int height = (int) ((float) first.getHeight() * width / first.getWidth());
drawableWrapper.setBounds(0, 0, width, height);
} else {
drawableWrapper.setBounds(0, 0, holder.getWidth(), holder.getHeight());
}
if (holder.isAutoPlay()) {
resource.setCallback(this);
resource.start();
resource.setLoopCount(GlideDrawable.LOOP_FOREVER);
}
}
resetText();
loadDone();
}
protected void updateVideoImage(String uri) {
Glide.with(getActivity())
.load(uri)
.centerCrop()
.into(new SimpleTarget<GlideDrawable>(CARD_WIDTH, CARD_HEIGHT) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
mPlaybackControlsRow.setImageDrawable(resource);
mRowsAdapter.notifyArrayItemRangeChanged(0, mRowsAdapter.size());
}
});
}
@Override protected void load2(Context context, ImageView imageView) throws Exception {
Glide
.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.listener(new LoggingListener<String, GlideDrawable>("second"))
.dontAnimate()
.dontTransform()
// .transcoder(new GifBitmapWrapperDrawableTranscoder(new GlideBitmapDrawableTranscoder(context)) {
// @Override public Resource<GlideDrawable> transcode(Resource<GifBitmapWrapper> toTranscode) {
// try {
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// return super.transcode(toTranscode);
// }
// })
.override(360, 360)
.thumbnail(Glide
.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.listener(new LoggingListener<String, GlideDrawable>("second-thumb"))
.animate(android.R.anim.fade_in)
.override(72, 72)
)
.into(imageView)
;
}
/**
* {@inheritDoc}
*/
@Override
public DrawableRequestBuilder<ModelType> thumbnail(
GenericRequestBuilder<?, ?, ?, GlideDrawable> thumbnailRequest) {
super.thumbnail(thumbnailRequest);
return this;
}
@Override protected void load2(Context context, ImageView imageView) throws Exception {
Glide
.with(this)
.load("http://facebook.github.io/stetho/static/logo.png")
.listener(new LoggingListener<String, GlideDrawable>())
.into(imageView)
;
}
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
Log.w(TAG, "onException: ", e);
Log.d(TAG, "onException: " + model);
Log.d(TAG, "onException: " + target.getRequest().isRunning());
return false;
}
/**
* {@inheritDoc}
*/
@Override
public DrawableRequestBuilder<ModelType> listener(
RequestListener<? super ModelType, GlideDrawable> requestListener) {
super.listener(requestListener);
return this;
}
@Override protected void load(Context context) {
SyncLoadImageViewTarget target = Glide
.with(this)
.load("http://placehold.it/1600x900/ff0000/00ff00&text=image")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.glide_placeholder)
.listener(new LoggingListener<String, GlideDrawable>())
.into(new SyncLoadImageViewTarget(imageView));
Log.d("isLoaded", target.isLoaded() + "");
if (!target.isLoaded()) {
//Glide.clear(target); NOT CLEARING ANYMORE
}
}
private void loadImageViewTarget() {
viewTarget = new ViewTarget<FutureStudioView, GlideDrawable>( customView ) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
this.view.setImage( resource.getCurrent() );
}
};
Glide
.with( context.getApplicationContext() ) // safer!
.load( eatFoodyImages[2] )
.into( viewTarget );
}
public DrawableRequestBuilder<GlideDrawable> build() {
//noinspection unchecked
return createBaseRequest(requestManager, song, ignoreMediaStore)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.error(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION)
.signature(createSignature(song));
}
@Override protected void load(Context context) throws Exception {
Glide
.with(context)
.load(R.drawable.glide)
.diskCacheStrategy(DiskCacheStrategy.NONE) // necessary only because I'm loading an APK resource
.skipMemoryCache(true) // remove in production, this is just there so it's reproducible quickly
.placeholder(R.drawable.glide_placeholder)
// delay to see what's going on, in normal usage replace this with .fitCenter()
.transform(new FitCenter(context), new DelayBitmapTransformation(1000))
.animate(new PaddingAnimationFactory<>(new DrawableCrossFadeFactory<GlideDrawable>(2000)))
.into(imageView)
;
}
/**
* Create a small thumbnail sized image that has the same bounds as the
* media store thumbnail images.
* <p>
* If the Uri points at an animated gif, the gif will not play.
*/
public GenericRequestBuilder<Uri, ?, ?, GlideDrawable> loadMediaStoreThumb(Uri uri, Key key)
{
Size size = clampSize(MEDIASTORE_THUMB_SIZE, MAXIMUM_SMOOTH_PIXELS, getMaxImageDisplaySize());
return mTinyImageBuilder
.clone()
.load(uri)
.signature(key)
// This attempts to ensure we load the cached media store version.
.override(size.width(), size.height());
}
/**
* Create very tiny thumbnail request that should complete as fast
* as possible.
* <p>
* If the Uri points at an animated gif, the gif will not play.
*/
public GenericRequestBuilder<Uri, ?, ?, GlideDrawable> loadTinyThumb(Uri uri, Key key)
{
Size size = clampSize(TINY_THUMB_SIZE, MAXIMUM_SMOOTH_PIXELS, getMaxImageDisplaySize());
return mTinyImageBuilder
.clone()
.load(uri)
.signature(key)
.override(size.width(), size.height());
}
public DrawableRequestBuilder<GlideDrawable> build() {
//noinspection unchecked
return createBaseRequest(requestManager, song, ignoreMediaStore)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.error(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION)
.signature(createSignature(song));
}
public DrawableRequestBuilder<GlideDrawable> build() {
//noinspection unchecked
return createBaseRequest(requestManager, artist, noCustomImage)
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
.error(DEFAULT_ERROR_IMAGE)
.animate(DEFAULT_ANIMATION)
.priority(Priority.LOW)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.signature(createSignature(artist));
}
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable> glideAnimation) {
Drawable start = mTarget.get();
if (start != null) {
setDrawableWithFade(start, resource);
} else {
setDrawable(resource);
}
}
public void showImage(String url, final ImageView image) {
Glide.with(getApplicationContext()).load(url).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)
.into(new SimpleTarget<GlideDrawable>() { // 加上这段代码 可以解决
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable> glideAnimation) {
image.setImageDrawable(resource); //显示图片
}
});
}