Search in sources :

Example 1 with ClickableImageSpan

use of com.zzhoujay.richtext.spans.ClickableImageSpan in project RichText by zzhoujay.

the class LongClickableLinkMovementMethod method onTouchEvent.

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();
        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);
        LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);
        if (link.length != 0) {
            long currTime = System.currentTimeMillis();
            LongClickableSpan l = link[0];
            int ls = buffer.getSpanStart(l);
            int le = buffer.getSpanEnd(l);
            // 判断点击的点是否在Image范围内
            ClickableImageSpan[] is = buffer.getSpans(ls, le, ClickableImageSpan.class);
            if (is.length > 0) {
                if (!is[0].clicked(x)) {
                    Selection.removeSelection(buffer);
                    return false;
                }
            } else if (off < layout.getOffsetToLeftOf(ls) || off > layout.getOffsetToLeftOf(le + 1)) {
                // 判断点击位置是否在链接范围内
                Selection.removeSelection(buffer);
                return false;
            }
            if (action == MotionEvent.ACTION_UP) {
                // 如果按下时间超过500毫秒,触发长按事件
                if (currTime - lastTime > MIN_INTERVAL) {
                    if (!l.onLongClick(widget)) {
                        // onLongClick返回false代表事件未处理,交由onClick处理
                        l.onClick(widget);
                    }
                } else {
                    l.onClick(widget);
                }
            } else {
                Selection.setSelection(buffer, ls, le);
            }
            lastTime = currTime;
            return true;
        } else {
            Selection.removeSelection(buffer);
            return false;
        }
    }
    return super.onTouchEvent(widget, buffer, event);
}
Also used : LongClickableSpan(com.zzhoujay.richtext.spans.LongClickableSpan) Layout(android.text.Layout) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan)

Example 2 with ClickableImageSpan

use of com.zzhoujay.richtext.spans.ClickableImageSpan 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

ClickableImageSpan (com.zzhoujay.richtext.spans.ClickableImageSpan)2 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Layout (android.text.Layout)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 LongClickableSpan (com.zzhoujay.richtext.spans.LongClickableSpan)1 ArrayList (java.util.ArrayList)1