Android Open Source - android-bubble-text Bubble Span Impl






From Project

Back to project page android-bubble-text.

License

The source code is released under:

MIT License

If you think the Android project android-bubble-text listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.oliveira.bubble;
//ww  w  . j ava2s.c o m
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.text.Layout;
import android.text.Spannable;
import android.text.style.ReplacementSpan;

import java.util.ArrayList;

public class BubbleSpanImpl extends ReplacementSpan implements BubbleSpan {

    public Object data;
    public AwesomeBubble bubble;
    private ChipsEditText et;
    private float baselineDiff;

    public BubbleSpanImpl(AwesomeBubble bubble) {
        this.bubble = bubble;
    }

    public BubbleSpanImpl(AwesomeBubble bubble, ChipsEditText et) {
        this.bubble = bubble;
        this.et = et;
    }

    @Override
    public void draw(Canvas canvas,
                     CharSequence text,
                     int start,
                     int end,
                     float x,
                     int top,
                     int y,
                     int bottom,
                     Paint paint) {

        canvas.save();

        baselineDiff = lineCorrectionLogic(start, et, bubble);
        float transY = top - baselineDiff;

        canvas.translate(x, transY);
        bubble.draw(canvas);
        canvas.restore();
    }

    @Override
    public void redraw(Canvas canvas) {

        if (et.getScrollY() != 0) {
            return;
        }

        int pos = et.getText().getSpanStart(this);
        if (pos == -1) {
            return;
        }

        Layout layout = et.getLayout();
        int line = layout.getLineForOffset(pos);
        float x = layout.getPrimaryHorizontal(pos);
        float y = layout.getLineTop(line);
        x += et.getPaddingLeft();
        y += et.getPaddingTop();

        canvas.save();
        canvas.translate(x, y - baselineDiff);
        bubble.draw(canvas);
        canvas.restore();
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return bubble.getWidth();
    }

    @Override
    public void setPressed(boolean value, Spannable s) {
        bubble.setPressed(value);
    }

    @Override
    public void resetWidth(int width) {
        bubble.resetWidth(width);
    }

    @Override
    public ArrayList<Rect> rect(ILayoutCallback callback) {
        ArrayList<Rect> result = new ArrayList<Rect>();
        Rect position = new Rect(bubble.rect());
        int spanStart = callback.getSpannable().getSpanStart(this);
        Point startPoint = callback.getCursorPosition(spanStart);
        position.offset(startPoint.x, startPoint.y);
        result.add(position);
        return result;
    }

    @Override
    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public Object data() {
        return data;
    }

    public static float lineCorrectionLogic(int start, ChipsEditText et, AwesomeBubble bubble) {

        float baselineDiff;
        if (et != null) {

            float multiplier = -1.8f;
            if (et.getLayout() != null && et.getLayout().getLineForOffset(start) == 0) {
                baselineDiff = ((float)bubble.style.bubblePadding) * multiplier;
            } else {
                multiplier += 1.0f;
                baselineDiff = ((float)bubble.style.bubblePadding) * multiplier;
            }

        } else {
            baselineDiff = ((float)bubble.style.bubblePadding) * 0.6f;
        }

        return baselineDiff;
    }
}




Java Source Code List

com.asolutions.widget.RowLayout.java
com.oliveira.bubble.AutoCompleteManager.java
com.oliveira.bubble.AutoCompletePopover.java
com.oliveira.bubble.AwesomeBubble.java
com.oliveira.bubble.BubbleSpanImpl.java
com.oliveira.bubble.BubbleSpan.java
com.oliveira.bubble.BubbleStyle.java
com.oliveira.bubble.ChipsEditText.java
com.oliveira.bubble.ChipsTextView.java
com.oliveira.bubble.CursorDrawable.java
com.oliveira.bubble.DefaultBubbles.java
com.oliveira.bubble.ILayoutCallback.java
com.oliveira.bubble.Linkify.java
com.oliveira.bubble.MultilineEditText.java
com.oliveira.bubble.Regex.java
com.oliveira.bubble.TappableSpan.java
com.oliveira.bubble.Utils.java