Skip to content

Subclass ViewTarget #3

Description

@thaindq

I'm trying to port this code to C# and this is what I've got so far

public class GlideImageGetter : Java.Lang.Object, Html.IImageGetter, Drawable.ICallback
{
    private Context mContext;
    private TextView mTextView;
    private HashSet<ImageGetterViewTarget> mTargets;

    public static GlideImageGetter Get(View view)
    {
        return (GlideImageGetter)view.GetTag(Resource.Id.drawable_callback_tag);
    }

    public void Clear()
    {
        GlideImageGetter prev = Get(mTextView);
        if (prev == null)
            return;

        foreach (var target in prev.mTargets)
        {
            Glide.Clear(target);
        }
    }

    public GlideImageGetter(Context context, TextView textView)
    {
        mContext = context;
        mTextView = textView;
        Clear();
        mTargets = new HashSet<ImageGetterViewTarget>();
        mTextView.SetTag(Resource.Id.drawable_callback_tag, this);
    }

    public Drawable GetDrawable(string source)
    {
        UrlDrawable urlDrawable = new UrlDrawable();

        Glide.With(mContext)
            .Load(source)
            .DiskCacheStrategy(DiskCacheStrategy.All)
            .Into(new ImageGetterViewTarget(mTextView, urlDrawable, mTargets));

        return urlDrawable;
    }

    public void InvalidateDrawable(Drawable who)
    {
        mTextView.Invalidate();
    }

    public void ScheduleDrawable(Drawable who, IRunnable what, long when) { }

    public void UnscheduleDrawable(Drawable who, IRunnable what) { }

    private class ImageGetterViewTarget : ViewTarget
    {
        private UrlDrawable mDrawable;

        public ImageGetterViewTarget(TextView view, UrlDrawable drawable, HashSet<ImageGetterViewTarget> mTargets) : base(view)
        {
            mTargets.Add(this);
            mDrawable = drawable;
        }

        public void OnResourceReady(Java.Lang.Object resource, IGlideAnimation glideAnimation)
        {
            Rect rect;
            TextView view = (TextView)View;
            GlideDrawable drawable = (GlideDrawable)resource;

            if (drawable.IntrinsicWidth > 100)
            {
                float width;
                float height;

                if (drawable.IntrinsicWidth >= view.Width)
                {
                    float downScale = (float)drawable.IntrinsicWidth / view.Width;
                    width = drawable.IntrinsicWidth / downScale;
                    height = drawable.IntrinsicHeight / downScale;
                }
                else
                {
                    float multiplier = (float)view.Width / drawable.IntrinsicWidth;
                    width = drawable.IntrinsicWidth * multiplier;
                    height = drawable.IntrinsicHeight * multiplier;
                }

                rect = new Rect(0, 0, Java.Lang.Math.Round(width), Java.Lang.Math.Round(height));
            }
            else
            {
                rect = new Rect(0, 0, drawable.IntrinsicWidth * 2, drawable.IntrinsicHeight * 2);
            }

            drawable.Bounds = rect;

            mDrawable.Bounds = rect;
            mDrawable.SetDrawable(drawable);


            if (drawable.IsAnimated)
            {
                mDrawable.SetCallback(Get(view));
                drawable.SetLoopCount(GlideDrawable.LoopForever);
                drawable.Start();
            }

            view.Text = view.Text;
            view.Invalidate();
        }        
    }
}

When I build the project, this error pops up

GlideImageGetter_ImageGetterViewTarget is not abstract and does not override abstract method onResourceReady(Object,GlideAnimation) in Target

I looked into binding interface Com.Bumptech.Glide.Request.Target.ITarget and couldn't find that abstract method but it does exist in java code. I guess the generic type Transition<? super R> transition is the cause. If so, is there any solution to this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions