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
{
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
{
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鍵
為什麼執行b2就終止了?
回覆刪除記得要放置按鈕,並把ID設定為button2
回覆刪除我是放兩個button一個開啟圖片一個轉換
回覆刪除可是只要執行到b2就會終止把b2那部分拿掉就可以執行
我b2的碼是 int mBitmapWidth = mBitmap.getWidth();
int mBitmapHeight = mBitmap.getHeight();
for(int y=0; y < mBitmapHeight;y++)
{
for(int x=0;x< mBitmapWidth;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));
}}
求大大救救我><