android 绘制带尺寸的矩形并在运行时修改它们

android draw rectangle with dimensions and modify them in runtime

我正在为 android 开发工程应用程序。问题是我需要绘制:矩形、由矩形组成的图形及其尺寸。然后,如果你触摸一维的一个极端,你就可以使矩形的那个维度变长或变短。

我正在实施下一个方案以实现我的目标:

class DrawFigureWithDimensions extends View{
    // implementation of the draw methods
    // ...
    class Rectangles{
        // characterization of the attributes needed for each "rectangle"
        // ...
        class DimensionPositionType{ ... }
        class DimensionExtremeType{ ... }
}
class DrawRectangleWithDimension extends DrawFiguresWithDimensions{ ... }
class DrawBoxWithDimension extends DrawFiguresWithDimensions{ ... }
...

然后,我在实现 DimensionPositionType 和 DimensionExtremeType 的内部 classes 时遇到问题,我找不到如何以合适的方式声明它们。我需要能够在扩展的 classes 中决定,例如 DrawRectangleWithDimensions,我需要什么类型的极端维度:固定的或可移动的。像这样:

public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {

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

public void setFigure(double width, double height) {
    figureRectangles = new Rectangle[1];
    figureRectangles[0] = new Rectangle(0, 0, width, height);
    figureRectangles[0].setHorizontalDimensionLeftExtremeType(FIXED);
    figureRectangles[0].setHorizontalDimensionRightExtremeType(MOVABLE);
}

例如,这是我为内部 class DimensionExtremeType:

编写的代码
    class DimensionExtremeType{
        boolean FIXED;
        boolean MOVABLE;
        DimensionExtremeType(String arg){
            if(arg == "FIXED"){
                setFixedExtreme();
            }else if(arg == "MOVABLE"){
                setMovableExtreme();
            }
        }
        public void setFixedExtreme(){
            FIXED = true;
            MOVABLE = false;
        }
        public void setMovableExtreme(){
            FIXED = false;
            MOVABLE = true;
        }
        public String getDimensionExtremeType(){
            if(FIXED==true){
                return "FIXED";
            }else if(MOVABLE==true){
                return "MOVABLE";
            }else {
                return null;
            }
        }
    }

我只知道 class 枚举的存在,它解决了我的设计问题。这是一种更容易执行的方法。根据我在问题的措辞中写的例子,这是使用 class 枚举的代码:

static class Rectangle{
    // ...
    DimensionExtremeType horizontalDimensionLeftExtremeType;
    DimensionExtremeType horizontalDimensionRightExtremeType;
    DimensionExtremeType verticalDimensionUpperExtremeType;
    DimensionExtremeType verticalDimensionLowerExtremeType;    
    // =-=-=-=     DIMENSION POSITION TYPES      =-=-=-=
    public enum DimensionExtremeType{FIXED, MOVABLE}

}

然后,在扩展的 classes 中,例如 DrawRectanglesWithDimensions:

public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {

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

    public void setFigure(float  width, float height) {
        figureRectangles = new Rectangle[1];
        figureRectangles[0] = new Rectangle(0, 0, width, height);
        figureRectangles[0].horizontalDimensionLeftExtremeType = Rectangle.DimensionExtremeType.FIXED;
        figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
        figureRectangles[0].verticalDimensionUpperExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
        figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
    }
}