当我在矩形类中缩放时,如何计算矩形的高度和宽度?

时间:2018-04-24 16:43:21

标签: android drawing zoom gesturedetector

当我在矩形类中缩放时,如何计算矩形的高度和宽度?

这是我的类矩形,我可以在scale方法中添加哪些内容来更改大小?感谢。

我对代码进行了一些编辑:     public class Rectangle extends Graphic {

public Rectangle(float centerX, float centerY, float width, float height) {
    super(centerX, centerY, width, height);
}

@Override
protected void onDraw(Canvas canvas) {
    float left = getCenterX() - (getWidth()*getScaleX())/2 ;
    float top = getCenterY() - (getHeight()*getScaleY())/2 ;
    float right = getCenterX() + (getWidth()*getScaleX())/2 ;
    float bottom = getCenterY() + (getHeight()*getScaleY())/2 ;
        Paint paint = new Paint();
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1.0f);
        canvas.drawRect(left, top, right, bottom, paint);
}

@Override
protected void onScale(float scaleFactorX, float scaleFactorY, float focusX, float focusY) {
    float newWidth = getWidth()*scaleFactorX;
    float newHeight = getHeight()*scaleFactorY;
    setWidth(newWidth);
    setHeight(newHeight);
}

@Override
protected void onDrag(float dx, float dy) {

}

@Override
protected void onSelectChange(boolean selected) {

    if (selected)
        getPaint().setColor(Color.GRAY);

}

@Override
protected void onClickEvent(float x, float y) {
    float left = getCenterX() - (getWidth()*getScaleX())/2 ;
    float top = getCenterY() - (getHeight()*getScaleY())/2 ;
    float right = getCenterX() + (getWidth()*getScaleX())/2 ;
    float bottom = getCenterY() + (getHeight()*getScaleY())/2 ;

    if(x > left && x < right && y > top && y < bottom)
        setSelected(true);
    else
        setSelected(false);
}

@Override
protected void onLongPressEvent(float x, float y) {

}

@Override
protected void onRotate(double angle) {

}
}

0 个答案:

没有答案