Reputation: 1433
I have an Object
lets call it "A" that has an ArrayList<B>
. The object B holds a Bitmap
. When I iterate through my ArrayList<B>
to grab the Bitmaps
from each B Object
there is only one type. I've done some testing and I am definitely putting in different Bitmaps
, but somewhere my logic is flawed and I am overwriting the all Bitmaps
with the last Bitmap
in my ArrayList<B>
.
Java
private A createObject() {
A a = new A(); // create object A
ArrayList<B> bList = new ArrayList<B>(); //create array list which will hold B objects
RelativeLayout rel = (RelativeLayout)findViewById(R.id.rel);
LinearLayout lin = new LinearLayout(this);
lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
a.setName(UUID.randomUUID().toString());
for (LinkedHashMap.Entry<String, ArrayList<Circle>> entry : mCordHashMap.entrySet()) {
ArrayList<Circle> coords = new ArrayList<Circle>();
B b = new B(); //create new B object
coords = entry.getValue();
Path path = new Path();
String key = entry.getKey();
String surfaceName = getSurfaceFromKey(key);
b.setName(surfaceName);
for (Circle c : coords) {
if (c == coords.get(0)) {
path.moveTo(c.getmX(), c.getmY());
} else {
path.lineTo(c.getmX(), c.getmY());
}
}
path.close();
mCanvas.drawPath(path, mPaint);
mImageView.invalidate();
b.setRegion(mBitmap); // add bitmap to B
bList.add(b); // add B to list
}
a.setBList(bList); // add the list of B objects to object A here
ArrayList<B> testList = a.getBList();
for (B b : testList) {
Bitmap bmp = b.getRegion().createScaledBitmap(b.getRegion(), 100, 100, false);
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iv.setImageBitmap(bmp);
lin.addView(iv);
}
rel.addView(lin);
return a;
}
Now all of this works, just not as intended. I get ImageViews
with the same Bitmap
in each of them.
So it looks like this
[a] [a] [a] [a]
However theBitmaps
that go in are different. For instance if I change where I add my views, the outcome is as desired.
2nd example
private A createObject() {
A a = new A(); // create object A
ArrayList<B> bList = new ArrayList<B>(); //create array list which will hold B objects
RelativeLayout rel = (RelativeLayout)findViewById(R.id.rel);
LinearLayout lin = new LinearLayout(this);
lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
a.setName(UUID.randomUUID().toString());
for (LinkedHashMap.Entry<String, ArrayList<Circle>> entry : mCordHashMap.entrySet()) {
ArrayList<Circle> coords = new ArrayList<Circle>();
B b = new B(); //create new B object
coords = entry.getValue();
Path path = new Path();
String key = entry.getKey();
String surfaceName = getSurfaceFromKey(key);
b.setName(surfaceName);
for (Circle c : coords) {
if (c == coords.get(0)) {
path.moveTo(c.getmX(), c.getmY());
} else {
path.lineTo(c.getmX(), c.getmY());
}
}
path.close();
mCanvas.drawPath(path, mPaint);
mImageView.invalidate();
// DIFFERENCE
Bitmap bmp = mBitmap.createScaledBitmap(mBitmap, 100, 100, false);
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iv.setImageBitmap(bmp);
lin.addView(iv);
b.setRegion(mBitmap); // add bitmap to B
bList.add(b); // add B to list
}
rel.addView(lin);
a.setBList(bList); // add the list of B objects to object A here
return a;
}
This code shows ImagevViews
with unique Bitmaps
inside, as desired, like this
[a] [b] [c] ...
Why? Where am I losing the logic?
Upvotes: 0
Views: 70
Reputation: 3964
You probably use same copy of Bitmap, which is wrong. Remember that by adding a Bitmap to a List, you don't create a copy, but place the actual bitmap. And you place many times the same bitmap!
The correct solution is using a new Bitmap every time. This is a hint
List<Bitmap> list = new ArrayList<Bitmap>();
for (...) {
Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canv = new Canvas(bmp);
/* Draw on canvas */
list.add(bmp);
}
Or, you may use bmp.copy(...) you you want to avoid replicating the drawing.
Upvotes: 2