I was carry out 3 tests to check if the bitmap is pass by value or reference but get confused after I run the following code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
V v = new V(this);
setContentView(v);
}
class V extends View{
Bitmap b1;
Bitmap b2;
public V(Context context) {
super(context);
//load bitmap1
InputStream is = getResources().openRawResource(R.drawable.missu);
b1 = BitmapFactory.decodeStream(is);
//testing start
b2 = b1;
//b1 = null;
//1.test if b1 and b2 are different instances
if(b2 == null){
Log.d("","b2 is null");
}
else{
Log.d("","b2 still hv thing");
}
//2.test if b2 is pass by ref or value
test(b2);
if(b2 == null){
Log.d("","b2 is null00");
}
else{
Log.d("","b2 still hv thing00");
}
//3.want to further confirm test2
b2 = b2.copy(Config.ARGB_8888, true);
settpixel(b2);
if(b2.getPixel(1, 1) == Color.argb(255,255, 255, 255)){
Log.d("","b2(1,1) is 255");
}
else{
Log.d("","b2(1,1) is not 255");
}
}
void test(Bitmap b){
b = null;
}
void settpixel(Bitmap b){
b.setPixel(1, 1, Color.argb(255,255, 255, 255));
}
}}
results:
b2 still hv thing
b2 still hv thing00
b2(1,1) is 255
The problem is test 2 and 3 contradict to each other. test2 show that b2 is pass by value because b2 didn't become null. But if bitmap is passed by value, then in test3, the setPixel() should be working on the copy of b2(the one in the function scope), but why b2(outer scope) change it's pixel value? p.s. the loaded bitmap is in deep red color.