/** * 计算图片分辨率 * * @param context * @param url * @return * @throws ExecutionException * @throws InterruptedException */ public static String calePhotoSize(Context context, String url) throws ExecutionException, InterruptedException { File file = Glide.with(context).load(url) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get(); // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); return options.outWidth + "*" + options.outHeight; } }
/** * Cancel any pending loads Glide may have for the target and free any resources that may have been loaded into * the target so they may be reused. * * @param target The target to cancel loads for. */ public static void clear(FutureTarget<?> target) { target.clear(); }
@Override protected void onUnsubscribe() { if (futureTask != null) { futureTask.callback.getFutureTarget().clear(); } mainHandler.removeCallbacksAndMessages(null); GlideBlurOnSubscribe.this.context = null; } });
@Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); for (Photo photo : photos) { if (isCancelled) { return; } FutureTarget<File> futureTarget = GlideApp.with(context) .downloadOnly() .load(photo) .submit(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE); try { futureTarget.get(); } catch (InterruptedException e) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Interrupted waiting for background downloadOnly", e); } } catch (ExecutionException e) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Got ExecutionException waiting for background downloadOnly", e); } } GlideApp.with(context).clear(futureTarget); } } }
@Test public void load_withBitmapResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(android.R.drawable.star_big_off) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withBitmapAliasResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.bitmap_alias) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withVectorDrawableResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.vector_drawable) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withShapeDrawableResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.shape_drawable) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withStateListDrawableResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.state_list_drawable) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withNinePatchResourceId_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.googlelogo_color_120x44dp) .submit() .get(); assertThat(drawable).isNotNull(); }
@Override public void run() throws Throwable { Glide.with(context) .load(ResourceIds.drawable.shape_drawable) .apply(centerCropTransform()) .submit() .get(); } });
@Override public void run() throws Throwable { Glide.with(context) .asBitmap() .load(ResourceIds.drawable.shape_drawable) .submit() .get(); } });
@Override public void run() throws Throwable { Glide.with(context) .load(colorDrawable) .apply(new RequestOptions() .centerCrop()) .submit() .get(); } });
@Test public void load_withShapeDrawableResourceId_asBitmap_withValidSize_returnsNonNullBitmap() throws ExecutionException, InterruptedException { Bitmap bitmap = Glide.with(context) .asBitmap() .load(ResourceIds.drawable.shape_drawable) .submit(100, 200) .get(); assertThat(bitmap).isNotNull(); assertThat(bitmap.getWidth()).isEqualTo(100); assertThat(bitmap.getHeight()).isEqualTo(200); }
@Test public void load_withStateListDrawableResourceId_asDrawable_withTransformation_nonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.state_list_drawable) .apply(centerCropTransform()) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withVectorDrawableResourceId_asDrawable_withTransformation_nonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.vector_drawable) .apply(centerCropTransform()) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withShapeDrawableResourceId_asDrawable_withTransformation_validSize_succeeds() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.shape_drawable) .apply(bitmapTransform(new RoundedCorners(10))) .submit(100, 200) .get(); assertThat(drawable).isNotNull(); assertThat(drawable.getIntrinsicWidth()).isEqualTo(100); assertThat(drawable.getIntrinsicHeight()).isEqualTo(200); }
@Test public void load_withBitmapAliasResourceId_asDrawable_withTransformation_producesNonNullDrawable() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(ResourceIds.drawable.bitmap_alias) .apply(centerCropTransform()) .submit() .get(); assertThat(drawable).isNotNull(); }
@Test public void load_withBitmapResourceId_asDrawable_withTransformation_producesNonNullBitmap() throws ExecutionException, InterruptedException { Drawable drawable = Glide.with(context) .load(android.R.drawable.star_big_off) .apply(centerCropTransform()) .submit() .get(); assertThat(drawable).isNotNull(); }