Search in sources :

Example 1 with DrawableWrapper

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

the class GlideImageGetter method getDrawable.

@Override
public Drawable getDrawable(ImageHolder holder, final RichTextConfig config, TextView textView) {
    final ImageTarget target;
    final GenericRequestBuilder load;
    DrawableTypeRequest dtr;
    DrawableWrapper drawableWrapper = new DrawableWrapper();
    byte[] src = Base64.decode(holder.getSource());
    if (src != null) {
        dtr = Glide.with(textView.getContext()).load(src);
    } else {
        dtr = Glide.with(textView.getContext()).load(holder.getSource());
    }
    if (holder.isGif()) {
        target = new ImageTargetGif(textView, drawableWrapper, holder, config, this);
        load = dtr.asGif();
    } else {
        target = new ImageTargetBitmap(textView, drawableWrapper, holder, config, this);
        load = dtr.asBitmap().atMost();
    }
    checkTag(textView);
    targets.add(target);
    if (!config.resetSize && holder.isInvalidateSize()) {
        load.override((int) holder.getScaleWidth(), (int) holder.getScaleHeight());
    }
    if (config.cacheType >= CacheType.LAYOUT) {
        Rect rect = loadCache(holder.getSource());
        if (rect != null) {
            holder.setCachedBound(rect);
            drawableWrapper.setBounds(rect);
        }
    } else {
        drawableWrapper.setBounds(0, 0, (int) holder.getScaleWidth(), (int) holder.getScaleHeight());
    }
    if (holder.getScaleType() == ImageHolder.ScaleType.CENTER_CROP) {
        if (holder.isGif()) {
            //noinspection ConstantConditions
            ((GifTypeRequest) load).centerCrop();
        } else {
            //noinspection ConstantConditions
            ((BitmapTypeRequest) load).centerCrop();
        }
    } else if (holder.getScaleType() == ImageHolder.ScaleType.FIT_CENTER) {
        if (holder.isGif()) {
            //noinspection ConstantConditions
            ((GifTypeRequest) load).fitCenter();
        } else {
            //noinspection ConstantConditions
            ((BitmapTypeRequest) load).fitCenter();
        }
    }
    textView.post(new Runnable() {

        @Override
        public void run() {
            load.placeholder(config.placeHolder).error(config.errorImage).into(target);
        }
    });
    drawableWrapper.setCallback(textView);
    return drawableWrapper;
}
Also used : BitmapTypeRequest(com.bumptech.glide.BitmapTypeRequest) Rect(android.graphics.Rect) GenericRequestBuilder(com.bumptech.glide.GenericRequestBuilder) DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper) GifTypeRequest(com.bumptech.glide.GifTypeRequest) DrawableTypeRequest(com.bumptech.glide.DrawableTypeRequest)

Example 2 with DrawableWrapper

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

the class ImageTarget method onLoadFailed.

@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
    super.onLoadFailed(e, errorDrawable);
    if (errorDrawable == null || !activityIsAlive()) {
        return;
    }
    DrawableWrapper drawableWrapper = urlDrawableWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    holder.setImageState(ImageHolder.ImageState.FAILED);
    holder.setException(e);
    drawableWrapper.setDrawable(errorDrawable);
    if (holder.getCachedBound() != null) {
        drawableWrapper.setBounds(holder.getCachedBound());
    } else {
        if (!config.autoFix && config.imageFixCallback != null) {
            config.imageFixCallback.onFailure(holder, e);
        }
        int width;
        int height = 0;
        if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
            width = getRealWidth();
            int ow = errorDrawable.getBounds().width();
            if (ow != 0) {
                height = errorDrawable.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();
    loadDone();
}
Also used : DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper)

Example 3 with DrawableWrapper

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

the class ImageTargetGif method onResourceReady.

@Override
public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) {
    if (!activityIsAlive()) {
        return;
    }
    DrawableWrapper drawableWrapper = urlDrawableWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    holder.setImageState(ImageHolder.ImageState.READY);
    gifDrawableSoftReference = new SoftReference<>(resource);
    Bitmap first = resource.getFirstFrame();
    holder.setSize(first.getWidth(), first.getHeight());
    drawableWrapper.setDrawable(resource);
    if (holder.getCachedBound() != null) {
        drawableWrapper.setBounds(holder.getCachedBound());
    } else {
        if (!config.autoFix && config.imageFixCallback != null) {
            config.imageFixCallback.onImageReady(holder, first.getWidth(), first.getHeight());
        }
        if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
            int width = getRealWidth();
            int height = (int) ((float) first.getHeight() * width / first.getWidth());
            drawableWrapper.setBounds(0, 0, width, height);
        } else {
            drawableWrapper.setBounds(0, 0, holder.getWidth(), holder.getHeight());
        }
        if (holder.isAutoPlay()) {
            resource.setCallback(this);
            resource.start();
            resource.setLoopCount(GlideDrawable.LOOP_FOREVER);
        }
    }
    resetText();
    loadDone();
}
Also used : Bitmap(android.graphics.Bitmap) DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper)

Example 4 with DrawableWrapper

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

the class AbstractImageLoader method onResourceReady.

@Override
public void onResourceReady(ImageWrapper imageWrapper) {
    if (imageWrapper == null) {
        onFailure(new RuntimeException("image decodeAsBitmap onFailure"));
        return;
    }
    DrawableWrapper drawableWrapper = drawableWrapperWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    TextView textView = textViewWeakReference.get();
    if (textView == null) {
        return;
    }
    imageWrapperWeakReference = new WeakReference<>(imageWrapper);
    holder.setImageState(ImageHolder.ImageState.READY);
    holder.setSize(imageWrapper.getWidth(), imageWrapper.getHeight());
    drawableWrapper.setDrawable(imageWrapper.getDrawable(textView.getResources()));
    if (holder.getCachedBound() != null) {
        drawableWrapper.setBounds(holder.getCachedBound());
    } else {
        if (!config.autoFix && config.imageFixCallback != null) {
            config.imageFixCallback.onImageReady(holder, imageWrapper.getWidth(), imageWrapper.getHeight());
        }
        if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
            int width = getRealWidth();
            int height = (int) ((float) imageWrapper.getHeight() * width / imageWrapper.getWidth());
            drawableWrapper.setBounds(0, 0, width, height);
        } else {
            drawableWrapper.setBounds(0, 0, (int) holder.getScaleWidth(), (int) holder.getScaleHeight());
        }
    }
    if (imageWrapper.isGif() && holder.isAutoPlay()) {
        imageWrapper.getAsGif().start(textView);
    }
    resetText();
    done();
}
Also used : DrawableWrapper(com.zzhoujay.richtext.drawable.DrawableWrapper) TextView(android.widget.TextView)

Example 5 with DrawableWrapper

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

the class AbstractImageLoader method onFailure.

@Override
public void onFailure(Exception e) {
    if (!activityIsAlive()) {
        return;
    }
    DrawableWrapper drawableWrapper = drawableWrapperWeakReference.get();
    if (drawableWrapper == null) {
        return;
    }
    holder.setImageState(ImageHolder.ImageState.FAILED);
    holder.setException(e);
    drawableWrapper.setDrawable(config.errorImage);
    if (holder.getCachedBound() != null) {
        drawableWrapper.setBounds(holder.getCachedBound());
    } else {
        if (!config.autoFix && config.imageFixCallback != null) {
            config.imageFixCallback.onFailure(holder, e);
        }
        int width;
        int height = 0;
        if (config.autoFix || holder.isAutoFix() || !holder.isInvalidateSize()) {
            width = getRealWidth();
            int ow = config.errorImage.getBounds().width();
            if (ow != 0) {
                height = config.errorImage.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();
    done();
}
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