Friday, October 14, 2011

Share Bitmap between activities as global common resources

Refer to the last exercises, we can "Pass Bitmap between activities (as Extra)" and "Save and Read Bitmap in Internal Storage"; here is one more alternative, share as global common resources.

Create one more class, CommonResources, with public static Bitmap. Such that it can be direct access by all activities of the same APK.

CommonResources.java
package com.exercise.AndroidBitmap;

import android.graphics.Bitmap;

public class CommonResources {
public static Bitmap bmA = null;
public static Bitmap bmB = null;
public static Bitmap bmC = null;
}


AndroidBitmapActivity.java
package com.exercise.AndroidBitmap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidBitmapActivity extends Activity {

ImageView image1, image2, image3, image4;
Button buttonSwitch;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
image3 = (ImageView)findViewById(R.id.image3);
image4 = (ImageView)findViewById(R.id.image4);
buttonSwitch = (Button)findViewById(R.id.switchactivity);

Bitmap bmOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
image1.setImageBitmap(bmOriginal);

int width = bmOriginal.getWidth();
int height = bmOriginal.getHeight();

final Bitmap bmDulicated2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Bitmap bmDulicated3 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Bitmap bmDulicated4 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] srcPixels = new int[width * height];
bmOriginal.getPixels(srcPixels, 0, width, 0, 0, width, height);
int[] destPixels = new int[width * height];

swapGB(srcPixels, destPixels);
bmDulicated2.setPixels(destPixels, 0, width, 0, 0, width, height);
image2.setImageBitmap(bmDulicated2);

swapRB(srcPixels, destPixels);
bmDulicated3.setPixels(destPixels, 0, width, 0, 0, width, height);
image3.setImageBitmap(bmDulicated3);

swapRG(srcPixels, destPixels);
bmDulicated4.setPixels(destPixels, 0, width, 0, 0, width, height);
image4.setImageBitmap(bmDulicated4);

buttonSwitch.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

CommonResources.bmA = bmDulicated2;
CommonResources.bmB = bmDulicated3;
CommonResources.bmC = bmDulicated4;

Intent intent = new Intent(AndroidBitmapActivity.this, SecondActivity.class);
startActivity(intent);
}});

}

void swapGB(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xffff0000)
| ((src[i] & 0x000000ff)<<8)
| ((src[i] & 0x0000ff00)>>8);
}
}

void swapRB(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xff00ff00)
| ((src[i] & 0x000000ff)<<16)
| ((src[i] & 0x00ff0000)>>16);
}
}

void swapRG(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xff0000ff)
| ((src[i] & 0x0000ff00)<<8)
| ((src[i] & 0x00ff0000)>>8);
}
}
}


SecondActivity.java
package com.exercise.AndroidBitmap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

public class SecondActivity extends Activity {

ImageView imageA, imageB, imageC;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
imageA = (ImageView)findViewById(R.id.imagea);
imageB = (ImageView)findViewById(R.id.imageb);
imageC = (ImageView)findViewById(R.id.imagec);

Bitmap bitmapA = CommonResources.bmA;
Bitmap bitmapB = CommonResources.bmB;
Bitmap bitmapC = CommonResources.bmC;

imageA.setImageBitmap(bitmapA);
imageB.setImageBitmap(bitmapB);
imageC.setImageBitmap(bitmapC);

}

}


Download the files.

2 comments:

Apurva Aggarwal said...

How to get image as it is????
Becauese when I am applying this code, the resultant image is getting colored

Apurva Aggarwal said...

Thanx,now its working with your other blog