Glide中的回调:targets

Glide隐藏了一大推复杂的在后台的场景,Glide做了所有的网络请求和处理在后台线程中,准备好了切回到ui线程后更新ImageView.

假设ImageView不再是图像的最后一步。我们只要Bitmap本身。Glide提供了一个用Targets的方式的接收图片资源的Bitmap.Targets是回调函数,图字Glide做完所有的加载和处理之后返回结果。

一:Glide提供了好几种Targets,并且每个都有其明确的目的。我们从simpleTarget开始

private SimpleTarget target = new SimpleTarget<Bitmap>() {  
    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
        // do something with the bitmap
        // for demonstration purposes, let's just set it to an ImageView
        imageView1.setImageBitmap( bitmap );
    }
};

private void loadImageSimpleTarget() {  
    Glide
        .with( context ) // could be an issue!
        .load( eatFoodyImages[0] )
        .asBitmap()
        .into( target );
}

  这段代码的第一部分创建了一个字段,声明了一个方法,即一旦Glide已经加载并处理完成图像,它将被调用。这个方法传了Bitmap作为一个参数。之后就可以使用Bitmap去进行我们的操作。

这段代码的第二部分是我们如何使用Glide用Targets和ImageView用法完全相同。你既可以传递一个Targets,也可以传递一个ImageView给into()方法。Glide自己处理并把结果给任何一个。添加.asBitmap,它强制Glide返回一个Bitmap对象。Glide也可以加载Gif和video.为了防止tatget的冲突(我们需要Bitmap)和未知资源和网络背后的URL(可能是一个GIF).,我们可以调用.asBitmap()告诉Glide我们需要一个图像。

二:除了实现简单版本的Glide的Target回调体统,还有一些地方需要我们注意。

1.首先是 SimpleTarget 对象的字段声明。into()中可以创建一个内部类供我们使用,但是这样就增加了一个可能性:就是Glide在做完图片请求之前,Android垃圾回收已经移除了匿名内部类,这样就会导致在Glide自动完成图片加载的时候回调不会被调用。所以声明的回调对象是一个字段对象,这样可以避免被Android垃圾回收机制回收。

2.with()可以传递当前应用的activity,这样当前activity销毁是Glide也会停止加载。如果你的tatget是独立于应用的activity的生命周期。这时候我们可以用context.getApplicationContext)。应用资源完全停止的时候,Glide才会杀死图片请求。

private void loadImageSimpleTargetApplicationContext() {  
    Glide
        .with( context.getApplicationContext() ) // safer!
        .load( eatFoodyImages[1] 
        .asBitmap()
        .into( target2 );
}

  三:Target的指定尺寸

target没有指定大小,如果传递一个imageview座位参数传递给into(),Glide将会使用imageview的大小去限制图形的大小,如果加载的图片是 1000x1000 像素的,但是 ImageView 只有 250x250 像素,Glide 将会减少图片的尺寸去节省时间和内存。但是 使用target时候并没有已知大小。如果有一个指定的大小,可以加强回调。你可以加强回调。如果你知道这种图片应该要多大,你应该在你的回调声明中指定它以节省一些内存。

private SimpleTarget target2 = new SimpleTarget<Bitmap>( 250, 250 ) {  
    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
        imageView2.setImageBitmap( bitmap );
    }
};

private void loadImageSimpleTargetApplicationContext() {  
    Glide
        .with( context.getApplicationContext() ) // safer!
        .load( eatFoodyImages[1] )
        .asBitmap()
        .into( target2 );
}

  四:ViewTarget的使用

不直接使用imageview的原因是多种多样的,我们已经展示如何接受一个Bitmap.但是对于一个自定义的控件,Glide并不知道应该把图片加载到什么地方去,因为有些自定义的控件可能是复合控件。所以这时候我们就要用到ViewTarget.

首先是一个自定义控件,里面封装的有ImageView.

public class FutureStudioView extends FrameLayout {  
    ImageView iv;
    TextView tv;

    public void initialize(Context context) {
        inflate( context, R.layout.custom_view_futurestudio, this );

        iv = (ImageView) findViewById( R.id.custom_view_image );
        tv = (TextView) findViewById( R.id.custom_view_text );
    }

    public FutureStudioView(Context context, AttributeSet attrs) {
        super( context, attrs );
        initialize( context );
    }

    public FutureStudioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super( context, attrs, defStyleAttr );
        initialize( context );
    }

    public void setImage(Drawable drawable) {
        iv = (ImageView) findViewById( R.id.custom_view_image );

        iv.setImageDrawable( drawable );
    }
}

 我们定义的ViewTarget

 

private void loadImageViewTarget() {  
    FutureStudioView customView = (FutureStudioView) findViewById( R.id.custom_view );

    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 );
}

 

  在 target 回调方法中,我们使用我们创建的方法 setImage(Drawable drawable) 在自定义 view 类中去设置图片。另外确保你注意到我们必须在ViewTarget 的构造函数中传递我们自定义 view 作为参数:new ViewTarget<FutureStudioView, GlideDrawable>(customView).这应该涵盖了所有你需要的自定义 view。你也可以在回调中添加额外的工作。如,我们可以分析传入的 Bitmap 的主要的颜色并设置十六进制值给 TextView。

总结:target分为两种,SimpleTarget和ViewTarget两种,SimpleTarget返回bitmap对象,可以供我们使用。ViewTarget主要使用场景是自定义的控件,因为Glide不能直接设置到自定义控件上,所以需要借助ViewTarget来完成给自定义控件赋值的操作。我们可以先进行图片加载的网络请求,然后在target的回调中根据返回的参数对我们的需求进行逻辑处理。

 

Target 指定尺寸

posted @ 2017-08-16 14:35  让学习如呼吸一般自然  阅读(523)  评论(0编辑  收藏  举报