/** * Sets the {@link ImageView} the resource will be loaded into, cancels any existing loads into * the view, and frees any resources Glide may have previously loaded into the view so they may be * reused. * * @see RequestManager#clear(Target) * * @param view The view to cancel previous loads for and load the new resource into. * @return The * {@link com.bumptech.glide.request.target.Target} used to wrap the given {@link ImageView}. */ public ViewTarget<ImageView, TranscodeType> into(ImageView view) { Util.assertMainThread(); Preconditions.checkNotNull(view); RequestOptions requestOptions = this.requestOptions; if (!requestOptions.isTransformationSet() && requestOptions.isTransformationAllowed() && view.getScaleType() != null) { // Clone in this method so that if we use this RequestBuilder to load into a View and then // into a different target, we don't retain the transformation applied based on the previous // View's scale type. switch (view.getScaleType()) { case CENTER_CROP: requestOptions = requestOptions.clone().optionalCenterCrop(); break; case CENTER_INSIDE: requestOptions = requestOptions.clone().optionalCenterInside(); break; case FIT_CENTER: case FIT_START: case FIT_END: requestOptions = requestOptions.clone().optionalFitCenter(); break; case FIT_XY: requestOptions = requestOptions.clone().optionalCenterInside(); break; case CENTER: case MATRIX: default: // Do nothing. } } return into( glideContext.buildImageViewTarget(view, transcodeClass), /*targetListener=*/ null, requestOptions); }
@Override public void applyOptions(Context context, GlideBuilder builder) { ViewTarget.setTagId(R.id.glide_tag_id); // Apply options to the builder here. // 默认内存和图片池大小 // MemorySizeCalculator calculator = new MemorySizeCalculator(context); // int defaultMemoryCacheSize = calculator.getMemoryCacheSize(); // 默认内存大小 // int defaultBitmapPoolSize = calculator.getBitmapPoolSize(); // 默认图片池大小 // builder.setMemoryCache(new LruResourceCache(defaultMemoryCacheSize)); // 该两句无需设置,是默认的 // builder.setBitmapPool(new LruBitmapPool(defaultBitmapPoolSize)); //定义图片的本地磁盘缓存 // File cacheDir = context.getExternalCacheDir();//指定的是数据的缓存地址 // int diskCacheSize = 1024 * 1024 * 1024;//最多可以缓存多少字节的数据 //设置磁盘缓存大小 // builder.setDiskCache(new DiskLruCacheFactory(cacheDir.getPath(), "glide", diskCacheSize)); // 定义缓存大小和位置 if (BaseConfig.sSDCardExist) { builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, Constants.Dir.IMAGE_CACHE_DIR, extDiskSize)); //外部存储 } else { builder.setDiskCache(new InternalCacheDiskCacheFactory(context, Constants.Dir.IMAGE_CACHE_DIR, intDiskSize)); //内部存储 } // 自定义内存和图片池大小 // builder.setMemoryCache(new LruResourceCache(memorySize)); // builder.setBitmapPool(new LruBitmapPool(memorySize)); // builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); }
@Override public void onCreate() { super.onCreate(); ViewTarget.setTagId(R.id.glide_tag); isInstallWx = isWXAppInstalledAndSupported(); //alibaba System.loadLibrary("gnustl_shared"); // System.loadLibrary("ijkffmpeg");//目前使用微博的ijkffmpeg会出现1K再换wifi不重连的情况 System.loadLibrary("qupai-media-thirdparty"); // System.loadLibrary("alivc-media-jni"); System.loadLibrary("qupai-media-jni"); ApplicationGlue.initialize(this); //Bugly SDK初始化 CrashReport.initCrashReport(getApplicationContext(), "3080098224", false); }
@Override public void applyOptions(Context context, GlideBuilder builder) { //解决setTag问题 ViewTarget.setTagId(R.id.glide_tag_id); //磁盘缓存 builder.setDiskCache(new DiskLruCacheFactory(AppFileHelper.getAppCachePath(), 50 * 1024 * 1024)); builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); //内存缓存 MemorySizeCalculator calculator = new MemorySizeCalculator(context); int defaultMemoryCacheSize = calculator.getMemoryCacheSize(); int defaultBitmapPoolSize = calculator.getBitmapPoolSize(); //设置比默认大小大1.5倍的缓存和图片池大小 int customMemoryCacheSize = (int) (1.5 * defaultMemoryCacheSize); int customBitmapPoolSize = defaultBitmapPoolSize; KLog.i("poolSize", "bitmapPoolSize >>>>> " + android.text.format.Formatter.formatFileSize(context, customBitmapPoolSize) + " memorySize>>>>>>>> " + android.text.format.Formatter.formatFileSize(context, customMemoryCacheSize)); builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize)); builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize)); }
public void display(Activity act, File file, ImageView tarView, final OnLoadImageListener listener) { if (listener == null) { return; } Glide.with(act).load(file).into(new ViewTarget<ImageView, GlideDrawable>(tarView) { @Override public void onStart() { super.onStart(); listener.onStart(); } @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { listener.onLoadCompleted(resource, glideAnimation); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); listener.onLoadFailed(e, errorDrawable); } }); }
public void display(Activity act, File file, int errorImg, ImageView tarView, final OnLoadImageListener listener) { if (listener == null) { return; } Glide.with(act).load(file).error(errorImg).into(new ViewTarget<ImageView, GlideDrawable> (tarView) { @Override public void onStart() { super.onStart(); listener.onStart(); } @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { listener.onLoadCompleted(resource, glideAnimation); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); listener.onLoadFailed(e, errorDrawable); } }); }
public void display(Fragment fragment, String load, ImageView tarView, final OnLoadImageListener listerner) { if (listerner == null) { return; } Glide.with(fragment).load(load).into(new ViewTarget<ImageView, GlideDrawable>(tarView) { @Override public void onStart() { super.onStart(); listerner.onStart(); } @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { listerner.onLoadCompleted(resource, glideAnimation); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); listerner.onLoadFailed(e, errorDrawable); } }); }
public void display(Context context, String load, ImageView tarView, final OnLoadImageListener listener) { Glide.with(context).load(load).into(new ViewTarget<ImageView, GlideDrawable>(tarView) { @Override public void onStart() { super.onStart(); listener.onStart(); } @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { listener.onLoadCompleted(resource, glideAnimation); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); listener.onLoadFailed(e, errorDrawable); } }); }
/** * Loads an image into the OkulusImageView * * @param context A reference to the context * @param imageView The ImageView to load the bitmap into * @param imageResourceId The image resource to load * @param avatarSize The size to scale the image to */ public static void loadCircularImage(Context context, OkulusImageView imageView, int imageResourceId, AvatarBitmapTransformation.AvatarSize avatarSize) { Glide.with(context) .load(imageResourceId) .asBitmap() .transform(AvatarBitmapTransformation.transformationFor(context, avatarSize)) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .into(new ViewTarget<OkulusImageView, Bitmap>(imageView) { @Override public void onResourceReady(Bitmap resource, GlideAnimation anim) { OkulusImageView myView = this.view; myView.setImageBitmap(resource); // Set your resource on myView and/or start your animation here. } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photogallery); //为glide设置全局tag id,避免ImageView Tag被glide占用 ViewTarget.setTagId(R.id.glide_request); mUrls = getIntent().getStringArrayExtra(EXTRA_URLS); mViewPager = (HackyViewPager) findViewById(R.id.view_pager); mViewPager.setAdapter(new SamplePagerAdapter()); mViewPager.setCurrentItem(getIntent().getIntExtra(EXTRA_POSITION, 0)); findViewById(R.id.button_original).setOnClickListener(this); //findViewById(R.id.button_save).setOnClickListener(this); }
private RequestBuilder<Object> getNullModelRequest() { when(glideContext.buildImageViewTarget(isA(ImageView.class), isA(Class.class))) .thenReturn(mock(ViewTarget.class)); when(glideContext.getDefaultRequestOptions()).thenReturn(new RequestOptions()); when(requestManager.getDefaultRequestOptions()) .thenReturn(new RequestOptions()); when(requestManager.getDefaultTransitionOptions(any(Class.class))) .thenReturn(new GenericTransitionOptions<>()); return new RequestBuilder<>(glide, requestManager, Object.class, context) .load((Object) null); }
@Override public void onCreate() { super.onCreate(); getAppComponent(); ViewTarget.setTagId(R.id.glide_tag); LoggerUtil.init(BuildConfig.DEBUG); if (LeakCanary.isInAnalyzerProcess(this)) { return; } LeakCanary.install(this); }
private String getTargetDescription(Target<?> target) { String result; if (target instanceof WrappingTarget) { Target wrapped = ((WrappingTarget) target).getWrappedTarget(); result = String.format(Locale.ROOT, "%s in %s", getTargetDescription(wrapped), target); } else if (target instanceof ViewTarget) { View v = ((ViewTarget) target).getView(); LayoutParams p = v.getLayoutParams(); result = String.format(Locale.ROOT, "%s(params=%dx%d->size=%dx%d)", target, p.width, p.height, v.getWidth(), v.getHeight()); } else { result = String.valueOf(target); } return result; }
@Override public void applyOptions(final Context context, GlideBuilder builder) { ViewTarget.setTagId(R.id.glide_tag_id); MemorySizeCalculator calculator = new MemorySizeCalculator(context); int defaultMemoryCacheSize = calculator.getMemoryCacheSize(); int defaultBitmapPoolSize = calculator.getBitmapPoolSize(); int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize); int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize); builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize)); builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize)); builder.setDiskCache(new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes)); }
@Override public void onCreate() { super.onCreate(); sInstance = this; // LeakCanary.install(this); //为防止出现 // "You must not call setTag() on a view Glide is targeting" on non-root ImageView //这个问题导致的软件崩溃 ViewTarget.setTagId(R.id.glide_tag); initComponent(); }
@Override public void applyOptions(Context context, GlideBuilder builder) { // set max size of the disk cache for images builder.setDiskCache(new InternalCacheDiskCacheFactory( context, App.getAppComponent().getDownloadPreferencesManager() .getTotalImageCacheSize())); ViewTarget.setTagId(R.id.tag_glide); //从默认的RGB_565改为ARGB_8888显示 builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888)); }
@Override public void applyOptions(final Context context, GlideBuilder builder) { ViewTarget.setTagId(R.id.glide_loader); builder.setDiskCache(new DiskLruCacheFactory(new DiskLruCacheFactory.CacheDirectoryGetter () { @Override public File getCacheDirectory() { return context.getExternalCacheDir(); } }, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE)); }
/** * 加载图片 * * @param listener 图片加载的回调 */ public void display(Activity act, String load, ImageView tarView, final OnLoadImageListener listener) { if (listener == null) { return; } Glide.with(act).load(load).into(new ViewTarget<ImageView, GlideDrawable>(tarView) { @Override public void onStart() { super.onStart(); listener.onStart(); } @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { listener.onLoadCompleted(resource, glideAnimation); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); listener.onLoadFailed(e, errorDrawable); } }); }
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 <X> ViewTarget<ImageView, X> buildImageViewTarget( ImageView imageView, Class<X> transcodeClass) { return imageViewTargetFactory.buildTarget(imageView, transcodeClass); }
@Override public void onCreate() { super.onCreate(); ViewTarget.setTagId(R.id._35096552_tag); }