Search in sources :

Example 1 with OnImageClickListener

use of com.zzhoujay.richtext.callback.OnImageClickListener in project RichText by zzhoujay.

the class CachedSpannedParser method handleImage.

private int handleImage(SpannableStringBuilder ssb, ImageGetterWrapper imageGetterWrapper, RichTextConfig config, boolean cached) {
    if (cached) {
        ClickableImageSpan[] cis = ssb.getSpans(0, ssb.length(), ClickableImageSpan.class);
        if (cis != null && cis.length > 0) {
            for (ClickableImageSpan ci : cis) {
                int start = ssb.getSpanStart(ci);
                int end = ssb.getSpanEnd(ci);
                ssb.removeSpan(ci);
                OnImageClickListener onImageClickListener = null;
                OnImageLongClickListener onImageLongClickListener = null;
                if (config.clickable > 0) {
                    onImageClickListener = config.onImageClickListener;
                    onImageLongClickListener = config.onImageLongClickListener;
                }
                Drawable drawable = imageGetterWrapper.getDrawable(ci.getSource());
                if (drawable == null) {
                    drawable = new ColorDrawable(Color.TRANSPARENT);
                }
                ClickableImageSpan nci = new ClickableImageSpan(drawable, ci, onImageClickListener, onImageLongClickListener);
                ssb.setSpan(nci, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return cis.length;
        }
    } else if (!config.noImage) {
        ImageSpan[] iss = ssb.getSpans(0, ssb.length(), ImageSpan.class);
        if (iss != null && iss.length > 0) {
            ArrayList<String> imageUrls = new ArrayList<>(iss.length);
            for (int i = 0; i < iss.length; i++) {
                ImageSpan imageSpan = iss[i];
                String imageUrl = imageSpan.getSource();
                imageUrls.add(imageUrl);
                int start = ssb.getSpanStart(imageSpan);
                int end = ssb.getSpanEnd(imageSpan);
                ClickableSpan[] clickableSpans = ssb.getSpans(start, end, ClickableSpan.class);
                if (clickableSpans != null && clickableSpans.length != 0) {
                    for (ClickableSpan cs : clickableSpans) {
                        ssb.removeSpan(cs);
                    }
                }
                OnImageClickListener onImageClickListener = null;
                OnImageLongClickListener onImageLongClickListener = null;
                if (config.clickable > 0) {
                    onImageClickListener = config.onImageClickListener;
                    onImageLongClickListener = config.onImageLongClickListener;
                }
                Drawable drawable = imageGetterWrapper.getDrawable(imageUrl);
                if (drawable == null) {
                    drawable = new ColorDrawable(Color.TRANSPARENT);
                }
                ClickableImageSpan cacheImageSpan = new ClickableImageSpan(drawable, imageUrls, i, onImageClickListener, onImageLongClickListener);
                ssb.removeSpan(imageSpan);
                ssb.setSpan(cacheImageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return iss.length;
        }
    }
    return 0;
}
Also used : OnImageClickListener(com.zzhoujay.richtext.callback.OnImageClickListener) ColorDrawable(android.graphics.drawable.ColorDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) ArrayList(java.util.ArrayList) OnImageLongClickListener(com.zzhoujay.richtext.callback.OnImageLongClickListener) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan) ClickableSpan(android.text.style.ClickableSpan) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan) ImageSpan(android.text.style.ImageSpan)

Aggregations

ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 ClickableSpan (android.text.style.ClickableSpan)1 ImageSpan (android.text.style.ImageSpan)1 OnImageClickListener (com.zzhoujay.richtext.callback.OnImageClickListener)1 OnImageLongClickListener (com.zzhoujay.richtext.callback.OnImageLongClickListener)1 ClickableImageSpan (com.zzhoujay.richtext.spans.ClickableImageSpan)1 ArrayList (java.util.ArrayList)1