2013-11-29 149 views
0

當我編譯此代碼時,我不會在eclipse上得到任何警告,但是當我在設備或模擬器上運行它時,該程序被迫關閉。Android:將imageview轉換爲位圖,灰度,將位圖轉換爲imageview

public class MainActivity extends Activity { 

    ImageView img; 
    Button btn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //convert imageview to bitmap 
     img =(ImageView) findViewById(R.id.imageView1); 
     BitmapDrawable drawable = (BitmapDrawable) img.getDrawable(); 
     final Bitmap imgbitmap = drawable.getBitmap(); 


     btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //convert bitmap to grayscale 
       Bitmap imgnew; 
       imgnew = toGrayscale(imgbitmap);  

       //convert bitmap to imageview 
       ImageView imgbit; 
       imgbit = (ImageView) findViewById(R.id.imageView2); 
       imgbit.setImageBitmap(imgnew); 
      } 
     }); 

    } 

    public Bitmap toGrayscale(Bitmap bmpOriginal){   
     int width, height; 
     height = bmpOriginal.getHeight(); 
     width = bmpOriginal.getWidth();  

     Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
     Canvas c = new Canvas(bmpGrayscale); 
     Paint paint = new Paint(); 
     ColorMatrix cm = new ColorMatrix(); 
     cm.setSaturation(0); 
     ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); 
     paint.setColorFilter(f); 
     c.drawBitmap(bmpOriginal, 0, 0, paint); 
     return bmpGrayscale; 
    } 

} 
+2

分享logcat的 –

+0

@payeli這裏我logcat的http://pastebin.com/MwgeXZbf –

+0

你可以請貼在這裏你的日誌貓嗎? – Yup

回答

1

如果上面是你的全代碼,然後,根本的問題是有,你有沒有定義btn。你需要在使用它之前定義它,否則當你自己點擊按鈕時它將不起作用。這可能會關閉你的應用程序。

btn=(Button) findViewById(R.id.button1); 
+0

謝謝:D 我忘了那個......問題解決了 –

相關問題