扩展EditText/TextView固定位置显示文字

文章目录

      • 扩展EditText/TextView固定位置显示文字
        • 一、问题描述
        • 二、解决方案
        • TextView的onDraw源码


扩展EditText/TextView固定位置显示文字

一、问题描述

EditText/TextView等控件在设置单行显示后,文字超过控件长度后可滑动显示。扩展EditText/TextView时,自定义绘制的文字也会在上述条件下跟随滑动。

二、解决方案

由TextView源码知文字滚动和Scroller滚动量有关,所以要固定文字可以在onDraw中设置canvas偏移Scroller滚动量

canvas?.save()//保存canvas状态
canvas?.translate((RecF?.left ?: 0f) + scrollX, dy)//(RecF?.left ?: 0f)是自定义文字的左边位置
textLayout?.draw(canvas)//通过BoringLayout/StaticLayout/DynamicLayout绘制文字
canvas?.restore()//还原canvas状态

TextView的onDraw源码

TextView中固定Drawable绘制方法

 @Override
    protected void onDraw(Canvas canvas) {
        restartMarqueeIfNeeded();

        // Draw the background for this view
        super.onDraw(canvas);

        final int compoundPaddingLeft = getCompoundPaddingLeft();
        final int compoundPaddingTop = getCompoundPaddingTop();
        final int compoundPaddingRight = getCompoundPaddingRight();
        final int compoundPaddingBottom = getCompoundPaddingBottom();
        final int scrollX = mScrollX;
        final int scrollY = mScrollY;
        final int right = mRight;
        final int left = mLeft;
        final int bottom = mBottom;
        final int top = mTop;
        final boolean isLayoutRtl = isLayoutRtl();
        final int offset = getHorizontalOffsetForDrawables();
        final int leftOffset = isLayoutRtl ? 0 : offset;
        final int rightOffset = isLayoutRtl ? offset : 0;

        final Drawables dr = mDrawables;
        if (dr != null) {
            /*
             * Compound, not extended, because the icon is not clipped
             * if the text height is smaller.
             */

            int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
            int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;

            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
            // Make sure to update invalidateDrawable() when changing this code.
            if (dr.mShowing[Drawables.LEFT] != null) {
                canvas.save();
                canvas.translate(scrollX + mPaddingLeft + leftOffset,//设置滚动距离为偏移量
                        scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightLeft) / 2);
                dr.mShowing[Drawables.LEFT].draw(canvas);
                canvas.restore();
            }

            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
            // Make sure to update invalidateDrawable() when changing this code.
            if (dr.mShowing[Drawables.RIGHT] != null) {
                canvas.save();
                canvas.translate(scrollX + right - left - mPaddingRight
                        - dr.mDrawableSizeRight - rightOffset,
                         scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2);
                dr.mShowing[Drawables.RIGHT].draw(canvas);
                canvas.restore();
            }
        }

你可能感兴趣的:(自定义控件,自定义控件,EditText,TextView,固定文字)