Java 跨类使用资源?

Java 跨类使用资源?,java,android,caching,Java,Android,Caching,我有一个GameLoop类,其中定义了ParticleSystem对象 ParticleSystem包含一个粒子对象数组-我不想为每个单独的粒子加载一个图像,我只想能够从GameLoop类中的源图像中进行绘制-如何以高效的方式进行此操作 解决方案: // in GameLoop definition private Bitmap particleA; private Bitmap particleB; // somewhere in GameLoop constructor particle

我有一个GameLoop类,其中定义了ParticleSystem对象

ParticleSystem包含一个粒子对象数组-我不想为每个单独的粒子加载一个图像,我只想能够从GameLoop类中的源图像中进行绘制-如何以高效的方式进行此操作

解决方案:

// in GameLoop definition

private Bitmap particleA;
private Bitmap particleB;

// somewhere in GameLoop constructor
particleA = BitmapFactor.decodeResource (activity.getResources(), R.drawable.particle_a);
particleB = BitmapFactor.decodeResource (activity.getResources(), R.drawable.particle_b);

// where you build your particle
Particle (GameLoop gl, ...) {
   oneLevelDown = new OneLevelDown(GameLoop gl, ...);
}

OneLevelDown (GameLoop gl, ...) {
   twoLevelDown = new TwoLevelDown(GameLoop gl, ...);
}

TwoLevelDown (GameLoop gl, ...) {
   particleABitmap = gl.getParticleA(); // simple getter
   particleBBitmap = gl.getParticleB(); // simple getter
}
以下是Dan S帮我想到的:

public class ResourceManager  {

Context mContext;   

public final HashMap<String, Bitmap> Texture = new HashMap<String, Bitmap>();

//Constructor
public ResourceManager(Context context)
{       
    mContext = context;
    Texture.put("particle1", loadBitmap(R.drawable.particle1));
}

public Bitmap getBitmap(String key)
{   
return Texture.get(key);
}

 /** Load an image */
    protected Bitmap loadBitmap(int id) { return   BitmapFactory.decodeResource(mContext.getResources(), id); }

  } //End ResourceManager
然后将rm变量向下传递:

    ParticleSystem.Draw(rm, canvas);
   {
     Particle.Draw(rm, canvas);
    }
在Particle类中,我在构造函数中设置了一个字符串assetKey,这样我就可以通过名称引用位图:

public void doDraw(ResourceManager rm, Canvas canvas) {
    canvas.drawBitmap(rm.getBitmap(AssetKey), xpos, ypos, null);
}

我希望这对其他人也有帮助。

在GameLoop的构造函数中创建位图并在其中保留对它们的引用,将它们设置为最终变量,以增加对意外赋值的保护。无论何时创建新粒子系统,请将它们传递给构造函数或使用适当的setter方法进行设置

示例:

// in GameLoop definition

private Bitmap particleA;
private Bitmap particleB;

// somewhere in GameLoop constructor
particleA = BitmapFactor.decodeResource (activity.getResources(), R.drawable.particle_a);
particleB = BitmapFactor.decodeResource (activity.getResources(), R.drawable.particle_b);

// where you build your particle
Particle (GameLoop gl, ...) {
   oneLevelDown = new OneLevelDown(GameLoop gl, ...);
}

OneLevelDown (GameLoop gl, ...) {
   twoLevelDown = new TwoLevelDown(GameLoop gl, ...);
}

TwoLevelDown (GameLoop gl, ...) {
   particleABitmap = gl.getParticleA(); // simple getter
   particleBBitmap = gl.getParticleB(); // simple getter
}
继续向下传递GameLoop直到完成。这可能会让人觉得效率不高,但一旦你掌握了窍门,你会喜欢的,也可以看看这篇文章

Java内存使用示例:

class Car{
   public int year;
   public int name;
}

// Object variables are references to an object's data.

Car a = new Car(); // first car object and variable. 
a.year = 1990;
a.name = "Sam";

Car b = new Car();// second car object and variable
b.year = 2000;
b.name = "Bob";

Car c = a; // third car **variable** but same car object as **a**

// primitives are different

int a = 23; // first variable, 4 bytes used
int b = 45; // second variable, 4 bytes used (and a total of 8)
int c = a;  // third variable, 4 bytes used (and a total of 12). c took on the value of a, but does not point to the same memory as a

我一直在尝试实现类似的东西,不幸的是,当一个新粒子被创建时,在到达游戏循环时遇到了困难。如何从下面的两个类访问GameLoop中的变量:粒子[0]=新粒子(GameLoop.Bitmap);只要将GameLoop对象向下传递到所需的位置即可。这类似于如何将上下文传递到视图对象以进行创建。您能给我一个如何从粒子“调用”GameLoop类的片段吗?你是说在粒子类中创建一个新对象吗?比如:Resources=newresourceclass();位图test=Resources.Bitmap——如果我这样做,我不仅仅是在ResourceClass定义中创建每个位图的副本吗?我希望定义它一次,然后在另一个类中使用它查看我的编辑。Java中的对象分配并不复制对象,只是JVM用来引用真实数据的对象id的值。唯一的重复是JVM对象引用大小,在32位处理器上通常为32位。例外情况是不可变对象,如字符串,但这仅限于调用的方法更改内部状态。好的,它在每一层上都初始化了对象的新版本--请原谅我的无知,但这不是会创建每个位图的副本吗,每次我创建一个新粒子?…它们是我关于创建一个新类如何工作的知识中的一个空白吗?顺便说一句,我很感激你不仅仅是在Java.com上发布了一个链接,告诉我要在任何地方学习所有东西。