Rotating an image around a specified point doesn't work! (Android)

后端 未结 2 684
攒了一身酷
攒了一身酷 2021-01-16 08:42

I\'m rotating an ImageView with postRotate(float degrees, float px, float py), setting px and py to a few differnt values including (0,0) and (imgView.getHeight(),imgView.ge

2条回答
  •  囚心锁ツ
    2021-01-16 09:27

    I am using a custom ImageView where I set the angle rotation.

    public class CompassImage extends ImageView {
        private float angleRotation;
    
        public CompassImage(Context context) {
            super(context);
        }
    
        public CompassImage(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CompassImage(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public void setAngleRotation(float angleRotation) {
            this.angleRotation = angleRotation;
            invalidate();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            Rect clipBounds = canvas.getClipBounds();
            canvas.save();
            canvas.rotate(angleRotation, clipBounds.exactCenterX(), clipBounds.exactCenterY());
            super.onDraw(canvas);
            canvas.restore();
        }
    }
    

    If you play around with clipBounds you may find that helpful.

提交回复
热议问题