如何在Java中将对象从B类通过A类传递到C类而不重复
假设我有 3 节课。
- A 级(延长活动)
B级
C 类(扩展视图)
A 创建一个包含 Bitmap 的 B 对象。 A 也会使 C 无效。C 的 onDraw
将 Bitmap
绘制到其 Canvas
上。
如何绘制 B 保存的位图而不在 C 中创建另一个位图对象?请注意,保持相同的“类结构”对我来说很重要。
这是伪代码。您可以看到这将 2 个 Bitmap 对象放入内存中。谢谢!
public class A extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
B b = new B();
C c = (C) findViewById(R.id.c);
c.passTheBitmap(b.bitmap);
c.postInvalidate();
}
}
public class B {
public Bitmap bitmap;
public B(){
bitmap = BitmapFactory.decodeResource(ActivityAContext.getResources(), R.drawable.fooBitmap);
}
}
public class C extends View {
public Bitmap bitmap;
public C(Context context, AttributeSet attrs) {
super(context, attrs);
}
public passTheBitmap(Bitmap b) {
bitmap = b;
}
@Override
public void onDraw(Canvas canvas) {
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
}
Let's say I have 3 classes.
- Class A (extends Activity)
Class B
Class C (extends View)
A creates a B object that holds a Bitmap
. A also invalidates C. C's onDraw
draws a Bitmap
to its Canvas
.
How do I draw the Bitmap
that B holds without creating another Bitmap
object in C? Note it is important for me to keep the same "class structure."
Here is the pseudo code. You can see this puts 2 Bitmap
objects in memory. Thanks!
public class A extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
B b = new B();
C c = (C) findViewById(R.id.c);
c.passTheBitmap(b.bitmap);
c.postInvalidate();
}
}
public class B {
public Bitmap bitmap;
public B(){
bitmap = BitmapFactory.decodeResource(ActivityAContext.getResources(), R.drawable.fooBitmap);
}
}
public class C extends View {
public Bitmap bitmap;
public C(Context context, AttributeSet attrs) {
super(context, attrs);
}
public passTheBitmap(Bitmap b) {
bitmap = b;
}
@Override
public void onDraw(Canvas canvas) {
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,通过在 C 中声明实例变量,您并没有完全复制 C 中的 Bitmap 对象。您只是获得了对 B 的构造函数中创建的原始 Bitmap 对象的另一个引用。
Well, by having the instance variable declared in C, you're not exactly duplicating the Bitmap object in C. You're just having another reference to the original Bitmap object that gets created in the constructor of B.
我假设您认为以下行:
会导致 b.bitmap 被复制。只要
b.bitmap
不是基元,就不会。在您的代码中,获得第二个副本的唯一方法是使用
new
有意创建一个新对象。例如,在
C
类中,如果您按照您所做的操作:
...那么 Abbitmap 和 C.bitmap 将引用内存中的同一对象。
如果这是 C++,那么您的担忧是合理的 - 但在 Java 中,您所做的并不会复制所传递的对象。
华泰
I assume you think that the following line:
Would cause
b.bitmap
to get copied. It doesn't, so long asb.bitmap
is not a primitive.In your code, the only way for there to be a second copy is if you use
new
to create a new object intentionally.E.g. in class
C
If you do as you have done:
... then A.b.bitmap and C.bitmap will reference the same object in memory.
If this was C++, then you have a valid concern - but in Java, what you are doing does not make a copy of the object being passed.
HTH