用户决定加载哪个类Android

时间:2012-01-07 22:09:32

标签: android class

编辑:我会尽量让对比更加清楚。 我有两个实时壁纸,一个被称为过去,被称为未来,我想要做的是将两者放入一个livewallpaper(两个一个交易)但让用户决定他们想要加载哪一个。 我是如何为一个设置的(让我们说过去)我在一个名为Past的类中运行onDraw方法(它没有任何暗示)只是通过onDraw并将整个livewallpaper togeather。 在livewallpaper引擎中我有这个。

 public class ThePastActivity extends WallpaperService {
@Override
public Engine onCreateEngine() {
    return new ThePastActivityEngine();
}


class ThePastActivityEngine extends Engine {

    private Past _past;

    public ThePastActivityEngine() {
        this._past = new Past();
        this._past.initialize(getBaseContext(), getSurfaceHolder());
    }


    @Override
    public void onVisibilityChanged(boolean visible) {
        if(visible){
            this._past.render();
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {        
        super.onSurfaceChanged(holder, format, width, height);          
    }


    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {            
        super.onSurfaceCreated(holder);
        this._past.start();
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {          
        super.onSurfaceDestroyed(holder);
        this._past.stop();
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,float xStep, float yStep, int xPixels, int yPixels) {
     this._past.drawXOff = Math.round((this._blimp.theBackgroundImage.getWidth() - initFrameParamsWidth()) * -xOffset);
     this._past.drawYOff = Math.round((this._blimp.theBackgroundImage.getHeight() - initFrameParams()) * -yOffset);

        this._past.render();

 }

}

现在我有两个而不是一个。新的称为Future,所以我现在就这样:

   public class ThePastActivity extends WallpaperService {

    public static final String SHARED_PREFS_NAME = "livewallpapersettings";
public static final String PREFERENCE_BACK = "livewallpaper_back";         

@Override
public Engine onCreateEngine() {
    return new ThePastActivityEngine();
}


class ThePastActivityEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener{
    private SharedPreferences prefs;
            private String whichEra;
    private Past _past;
            private Future _future;

            //make a new name  ChooseEra and let it become either Past or Future
            private ChooseEra _chooseEra;

    public ThePastActivityEngine() {
        this._chooseEra = new ChooseEra();
        this._past.initialize(getBaseContext(), getSurfaceHolder());
                    prefs = TheSteampunkCityActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
        prefs.registerOnSharedPreferenceChangeListener(this);
        onSharedPreferenceChanged(prefs, null);
    }

            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

        whichEra=(prefs.getString(PREFERENCE_BACK, "past"));
        // from here I want to get either the "past" or "future" 
                    // make the ChooseEra to be either Past or Future
                   // and use that for this livewallpaper engine instead
    }


    @Override
    public void onVisibilityChanged(boolean visible) {
        if(visible){
            this._chooseEra.render();
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {        
        super.onSurfaceChanged(holder, format, width, height);          
    }


    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {            
        super.onSurfaceCreated(holder);
        this._past.start();
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {          
        super.onSurfaceDestroyed(holder);
        this._chooseEra.stop();
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,float xStep, float yStep, int xPixels, int yPixels) {
     this._chooseEra.drawXOff = Math.round((this._chooseEra.theBackgroundImage.getWidth() - initFrameParamsWidth()) * -xOffset);
     this._chooseEra.drawYOff = Math.round((this._chooseEra.theBackgroundImage.getHeight() - initFrameParams()) * -yOffset);

        this._chooseEra.render();

 }

}

所以chooseEra必须成为Future或Past,所以它只读取两个类中的一个,并通过引擎传递参数。 我遇到的问题是让ChooseEra成为过去或未来。通常使用方法很容易,但这是我第一次尝试更改类名,所以当我把

private ChooseEra _chooseEra; 

根本没有意义,我在if else语句中尝试了ChooseEra = Past和ChooseEra = Future,比较了“过去”和“未来”的前提但没有运气。 我们非常感谢任何帮助。 萨姆

1 个答案:

答案 0 :(得分:1)

您的打字问题。如果要返回要使用的实现(Future或Past),则需要可以使用的共享接口或基类。我会选择这个名字为Era。定义该共享类型的所有变量,它将起作用。例如:

public Era readTheEra(SharedPreferences prefs) {
    String whichEra = prefs.getString(PREFERENCE_BACK, "past");
    Era era = whichEra.equals("past") ? new Past() : new Future();
    return era;
}

注意Era被标记为方法的返回类型(你有无效的void)。另请注意,我只是将SharedPreferences传递给方法,并封装代码以提取方法中的首选项值。这样你就不会写入实例变量(你不需要),然后阅读其他方法。只需将信息传递给方法,不要保存中间步骤。您唯一需要的是Era参考使用。实例化正确的类后,不需要首选项的值。

您需要将两个具体实现标记为实现Era接口:

public interface Era {
   // put methods here you need both implementations to 
   // have so you can work from Era interface and not the 
   // individual concrete clases.
}

public class Past implements Era {
    ...
}

public class Future implements Era {
    ...
}

public class Engine {
   private Era era;

   ...
   private Era readTheEra(SharedPreferences prefs) {
      String whichEra = prefs.getString(PREFERENCE_BACK, "past");
      Era era = whichEra.equals("past") ? new Past() : new Future();
      return era;
   }
}

我选择使用接口,因为您的问题不够清楚,无法知道您是否需要类或只能使用接口。但是如果你需要使用像Activity这样的类或其他什么,那么所有相同的东西都适用。具有抽象基类的Subclass Activty,以及Past和Future应该是抽象基类的子类。