我把源码贴在这里,各位帮忙看一下package com.example.gsbtest;import java.io.FileNotFoundException;
import java.io.InputStream;import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class MainActivity extends Activity { Button btn0,btn1;
TextView Text1;
Bitmap bitmap, temp ;//选择了图片后调用
ImageView imageView;    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn0 = (Button)findViewById(R.id.b01);
        btn0.setText("select image");
        btn1 = (Button)findViewById(R.id.sufflebtn);
        
        btn0.setOnClickListener(new OnClickListener(){
        
         public void onClick(View v){
         Intent intent = new Intent();
         intent.setType("image/*");
         intent.setAction(Intent.ACTION_GET_CONTENT);
         startActivityForResult(intent,1);
         }
        });
        
        btn1.setOnClickListener(new OnClickListener(){
       
         @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
         Text1 = (TextView)findViewById(R.id.Txt1);
         if(temp==null || temp.isRecycled() ) 
         return;
         suffleImageRGB(temp);
}
        });
        
    }    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {  
        if (resultCode == RESULT_OK) {  
            Uri uri = data.getData();  
            Log.e("uri", uri.toString());  
            android.content.ContentResolver cr = this.getContentResolver();  
            try {  
            
                bitmap = BitmapFactory.decodeStream((InputStream) cr.openInputStream(uri));  
                  temp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                
                imageView = (ImageView) findViewById(R.id.iv01);  
                /* 将Bitmap设定到ImageView */  
                imageView.setImageBitmap(temp); 
            } catch (FileNotFoundException e) {  
                Log.e("Exception", e.getMessage(),e);  
            }  
        }  
        super.onActivityResult(requestCode, resultCode, data);  
    }  
    
    void suffleImageRGB(Bitmap bmp) 
    {    
    
     int w = bmp.getWidth();     
     int h = bmp.getHeight();     
     int color=0,r=0,g=0,b=0;     
     for(int x=0; x<w; x++)
     {          
     for(int y=0 ;y<h; y++)
     {             
     color = bmp.getPixel(x, y);       
     r = Color.red(color);             
     g = Color.green(color);             
     b = Color.blue(color);  
     bmp.setPixel(x,y,Color.argb(Color.alpha(color), b,g ,r ) );//R->G->B->R        }    }   } 
     imageView.setImageBitmap(bmp);
     }
     }
    }
        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

解决方案 »

  1.   

    Bitmap bmp=原图片;
    bmp=bmp.copy(bmp.getConfig(), true);
      

  2.   

    谢谢 2楼的朋友, 我写了代码,能否帮我看下,哪里写的不对,因为当我点击shuffle按钮的时候,过一会儿有个弹出窗口GSBTest isn't responding. Do you want to close it? 然后点击wait(有wait和close按钮) 过很久的话图片会变绿,不知道这样写的对不对, 下面贴上源码package com.example.gsbtest;import java.io.FileNotFoundException;
    import java.io.InputStream;import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Color;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;public class MainActivity extends Activity { Button btn0,btn1;
    TextView Text1;
    Bitmap bitmap, temp ;//选择了图片后调用
    ImageView imageView;    @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            btn0 = (Button)findViewById(R.id.b01);
            btn0.setText("select image");
            btn1 = (Button)findViewById(R.id.sufflebtn);
            
            btn0.setOnClickListener(new OnClickListener(){
            
             public void onClick(View v){
             Intent intent = new Intent();
             intent.setType("image/*");
             intent.setAction(Intent.ACTION_GET_CONTENT);
             startActivityForResult(intent,1);
             }
            });
            
            btn1.setOnClickListener(new OnClickListener(){
           
             @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
             Text1 = (TextView)findViewById(R.id.Txt1);
             if(temp==null || temp.isRecycled() ) 
             return;
             suffleImageRGB(temp);
    }
            });
            
        }    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {  
            if (resultCode == RESULT_OK) {  
                Uri uri = data.getData();  
                Log.e("uri", uri.toString());  
                android.content.ContentResolver cr = this.getContentResolver();  
                try {  
                    bitmap = BitmapFactory.decodeStream((InputStream) cr.openInputStream(uri));  
                    temp = bitmap.copy(bitmap.getConfig(), true);
                    
                    imageView = (ImageView) findViewById(R.id.iv01);  
                    /* 将Bitmap设定到ImageView */  
                    imageView.setImageBitmap(temp); 
                } catch (FileNotFoundException e) {  
                    Log.e("Exception", e.getMessage(),e);  
                }  
            }  
            super.onActivityResult(requestCode, resultCode, data);  
        }  
        
        void suffleImageRGB(Bitmap bmp) 
        {    
        
         int w = bmp.getWidth();     
         int h = bmp.getHeight();     
         int color=0,r=0,g=0,b=0;     
         for(int x=0; x<w; x++)
         {          
         for(int y=0 ;y<h; y++)
         {             
         color = bmp.getPixel(x, y);       
         r = Color.red(color);             
         g = Color.green(color);             
         b = Color.blue(color);  
         bmp.setPixel(x,y,Color.argb(Color.alpha(color), b,g ,r ) );//R->G->B->R        }    }   } 
         imageView.setImageBitmap(bmp);
         }
         }
        }
            @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }strings.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>    <string name="app_name">GSBTest</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
        <string name="suffle">suffle</string>
        <string name="desc">imageview01</string></resources>
      

  3.   

    我把suffleImageRGB(temp); 这一行换成如下的代码,但是执行还是很慢,是不是写的不对,还是我才疏学浅,异步/线程不是这么写?handler.post(new Runnable()
    {
            public void run()
            {
                    suffleImageRGB(temp);
            }
    });
      

  4.   

    将如下代码放到onCreate函数里的按钮点击事件上
     CountingTask task=new CountingTask();  
             task.execute();  然后按照如下代码写AsyncTask,但是不知道是不是写的不对,很多报错 private class CountingTask extends AsyncTask<Void, Integer, Integer>{  
         CountingTask()
         {
         super();
         } 
        
         protected void onPostExecute(Object result) 
         {
       System.out.print("onPostExecute is running...");
       imageView.setImageBitmap((Bitmap)result);
       super.onPostExecute(result);
         }
          
         protected void onPreExecute() 
         {
         System.out.print("onPreExecute is running...");
         super.onPreExecute();
         }
        
         protected void onProgressUpdate(Object... values) 
         {
           System.out.print("onProgressUpdate is running...");
           super.onProgressUpdate(values);   
         }
          
        
         protected Object doInBackground(Void... params) 
         {  
             suffleImageRGB(temp);
    return null;
         }      }
      

  5.   

    在protected void onPostExecute(Object result)  这一行上
    报错:Name clash:The method onPostExecute(Object) of type MainActivity.CountingTask has the same erasure as onPostExecute(Result) of type AsyncTask<Params, Progress, Result> but doese not override it
    在protected void onProgressUpdate(Object... values) 这一行上也是类似的报错