简体   繁体   中英

Is it possible to invalidate Text only, but not background image on a custom View on android?

My CustomView draws some polygons and the title text is located at the centre of a view. As the application runs, I need to change the title text of these views frequently. Of course, I can invalidate after setting a new text so that the application will redraw everything - polygons, colors, and finally the new text - but I am just wondering if there is a clever way that only invalidates the title text, leaving all background polygons untouched. The current code looks like this:

public CustomView extends View {

    private Path mPolygon1;
    private Path mPolygon2;
    // .. more polygons

    private String mTitleText;

    @Override
    protected void onDraw(Canvas canvas) {

        // Draws all images
        canvas.drawPath(mPolygon1, mPaint);
        canvas.drawPath(mPolygon2, mPaint);
        // ... more polygon drawing

        canvas.drawText(mTitleText, 0, mTitleText.length(), mTextPaint);
    }

    public void setTitle(String title) {
        mTitleText = title;
        invalidate();           // more clever way????
    }
}

You can use invalidate(Rect) or invalidate(left, top, right, bottom)

Here is a link for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM