Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas.drawRect is not work in TextView's onDraw when set gravity

Tags:

android

I have a custom view extends form TextView,and write some code in onDraw():

@Override
protected void onDraw(Canvas canvas) {
    if (mTextShader == null) {
        createShader();
    }
    shaderMatrix.setTranslate(mProgressWidth,0);
    mTextShader.setLocalMatrix(shaderMatrix);

    Paint paint = new Paint();
    paint.setShader(mBgShader);
    canvas.drawRect(0,0,mProgressWidth,getBottom(),paint);
    getPaint().setShader(mTextShader);
    super.onDraw(canvas);
}

It looks like this: enter image description here Just set some shader,and draw a rect,it works fine if I don't use

android:gravity="center"

and

android:singleLine="true"

together,(just one of them is pretty good,)

if only use this property,these shaders and rect are gone!

why?

like image 492
RainFool Avatar asked Oct 30 '22 02:10

RainFool


1 Answers

Before you draw in the canvas, translate the scrolled XY distance

canvas.translate(getScrollX(), getScrollY());
canvas.drawRect(0,0, getWidth(), getHeight(), borderPaint);
like image 91
BabyishTank Avatar answered Nov 11 '22 15:11

BabyishTank