1. 佈局設計
2. 程式設計
package com.example.rgbtest;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity{
ImageView img1, img2;
Button b1,b2,b3;
Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = (ImageView) findViewById(R.id.imageView1);
img2 = (ImageView) findViewById(R.id.imageView2);
img1.setDrawingCacheEnabled(true);
bitmap = img1.getDrawingCache();
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bitmap = img1.getDrawingCache();
img2.setImageBitmap(bitmap);
}
});
b2=(Button) findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bitmap = img1.getDrawingCache();
Bitmap tmp = Bitmap.createBitmap(bitmap);
for(int y=0; y for(int x=0; x
{
int c = bitmap.getPixel(x, y);
int r = Color.red(c);
int g = Color.green(c);
int b = Color.blue(c);
tmp.setPixel(x, y, Color.rgb(g, b, r));
}
img2.setImageBitmap(tmp);
}
});
b3=(Button) findViewById(R.id.button3);
b3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bitmap = img1.getDrawingCache();
Bitmap tmp = Bitmap.createBitmap(bitmap);
for(int y=0; y for(int x=0; x
{
int c = bitmap.getPixel(x, y);
int r = Color.red(c);
int g = Color.green(c);
int b = Color.blue(c);
tmp.setPixel(x, y, Color.rgb(b, r, g));
}
img2.setImageBitmap(tmp);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
3.執行結果
按下 RGB->RGB鍵
按下RGB->GBR鍵
按下RGB->BRG鍵