2012-08-03 130 views
0

我正在寻求减少Android项目中的冗余代码。让我告诉你我已经拥有了什么。我有两个主要的观点组。一个叫做MapView,它是一个包含可以缩放,平移等位图的视图。另一个称为StepInfoView,它是一个包含静态数据的视图,例如坐标数组和关于这些坐标的信息。这个视图也是放大并且以与MapView相同的方式移动,但是,放大的比例因子和数量需要独立于MapView。Java继承:抽象类中的“Casting”变量为静态

StepInfoView扩展了几个不同的类(总共6个),所有这些类都需要同步(坐标绘制在一个坐标之间的线被绘制在另一个上,等等)。

所以,这里是我的代码的要点,目前:

public class MapView extends View 
{ 
    protected float scale = 1; 

    protected class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener 
    { 
     @Override 
     public boolean onScale(ScaleGestureDetector detector) 
     { 
      scale *= detector.getScaleFactor()); 
      return true; 
     } 

    } 

    } 
... 

public class StepInfoView extends View 
{ 
    static protected float scale = 1; 
    protected class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener 
    { 
     @Override 
     public boolean onScale(ScaleGestureDetector detector) 
     { 
      scale *= detector.getScaleFactor()); 
      return true; 
     } 

    } 
} 
... 
public class StepView extends StepInfoView 
{ 
    // Custom onDraw, same scale as PathView 
} 
public class PathView extends StepInfoView 
{ 
    // Custom onDraw, same scale as StepView 
} 

正如你看到的,onScales是相同。我想创建一个抽象类来保存缩放功能,让我们称之为RootView。 MapView和StepInfoView都具有比例尺,但是我无法在RootView中进行比例缩放,因为MapView比例尺必须独立于StepInfoView。这里是我知道将工作的解决方案,但我不喜欢getter和setter的混乱:

abstract class RootView extends View 
{ 
    abstract protected float getScale(); 

    abstract protected void setScale(float s); 
    protected class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener 
    { 
     @Override 
     public boolean onScale(ScaleGestureDetector detector) 
     { 
     setScale(getScale() * detector.getScaleFactor()); 
     return true; 
     } 
    } 
} 
... 
public class MapView extends RootView 
{ 
    public float scale = 1; 
    protected float getScale(){ 
     return scale; 
    } 

    protected void setScale(float s){ 
     scale = s; 
    } 
} 

public class StepInfoView extends RootView 
{ 
    public static float scale = 1; 
    protected float getScale(){ 
     return scale; 
    } 

    protected void setScale(float s){ 
     scale = s; 
    } 
} 
public class StepView extends StepInfoView 
{ 
    // Custom onDraw, same scale as PathView 
} 
public class PathView extends StepInfoView 
{ 
    // Custom onDraw, same scale as StepView 
} 

这工作正是我想要的工作,但再次,有重复相同的代码( getter和setter以及MapView和StepInfoView中的“scale”变量)。

我希望我能做的是在RootView中声明一个非静态的缩放,并在StepInfoView中将它“静态”转换为静态。这不可能...

你能帮我吗?

回答

0

为什么不把scale字段变成RootView类?

更新:明白了。我认为有一个聚合的地方:为stepinfo scale值做一些对象容器,并通过StepInfoView的。无论如何,我认为最简单的方式就是使用getters。

+0

我需要在StepInfoView中保持静态。如果我在RootView中将其设为静态,那么StepInfoViews和MapViews将不会相互独立地进行缩放。 – androidnotgenius 2012-08-03 11:37:54

+0

“为stepinfo scale值创建一些对象容器,并通过StepInfoView的” “对不起,但我不明白你的建议。 – androidnotgenius 2012-08-03 11:42:15

1

浮动花费大约4个字节。有另一个类将使用比这更多的内存。由于对象通常是8字节对齐的,所以它甚至可能不会使对象变小。

如果你想避免复杂性,我会离开字段非静态。

你可以有

class RootView { 
    protected final float[] scale; 

    RootView() { 
     scale = new float[] { 1.0f }; 
    } 

    protected RootView(float[] scale) { 
     this.scale = scale; 
    } 

    public boolean onScale(ScaleGestureDetector detector) { 
     scale[0] *= detector.getScaleFactor(); 
     return true; 
    } 
} 

class StepInfoView extends RootView { 
    static final float[] scale = { 1.0f }; 

    StepInfoView() { 
     super(scale); 
    } 
+0

我不关心这个应用程序的内存。如果将字段保留为非静态,则需要更新扩展了StepBaseInfo的所有类,如果缩放更改。 – androidnotgenius 2012-08-03 11:39:27

+0

在我的编辑中添加一个替代方案。 – 2012-08-03 11:45:57

+0

因此,这只适用于规模,但我也有一个变量“位置”,为MapView和StepInfoViews平移的量,它以与scale相同的方式进行处理(对于Map而言是非静态的,对于StepInfo是静态的)。我可以在MapView和StepInfoView中做super.pan(x,y)吗? – androidnotgenius 2012-08-03 11:48:51

0

移动的规模场监听,并提供获取和设置。

在该视图中,保留对侦听器的引用,以便可以访问该比例。

对于MapView,为每个视图实例注册一个不同的侦听器,以使它们具有不同的比例。

对于StepInfoView,为每个视图实例注册相同的侦听器,以使它们具有相同的缩放比例。您可以将侦听器的引用存储在静态字段中。

+0

我真的想避免吸气剂和吸附剂.. – androidnotgenius 2012-08-03 11:53:02

+0

如果你有两个对象进行通信,你需要方法来让他们进行通信。由于您只有一个值可以从侦听器与视图进行通信,您可以在两者之间共享一个字段(正如您所看到的那样易碎且会导致重复),或者您有一个将值推入视图的函数(一个setter)或一个函数,其中的值是从侦听器(一个getter或一个公共字段)中拉取的。从同一个监听器获取多个视图比通过监听器推送多个视图更简单。 – 2012-08-03 11:56:27