我需要获取画布内部的图像边界以检测是否已被触摸,但是如果我旋转画布,则getbounds()保持相同的值,如何获得画布后的正确值旋转?
//CODE ON VIEW CLASS:
canvas.save();
canvas.rotate(rotation, Xpos, Ypos);
secHandImg.setBounds(left,top,right,bottom);
secHandImg.setAntiAlias(true);
secHandImg.draw(canvas);
circleHandImg.setBounds(left,top,right,bottom);
circleHandImg.setAntiAlias(true);
circleHandImg.draw(canvas);
canvas.restore();
//CODE ON FRAGMENT CLASS:
public boolean onTouch(View view, MotionEvent event) {
Rect imageBounds = MyClass.secHandImg.getBounds();
int action = event.getAction();
final int x = (int)event.getX();
final int y = (int)event.getY();
if(action == MotionEvent.ACTION_DOWN) {
if(x >= imageBounds.left && x < (imageBounds.left + imageBounds.width())
&& y >= imageBounds.top && y < (imageBounds.top + imageBounds.height())){
//THIS DON´T WORK IF CANVAS ROTATES
imageBoundsTouch = true;
}
}
这是一张图片,可以更好地解释我的问题:
答案 0 :(得分:1)
您可以使用三角函数来获取旋转矩形的边界:
function BBoxDimensions(width,height,radianAngle){
var c = Math.abs(Math.cos(radianAngle));
var s = Math.abs(Math.sin(radianAngle));
return({ width: height * s + width * c, height: height * c + width * s });
}
您可以使用trig来获取旋转的边界框的XY:
// where cx/cy is the center of rotation of the target
// and startingX/startingY is the starting xy of the unrotated target
var x1 = startingX - cx;
var y1 = startingY - cy;
newX =cx+ x1*Math.cos(angle) - y1*Math.sin(angle);
newY =cy+ y1*Math.cos(angle) + x1*Math.sin(angle);
答案 1 :(得分:0)
重新计算onOrientationChanged()
的边界值