How to rotate TextView without clipping its bounds?

前端 未结 4 1903
南旧
南旧 2021-02-08 10:41

I\'m trying to rotate my subclass of TextView using canvas.rotate():

canvas.save();

final int w = getWidth();
final int h = getHeight(         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 11:10

    At the first look solution provided by G. Blake Meike is a good way to start, however there is a lot of problems upcome.

    To overcome problems there is an another way. It's quite simple, but generally shouldn't be used (since I've just deleted unneccesary for me stuff from original android source). To take control over drawing of concrete View you can override drawChild method of it's parent (yes, you need your own subclass of ViewGroup or something more concrete like FrameLayout). Here is an example of my solution:

    @Override
    protected boolean drawChild(Canvas canvas, View child, long time) {
        //TextBaloon - is view that I'm trying to rotate
        if(!(child instanceof TextBaloon)) {
            return super.drawChild(canvas, child, time);
        }
    
        final int width = child.getWidth();
        final int height = child.getHeight();
    
        final int left = child.getLeft();
        final int top = child.getTop();
        final int right = left + width;
        final int bottom = top + height;
    
        int restoreTo = canvas.save();
    
        canvas.translate(left, top);
    
        invalidate(left - width, top - height, right + width, bottom + height);
        child.draw(canvas);
    
        canvas.restoreToCount(restoreTo);
    
        return true;
    }
    

    Main idea is that I'm granting non-clipped canvas to child.

提交回复
热议问题