How to use Android marktipsview text identification control

Time:2021-11-30

This example shares the specific code of Android marktipsview text identification control for your reference. The specific contents are as follows

Effect preview

use

The use method is the same as that of textview. The background setting is directly setbackgroundcolor

Source code:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

/**
 *Draw mark annotation
 */
public class MarkTipsView extends AppCompatTextView {

    /**
     *Brush
     */
    private Paint paint;
    /**
     *Width
     */
    private int width;
    /**
     *Height
     */
    private int height;
    /**
     *Identification width
     */
    private int markWidth = 12;
    /**
     *Sign height
     */
    private int markHeight = 12;
    /**
     *Fillet size
     */
    private float radius = 10;
    /**
     *Fill color
     */
    private int solid = Color.parseColor("#F1AA44");

    public MarkTipsView(Context context) {
        super(context);
        initAttributeSet(context, null);
    }

    public MarkTipsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttributeSet(context, attrs);
    }

    public MarkTipsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttributeSet(context, attrs);
    }

    private void initAttributeSet(Context context, AttributeSet attrs) {
        paint = new Paint();
        paint.setDither(true);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        TypedArray array = context.obtainStyledAttributes(attrs,new int[]{android.R.attr.background});
        solid =  array.getInt(0,solid);
        array.recycle();
    }

    @Override
    public void setBackgroundColor(int color) {
        solid = color;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom() + markHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(solid);
        //Rectangle
        Path path = new Path();
        RectF rect = new RectF(0, 0, width, height - markHeight);
        path.addRoundRect(rect, radius, radius, Path.Direction.CCW);
        //Triangle
        Path onePath = new Path();
        onePath.lineTo(width / 2 - markWidth, rect.height());
        onePath.lineTo(width / 2, height);
        onePath.lineTo(width / 2 + markWidth, rect.height());
        onePath.close();
        path.addPath(onePath);
        //Draw path
        canvas.drawPath(path, paint);
        super.onDraw(canvas);
        //Redraw text
        setText(getText());
    }

    /**
     *Gets the height of the bottom identification arrow
     * @return
     */
    public int getMarkHeight() {
        return markHeight;
    }

    /**
     *Sets the height of the bottom identification arrow
     * @param markHeight
     */
    public void setMarkHeight(int markHeight) {
        this.markHeight = markHeight;
        invalidate();
    }

    /**
     *Gets the width of the bottom identification arrow
     * @return
     */
    public int getMarkWidth() {
        return markWidth;
    }

    /**
     *Sets the width of the bottom identification arrow
     * @param markWidth
     */
    public void setMarkWidth(int markWidth) {
        this.markWidth = markWidth;
        invalidate();
    }

    /**
     *Get fillet size
     * @return
     */
    public float getRadius() {
        return radius;
    }

    /**
     *Set fillet size
     * @param radius
     */
    public void setRadius(float radius) {
        this.radius = radius;
        invalidate();
    }

}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.