Search in sources :

Example 6 with DrawableWrapper

use of com.zzhoujay.richtext.drawable.DrawableWrapper in project RichText by zzhoujay.

the class AbstractImageLoader method onLoading.

@Override
public void onLoading() {
    if (!activityIsAlive()) {
        return;
    }
    DrawableWrapper drawableWrapper = drawableWrapperWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    holder.setImageState(ImageHolder.ImageState.LOADING);
    drawableWrapper.setDrawable(config.placeHolder);
    if (holder.getCachedBound() != null) {
        drawableWrapper.setBounds(holder.getCachedBound());
    } else {
        if (!config.autoFix && config.imageFixCallback != null) {
            config.imageFixCallback.onLoading(holder);
        }
        int width;
        int height = 0;
        if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
            width = getRealWidth();
            int ow = config.placeHolder.getBounds().width();
            if (ow != 0) {
                height = config.placeHolder.getBounds().height() * width / ow;
            }
            if (height == 0) {
                height = width / 2;
            }
        } else {
            width = (int) holder.getScaleWidth();
            height = (int) holder.getScaleHeight();
        }
        drawableWrapper.setBounds(0, 0, width, height);
    }
}
Also used : DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper)

Example 7 with DrawableWrapper

use of com.zzhoujay.richtext.drawable.DrawableWrapper in project RichText by zzhoujay.

the class DefaultImageGetter method done.

@Override
public void done(Object from) {
    if (from instanceof AbstractImageLoader) {
        AbstractImageLoader imageLoader = ((AbstractImageLoader) from);
        DrawableWrapper drawableWrapper = (DrawableWrapper) imageLoader.drawableWrapperWeakReference.get();
        if (drawableWrapper != null) {
            if (imageLoader.config.cacheType > CacheType.NONE) {
                cacheBound(imageLoader.holder.getSource(), drawableWrapper.getBounds());
            }
            if (imageLoader.config.cacheType > CacheType.LAYOUT) {
                Drawable drawable = drawableWrapper.getDrawable();
                if (drawable instanceof BitmapDrawable) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    if (bitmap != null) {
                        cacheBitmap(imageLoader.holder.getSource(), bitmap);
                    }
                }
            }
        }
        synchronized (lock) {
            Cancelable cancelable = taskMap.get(imageLoader);
            if (cancelable != null) {
                tasks.remove(cancelable);
            }
            taskMap.remove(imageLoader);
        }
        loadedCount++;
        if (notify != null) {
            notify.done(loadedCount);
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 8 with DrawableWrapper

use of com.zzhoujay.richtext.drawable.DrawableWrapper in project RichText by zzhoujay.

the class DefaultImageGetter method getDrawable.

@Override
public Drawable getDrawable(final ImageHolder holder, final RichTextConfig config, final TextView textView) {
    final DrawableWrapper drawableWrapper = new DrawableWrapper();
    if (config.cacheType >= CacheType.LAYOUT) {
        Rect rect = loadCacheBound(holder.getSource());
        if (rect != null) {
            holder.setCachedBound(rect);
            drawableWrapper.setBounds(rect);
        }
    } else {
        drawableWrapper.setBounds(0, 0, (int) holder.getScaleWidth(), (int) holder.getScaleHeight());
    }
    if (config.cacheType > CacheType.LAYOUT) {
        Bitmap bitmap = loadCacheBitmap(holder.getSource());
        if (bitmap != null) {
            drawableWrapper.setDrawable(new BitmapDrawable(textView.getResources(), bitmap));
            return drawableWrapper;
        }
    }
    Cancelable cancelable;
    AbstractImageLoader imageLoader;
    if (TextKit.isLocalPath(holder.getSource())) {
        LocalFileImageLoader localFileImageLoader = new LocalFileImageLoader(holder, config, textView, drawableWrapper, this);
        Future<?> future = getExecutorService().submit(localFileImageLoader);
        cancelable = new FutureCancelableWrapper(future);
        imageLoader = localFileImageLoader;
    } else {
        byte[] src = Base64.decode(holder.getSource());
        if (src != null) {
            Base64ImageLoader base64ImageLoader = new Base64ImageLoader(src, holder, config, textView, drawableWrapper, this);
            Future<?> future = getExecutorService().submit(base64ImageLoader);
            cancelable = new FutureCancelableWrapper(future);
            imageLoader = base64ImageLoader;
        } else {
            Request builder = new Request.Builder().url(holder.getSource()).get().build();
            Call call = getClient().newCall(builder);
            CallbackImageLoader callback = new CallbackImageLoader(holder, config, textView, drawableWrapper, this);
            cancelable = new CallCancelableWrapper(call);
            imageLoader = callback;
            call.enqueue(callback);
        }
    }
    checkTarget(textView);
    synchronized (lock) {
        tasks.add(cancelable);
        taskMap.put(imageLoader, cancelable);
    }
    return drawableWrapper;
}
Also used : Call(okhttp3.Call) Rect(android.graphics.Rect) DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper) Request(okhttp3.Request) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Bitmap(android.graphics.Bitmap)

Example 9 with DrawableWrapper

use of com.zzhoujay.richtext.drawable.DrawableWrapper in project RichText by zzhoujay.

the class GlideImageGetter method done.

@Override
public void done(Object from) {
    if (from instanceof ImageTarget) {
        ImageTarget imageTarget = (ImageTarget) from;
        DrawableWrapper drawableWrapper = (DrawableWrapper) imageTarget.urlDrawableWeakReference.get();
        if (drawableWrapper != null && imageTarget.config.cacheType >= CacheType.LAYOUT) {
            cache(imageTarget.holder.getSource(), drawableWrapper.getBounds());
        }
        targets.remove(imageTarget);
        loadedCount++;
        if (imageLoadNotify != null) {
            imageLoadNotify.done(loadedCount);
        }
    }
}
Also used : DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper)

Example 10 with DrawableWrapper

use of com.zzhoujay.richtext.drawable.DrawableWrapper in project RichText by zzhoujay.

the class ImageTarget method onLoadStarted.

@Override
public void onLoadStarted(Drawable placeholder) {
    super.onLoadStarted(placeholder);
    if (placeholder == null || !activityIsAlive()) {
        return;
    }
    DrawableWrapper drawableWrapper = urlDrawableWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    holder.setImageState(ImageHolder.ImageState.LOADING);
    drawableWrapper.setDrawable(placeholder);
    if (holder.getCachedBound() != null) {
        drawableWrapper.setBounds(holder.getCachedBound());
    } else {
        if (!config.autoFix && config.imageFixCallback != null) {
            config.imageFixCallback.onLoading(holder);
        }
        int width;
        int height = 0;
        if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
            width = getRealWidth();
            int ow = placeholder.getBounds().width();
            if (ow != 0) {
                height = placeholder.getBounds().height() * width / ow;
            }
            if (height == 0) {
                height = width / 2;
            }
        } else {
            width = (int) holder.getScaleWidth();
            height = (int) holder.getScaleHeight();
        }
        drawableWrapper.setBounds(0, 0, width, height);
    }
    resetText();
}
Also used : DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper)

Aggregations

DrawableWrapper (com.zzhoujay.richtext.drawable.DrawableWrapper)11 Bitmap (android.graphics.Bitmap)3 BitmapDrawable (android.graphics.drawable.BitmapDrawable)3 Rect (android.graphics.Rect)2 Drawable (android.graphics.drawable.Drawable)2 TextView (android.widget.TextView)2 BitmapTypeRequest (com.bumptech.glide.BitmapTypeRequest)1 DrawableTypeRequest (com.bumptech.glide.DrawableTypeRequest)1 GenericRequestBuilder (com.bumptech.glide.GenericRequestBuilder)1 GifTypeRequest (com.bumptech.glide.GifTypeRequest)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1