下面列出了怎么用com.bumptech.glide.request.transition.Transition的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) {
super.onResourceReady(resource, transition);
BadgedFourThreeImageView badgedImageView = (BadgedFourThreeImageView) getView();
if (resource instanceof GifDrawable) {
Bitmap image = ((GifDrawable) resource).getFirstFrame();
if (image != null) {
// look at the corner to determine the gif badge color
int cornerSize = (int) (56 * getView().getContext().getResources().getDisplayMetrics
().scaledDensity);
Bitmap corner = Bitmap.createBitmap(image,
image.getWidth() - cornerSize,
image.getHeight() - cornerSize,
cornerSize, cornerSize);
boolean isDark = ColorUtils.isDark(corner);
corner.recycle();
badgedImageView.setBadgeColor(ContextCompat.getColor(getView().getContext(),
isDark ? R.color.gif_badge_dark_image : R.color.gif_badge_light_image));
} else {
badgedImageView.setBadgeColor(ContextCompat.getColor(getView().getContext(),
R.color.gif_badge_light_image));
}
}
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_WHAT_SHOW_NETWORK_IMAGE:
String url = msg.getData().getString("url");
RequestOptions requestOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.RESOURCE)
.override(150, 150)
.placeholder(R.mipmap.ic_launcher);
Glide.with(getActivity()).asBitmap().load(url)
.apply(requestOptions)
.thumbnail(0.5F)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
// OutputStream outputStream = null;
// bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
mImageView.setImageBitmap(bitmap);
}
});
break;
}
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_WHAT_SHOW_NETWORK_IMAGE:
String url = msg.getData().getString("url");
RequestOptions requestOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.RESOURCE)
.override(150, 150)
.placeholder(R.mipmap.ic_launcher);
Glide.with(getActivity()).asBitmap().load(url)
.apply(requestOptions)
.thumbnail(0.5F)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
// mImageView.setImageBitmap(CommonUtil.toRoundCorner(bitmap, 100));
mImageView.setImageBitmap(bitmap);
}
});
break;
}
}
private void updateBackground(String uri) {
int width = mMetrics.widthPixels;
int height = mMetrics.heightPixels;
RequestOptions options = new RequestOptions()
.centerCrop()
.error(mDefaultBackground);
Glide.with(this)
.asBitmap()
.load(uri)
.apply(options)
.into(new SimpleTarget<Bitmap>(width, height) {
@Override
public void onResourceReady(
Bitmap resource,
Transition<? super Bitmap> transition) {
mBackgroundManager.setBitmap(resource);
}
});
}
private void updateBackground(String uri) {
RequestOptions options = new RequestOptions()
.centerCrop()
.error(mDefaultBackground);
Glide.with(this)
.asBitmap()
.load(uri)
.apply(options)
.into(new SimpleTarget<Bitmap>(mMetrics.widthPixels, mMetrics.heightPixels) {
@Override
public void onResourceReady(
Bitmap resource,
Transition<? super Bitmap> transition) {
mBackgroundManager.setBitmap(resource);
}
});
}
public static void load(Context context, String url, OnDrawableCallback callback) {
if (context == null) {
return;
}
if (context instanceof Activity && ((Activity) context).isDestroyed()) {
return;
}
GlideApp.with(context)
.load(url)
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
if (callback != null) {
callback.onDrawable(resource);
}
}
});
}
public static void loadSmallImageMessage(Context context, String url, final ImageView imageView) {
if (null == imageMessageOptions) {
imageMessageOptions = new RequestOptions().placeholder(R.drawable.icon_user);
}
Glide.with(context).asBitmap().load(url).apply(imageMessageOptions).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
@Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (width > height) {
width = (width / height) * 198;
height = 198;
} else {
height = (height / width) * 198;
width = 198;
}
imageView.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
imageView.setImageBitmap(bitmap);
}
});
}
private void saveToSystemGallery(final String name, int id) {
GlideApp.with(this).downloadOnly().load(id).into(new SimpleTarget<File>() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), name + ".jpg");
try {
FileUtils.copyFile(resource, file);
showMessage("保存图片成功了", TastyToast.SUCCESS);
notifySystemGallery(file);
} catch (IOException e) {
e.printStackTrace();
showMessage("保存图片失败了", TastyToast.ERROR);
}
}
});
}
public static void set(String url, int setTo, Context context, SetWallpaperListener setWallpaperListener) {
Toast.makeText(context, R.string.save_image_first, Toast.LENGTH_SHORT).show();
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Glide.with(context).asBitmap().load(url).into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
new SetAsWallpaperAsyncTask(resource, setTo, wallpaperManager, windowManager, setWallpaperListener).execute();
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
private void saveToSystemGallery(final String name, int id) {
GlideApp.with(this).downloadOnly().load(id).into(new SimpleTarget<File>() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), name + ".jpg");
try {
FileUtils.copyFile(resource, file);
showMessage("保存图片成功了", TastyToast.SUCCESS);
notifySystemGallery(file);
} catch (IOException e) {
e.printStackTrace();
showMessage("保存图片失败了", TastyToast.ERROR);
}
}
});
}
public static void displayAsBitmap(Context context, String path, final OnBitmapResourceCallBack callBack){
GlideApp.with(context)
.load(path)
.dontAnimate()
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
if(callBack!=null){
BitmapDrawable bitmapDrawable = (BitmapDrawable) resource;
Bitmap bitmap = null;
if(bitmapDrawable!=null){
bitmap = bitmapDrawable.getBitmap();
}
callBack.onResourceReady(bitmap);
}
}
});
}
private void loadImageView() {
final String url = getArguments().getString(ARGUMENTS_IMAGE);
Glide.with(this).downloadOnly().load(url)
/* todo replace error icon */
.apply(new RequestOptions().error(R.mipmap.qq_refresh_success))
.into(new SimpleTarget<File>() {
@Override
public void onResourceReady(File resource, Transition<? super File> transition) {
mImageView.setImage(ImageSource.uri(Uri.fromFile(resource)));
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
}
});
}
/**
* 更新通知
*/
private void updateNotification() {
final int dimen = DensityUtil.dp2px(this, 128);
Glide.with(this)
.asBitmap()
.load(cover)
.into(new RequestFutureTarget<Bitmap>(dimen, dimen) {
@Override
public synchronized void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
showNotification(resource);
}
@Override
public synchronized void onLoadFailed(@Nullable Drawable errorDrawable) {
showNotification(BitmapFactory.decodeResource(getResources(), R.drawable.img_cover_default));
}
});
}
private void updateNotification() {
final String coverUrl = bookShelfBean == null ? null : bookShelfBean.getBookInfoBean().getRealCoverUrl();
final int dimen = DensityUtil.dp2px(this, 128);
Glide.with(this)
.asBitmap()
.load(coverUrl)
.into(new RequestFutureTarget<Bitmap>(dimen, dimen) {
@Override
public synchronized void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
showNotification(resource);
}
@Override
public synchronized void onLoadFailed(@Nullable Drawable errorDrawable) {
showNotification(BitmapFactory.decodeResource(getResources(), R.drawable.img_cover_default));
}
});
}
private void loadImage(String imageTitle) {
int maxImageSide = presenter.calcMaxImageSide();
ImageUtil.loadImageWithSimpleTarget(GlideApp.with(this),
PostManager.getInstance(this.getApplicationContext()).getOriginImageStorageRef(imageTitle),
new SimpleTarget<Bitmap>(maxImageSide, maxImageSide) {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
progressBar.setVisibility(View.GONE);
touchImageView.setImageBitmap(resource);
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
progressBar.setVisibility(View.GONE);
touchImageView.setImageResource(R.drawable.ic_stub);
}
});
}
@Override
public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {
int w = DensityUtil.getScreenWidth(mContext);
int hh = resource.getIntrinsicHeight();
int ww = resource.getIntrinsicWidth();
int high = hh * (w - 50) / ww;
Rect rect = new Rect(20, 20, w - 30, high);
resource.setBounds(rect);
urlDrawable.setBounds(rect);
urlDrawable.setDrawable(resource);
gifDrawables.add(resource);
resource.setCallback(mTextView);
resource.start();
resource.setLoopCount(GifDrawable.LOOP_FOREVER);
mTextView.setText(mTextView.getText());
mTextView.invalidate();
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
if (nowPlayingClaimBitmap == null &&
nowPlayingClaim != null &&
!Helper.isNullOrEmpty(nowPlayingClaim.getThumbnailUrl())) {
Glide.with(getApplicationContext()).asBitmap().load(nowPlayingClaim.getThumbnailUrl()).into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
nowPlayingClaimBitmap = resource;
callback.onBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
return nowPlayingClaimBitmap;
}
@Override
protected void loadImageFromFile(@NonNull final Uri selectedFileUri) {
GlideApp.with(AlbumTagEditorActivity.this)
.as(BitmapPaletteWrapper.class)
.load(selectedFileUri)
.transition(new GenericTransitionOptions<BitmapPaletteWrapper>().transition(android.R.anim.fade_in))
.apply(new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true))
.into(new VinylSimpleTarget<BitmapPaletteWrapper>() {
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
}
@Override
public void onResourceReady(@NonNull BitmapPaletteWrapper resource, Transition<? super BitmapPaletteWrapper> glideAnimation) {
VinylMusicPlayerColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT);
albumArtBitmap = ImageUtil.resizeBitmap(resource.getBitmap(), 2048);
setImageBitmap(albumArtBitmap, VinylMusicPlayerColorUtil.getColor(resource.getPalette(), ATHUtil.resolveColor(AlbumTagEditorActivity.this, R.attr.defaultFooterColor)));
deleteAlbumArt = false;
dataChanged();
setResult(RESULT_OK);
}
});
}
@Override
public boolean onBindItem(PickerItem item, boolean create, int i) {
super.onBindItem(item,create,i);
Genre genre = mData.get(i);
item.setTitle(genre.name);
item.setRadiusUnit(genre.songCount);
// Glide
ArrayList<Song> songs = GenreLoader.getSongs(mContext,genre.id);
Glide.with(mContext).load(MusicUtil.getMediaStoreAlbumCoverUri(songs.get(0).albumId)).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
item.setBackgroundImage(resource);
GenrePickerAdapter.this.notifyBackImageUpdated(i);
}
});
return true;
}
@Override
public boolean onBindItem(PickerItem item, boolean create, int i) {
super.onBindItem(item,create,i);
Song song = mData.get(i);
item.setTitle(song.title);
item.setRadiusUnit(PickerItem.SIZE_RANDOM);
// Glide
Glide.with(mContext).load(MusicUtil.getMediaStoreAlbumCoverUri(song.albumId)).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
item.setBackgroundImage(resource);
SongPickerAdapter.this.notifyBackImageUpdated(i);
}
});
return true;
}
@Override
public void onBindViewHolder(final ContributorAdapter.ViewHolder holder, int position) {
ContributorData contributor = contributors.get(position);
Glide.with(holder.imageView.getContext()).asBitmap().load(contributor.imageUrl).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
holder.imageView.setImageBitmap(resource);
}
});
holder.textView.setText(contributor.name);
holder.itemView.setTag(contributor);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view.getTag() != null && view.getTag() instanceof ContributorData)
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(((ContributorData) view.getTag()).url)));
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == REQUEST_CODE_AVATAR && resultCode == Activity.RESULT_OK && data != null) {
if (data.getBooleanExtra("delete", false)) {
viewModel.setAvatar(null);
return;
}
final Media result = data.getParcelableExtra(AvatarSelectionActivity.EXTRA_MEDIA);
final DecryptableStreamUriLoader.DecryptableUri decryptableUri = new DecryptableStreamUriLoader.DecryptableUri(result.getUri());
GlideApp.with(this)
.asBitmap()
.load(decryptableUri)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.override(AvatarHelper.AVATAR_DIMENSIONS, AvatarHelper.AVATAR_DIMENSIONS)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
viewModel.setAvatar(Objects.requireNonNull(BitmapUtil.toByteArray(resource)));
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
public static void loadBlurredIconIntoViewBackground(@NonNull Recipient recipient, @NonNull View target) {
Context context = target.getContext();
if (recipient.getContactPhoto() == null) {
target.setBackgroundColor(ContextCompat.getColor(target.getContext(), R.color.black));
return;
}
GlideApp.with(target)
.load(recipient.getContactPhoto())
.transform(new CenterCrop(), new BlurTransformation(context, 0.25f, BlurTransformation.MAX_RADIUS))
.into(new CustomViewTarget<View, Drawable>(target) {
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
target.setBackgroundColor(ContextCompat.getColor(target.getContext(), R.color.black));
}
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
target.setBackground(resource);
}
@Override
protected void onResourceCleared(@Nullable Drawable placeholder) {
target.setBackground(placeholder);
}
});
}
private void onCaptureClicked() {
orderEnforcer.reset();
Stopwatch fastCaptureTimer = new Stopwatch("Capture");
camera.capture((jpegData, frontFacing) -> {
fastCaptureTimer.split("captured");
Transformation<Bitmap> transformation = frontFacing ? new MultiTransformation<>(new CenterCrop(), new FlipTransformation())
: new CenterCrop();
GlideApp.with(this)
.asBitmap()
.load(jpegData)
.transform(transformation)
.override(cameraPreview.getWidth(), cameraPreview.getHeight())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
fastCaptureTimer.split("transform");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resource.compress(Bitmap.CompressFormat.JPEG, 80, stream);
fastCaptureTimer.split("compressed");
byte[] data = stream.toByteArray();
fastCaptureTimer.split("bytes");
fastCaptureTimer.stop(TAG);
controller.onImageCaptured(data, resource.getWidth(), resource.getHeight());
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
controller.onCameraError();
}
});
});
}
private void refreshData(Topic topic) {
topicTitleTextView.setText(topic.getTitle());
topicContentTextView.setText(topic.getContent());
String imgFullUrl = NetConstant.RESOURCES_BASE + topic.getImg();
Glide.with(this).asBitmap().load(imgFullUrl).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
// topicImageView.setImageBitmap(CommonUtil.toRoundCorner(bitmap, bitmap.getWidth()));
topicImageView.setImageBitmap(bitmap);
}
});
}
public static void resizeImage(Activity activity, Uri uri, int width_px, int height_px, Intent adv) {
Glide.with(activity)
.asBitmap()
.load(uri)
.into(new SimpleTarget<Bitmap>(width_px, height_px) {
@Override
public void onResourceReady(@NonNull Bitmap bitmap, Transition<? super Bitmap> transition) {
saveResizedImage(bitmap, activity, uri, adv);
}
});
}
private void loadAndSetBackgroundImageParameter(VideoEntity selectedVideo,
final Activity activity, DisplayMetrics metrics,
final BackgroundManager backgroundManager, RequestOptions defaultPlaceHolder,
Drawable defualtDrawble) {
if (selectedVideo == null) {
return;
}
String url1 = selectedVideo.getVideoBgImageLocalStorageUrl();
String url2 = selectedVideo.getBgImageUrl();
String loadedUri;
if (url1.isEmpty()) {
loadedUri = url2;
} else {
loadedUri = url1;
}
// wait until runtime the activity has window
if (!backgroundManager.isAttached()) {
backgroundManager.attach(activity.getWindow());
}
// glide on error
Glide.with(activity)
.asBitmap()
.load(loadedUri)
.apply(defaultPlaceHolder)
.into(new SimpleTarget<Bitmap>(metrics.widthPixels, metrics.heightPixels) {
@Override
public void onResourceReady(Bitmap resource,
Transition<? super Bitmap> glideAnimation) {
backgroundManager.setDrawable(
new BitmapDrawable(activity.getResources(), resource));
}
});
}
private void loadAndSetVideoCardImage() {
String loadedUri = findLocalContentUriOrNetworkUrl(CARD);
Glide.with(this)
.asBitmap()
.load(loadedUri)
.apply(mDefaultPlaceHolder)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(final Bitmap resource,
Transition<? super Bitmap> glideAnimation) {
mDescriptionOverviewRow.setImageBitmap(getActivity(), resource);
}
});
setSelectedPosition(0, false);
}
@Override
public void loadImage(String url,final ImageView iv) {
Glide.with(iv.getContext())
.load(url)
.into(new SimpleTarget<Drawable>(200, 200) {
@Override
public void onResourceReady(Drawable resource,
Transition<? super Drawable> transition) {
iv.setImageDrawable(resource);
}
});
}
private void setupDetailsOverviewRow() {
final DetailsOverviewRow row = new DetailsOverviewRow(mSelectedVideo);
RequestOptions options = new RequestOptions()
.error(R.drawable.default_background)
.dontAnimate();
Glide.with(this)
.asBitmap()
.load(mSelectedVideo.cardImageUrl)
.apply(options)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(
Bitmap resource,
Transition<? super Bitmap> transition) {
row.setImageBitmap(getActivity(), resource);
startEntranceTransition();
}
});
SparseArrayObjectAdapter adapter = new SparseArrayObjectAdapter();
adapter.set(ACTION_WATCH_TRAILER, new Action(ACTION_WATCH_TRAILER, getResources()
.getString(R.string.watch_trailer_1),
getResources().getString(R.string.watch_trailer_2)));
adapter.set(ACTION_RENT, new Action(ACTION_RENT, getResources().getString(R.string.rent_1),
getResources().getString(R.string.rent_2)));
adapter.set(ACTION_BUY, new Action(ACTION_BUY, getResources().getString(R.string.buy_1),
getResources().getString(R.string.buy_2)));
row.setActionsAdapter(adapter);
mAdapter.add(row);
}