First Activity (Modified from last exercise "Image processing on Bitmap"):
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
Intent intent = new Intent(AndroidBitmapActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("BITMAP_A", bmDulicated2);
bundle.putParcelable("BITMAP_B", bmDulicated3);
bundle.putParcelable("BITMAP_C", bmDulicated4);
intent.putExtras(bundle);
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);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/switchactivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch to second activity"
/>
</LinearLayout>
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);
Bundle bundle = this.getIntent().getExtras();
Bitmap bmA = bundle.getParcelable("BITMAP_A");
Bitmap bmB = bundle.getParcelable("BITMAP_B");
Bitmap bmC = bundle.getParcelable("BITMAP_C");
imageA.setImageBitmap(bmA);
imageB.setImageBitmap(bmB);
imageC.setImageBitmap(bmC);
}
}
second.xml, layout of SecondActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Second Activity"
/>
<ImageView
android:id="@+id/imagea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imagec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Also, AndroidManifest.xml have to be modified to add Activity SecondActivity.
Download the files.
No comments:
Post a Comment