Glide callback after success in Kotlin

前端 未结 5 1782
孤独总比滥情好
孤独总比滥情好 2021-02-12 23:37
   private SimpleTarget target = new SimpleTarget() {  

    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
             


        
5条回答
  •  心在旅途
    2021-02-13 00:18

    The problem is that in the Java code, you used the type SimpleTarget as the type of target. This is a raw type (missing generic parameters), and is one of the big legacy problems in Java generics. Kotlin doesn't allow raw types, and this is why you got problems while converting.

    To fix this, you should do the following in Java:

    private SimpleTarget target = new SimpleTarget() { ... }
    

    Which will force you to add asBitmap() to your Glide call:

    Glide.with(context)
            .load(uri)
            .asBitmap()
            .override(600, 600)
            .fitCenter()
            .into(target);
    

    Now that your code is using generics safely, it can be translated to Kotlin without a problem:

    Glide.with(context)
            .load(uri)
            .asBitmap()
            .override(600, 600)
            .fitCenter()
            .into>(target)
    

提交回复
热议问题