ITKeyword,专注技术干货聚合推荐

注册 | 登录

java - Updating view bounds after canvas transformations (rotate, scale, transform)

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:java.lang.UnsupportedOperationException android.view.GLES20Canvas.clipPath(GLES20Canvas.java:451

             我在绘制图像的时候报了这个错,这是Android ICS系统版本的错误,跟硬件加速有关。             在程序里设置下 mImageView.setLayerType(View.LAY

I am trying to figure out how to update the bound of my view to match the canvas after I applied changes to rotate, scale and transform.

This is my class for a custom textview. I have rotate working like I want but the view is not wrapping around my canvas.

public class RDTextView extends TextView {

public int angle = 0;

public RDTextView(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas)
{ // Save the current matrix
    canvas.save();
    // Rotate this View at its center
    canvas.rotate(angle,this.getWidth()/2, this.getHeight()/2);
    // Draw it --- will add scale
    super.onDraw(canvas);
    // Restore to the previous matrix
    canvas.restore();
}

My issue is that I have a border around the view but after I change the angle it doesn't follow the canvas shape.

The border is unchanged after rotation but I would like to update it to match the content.

java android android-canvas android-ui
|
  this question
asked Sep 4 '13 at 23:16 Bryan Williams 369 5 20

 | 

1 Answers
1

I just made following code for my needs.

    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JLabel;


    public class RotatedLabel extends JLabel {

        //we need to remember original size
        private int originalWidth = 0;
        private int originalHeight = 0;

        //I do rotation around the center of label, so save it too
        private int originX = 0;
        private int originY = 0;

        public RotatedLabel(String text, int horizotalAlignment) {
            super(text, horizotalAlignment);
        }

        @Override
        public void setBounds(int x, int y, int width, int height) {
            //save new data
            originX = x + width / 2;
            originY = y + height / 2;
            originalWidth = width;
            originalHeight = height;
            super.setBounds(x, y, width, height);
        };

        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;

            int w2 = getWidth() / 2;
            int h2 = getHeight() / 2;

            double angle = -Math.PI / 2;//it's our angle

            //now we need to recalculate bounds
            int newWidth = (int)Math.abs(originalWidth * Math.cos(angle) + originalHeight * Math.sin(angle));
            int newHeight = (int)Math.abs(originalWidth * Math.sin(angle) + originalHeight * Math.cos(angle));

            g2d.rotate(angle, w2, h2);
            super.paint(g);

            //set new bounds
            super.setBounds(originX - newWidth / 2, originY - newHeight / 2, newWidth, newHeight);
        }
    }

|
  this answer
answered Oct 3 '13 at 14:42 Vladimir Vlasov 300 1 4 15

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。