2014-12-06 48 views
2

我正在Android中創建一個應用程序,用戶在畫布上畫一些東西,並保存它的用戶目錄,即SD卡。如何在Android中將畫布轉換爲位圖?

但我的問題是,我保存的位圖總是顯示一個黑色的圖像,我的意思是圖像從不保存在畫布上繪製只有黑色圖像保存。

這裏是我的代碼

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.CompressFormat; 
import android.graphics.Bitmap.Config; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.os.Environment; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Toast; 

public class DrawWrite extends View { 
    float TouchXD = 0, TouchYD = 0, TouchXU = 0, TouchYU = 0, TouchXM = 0, 
      TouchYM = 0; // Define touch co-ordinates 
    float x1 = 0, y1 = 0, x2 = 0, y2 = 0; // Define drawing path co-ordinates 
    float stroke = 2; // Define the message structure width 
    int i=0; 
boolean Move = false, moveD = false, moveU = false; // Define whether the 
                // touch has occurred or 
                // not 
boolean exp = false; 
Paint paint = new Paint(); // Paint object 
Path mPath = new Path(); // Define the drawing message path 
Context context; 

public DrawWrite(Context context) { 
    super(context); 
    this.context=context; 
} 

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    invalidate(); 
    paint.setAntiAlias(true); 
    if (DrawWriteActivity.clearScreen){ 
     mPath.reset(); 
     DrawWriteActivity.clearScreen = false; 
    } 
    if(DrawWriteActivity.saveImage){ 
     try { 
      getDrawnMessage(canvas); 
      DrawWriteActivity.saveImage = false; 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
    paint.setColor(Color.parseColor(DrawWriteActivity.colorProvider)); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(DrawWriteActivity.strokeProvider); 
    if (moveD == true) { 
     x1 = TouchXD; 
     y1 = TouchYD; 
     moveD = false; 
    } else if (Move == true) { 
     x2 = TouchXD; 
     y2 = TouchYD; 
     mPath.moveTo(x1, y1); 
     mPath.lineTo(x2, y2); 
     canvas.drawPath(mPath, paint); 
     x1 = x2; 
     y1 = y2; 
    } 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     TouchXD = (float) event.getX(); 
     TouchYD = (float) event.getY(); 
     moveD = true; 
     break; 
    case MotionEvent.ACTION_UP: 
     TouchXU = (float) event.getX(); 
     TouchYU = (float) event.getY(); 
     moveU = true; 
     break; 
    case MotionEvent.ACTION_MOVE: 
     TouchXD = (float) event.getX(); 
     TouchYD = (float) event.getY(); 
     Move = true; 
     break; 
    } 
    return true; 
} 

public void getDrawnMessage(Canvas canvas) throws FileNotFoundException{ 
    Bitmap bitmap; 
    setDrawingCacheEnabled(true); 
    Toast.makeText(getContext(), "Toasting", Toast.LENGTH_SHORT).show(); 
    String root = Environment.getExternalStorageDirectory().toString(); 
    File imgDir = new File(root+"/ChitBak/"); 
    String imgName; 
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); 
    imgDir.mkdirs(); 
    imgName = "img"+i+".jpg"; 
    i++; 
    File file = new File(imgDir,imgName); 
    if(file.exists()) file.delete(); 
    FileOutputStream outImg = new FileOutputStream(file); 
    bitmap.compress(CompressFormat.JPEG, 100, outImg); 
    setDrawingCacheEnabled(false); 
} 
} 

我也想澄清,當我使用

bitmap = Bitmap.createBitmap(getDrawingCache()); 

方法則拋出一個計算器例外。

請朋友們幫助我,因爲我從一週的這種情況中生氣。

回答

2

您的位圖是黑色的,因爲您沒有在其上繪製任何東西。

有一些位圖繪製你的確有這個調用創建:

bitmap = Bitmap.createBitmap(getDrawingCache()); 

然後你有一個計算器例外,只是因爲:

  • getDrawingCache()呼叫onDraw(Canvas)
  • onDraw(Canvas)通話getDrawnMessage(Canvas canvas)
  • getDrawnMessage(Canvas canvas)致電getDrawingCache()
  • getDrawingCache()呼叫onDraw(Canvas)
  • ...

所以你真正的問題是StackOverflow的例外。

解決方法:不要在onDraw(Canvas)方法中調用getDrawnMessage(Canvas canvas)

+0

男人你是誰明白我的問題只有一個。 – 2014-12-08 13:43:02

0

也許有邏輯錯誤: 當你設置移動=在移動事件真正,你忘了重置爲假的,但的onDraw()將被頻繁調用,那麼X1將相等於X2,Y1至Y2,你什麼也看不到,只有一個點。

建議解決方案:

  1. 改變你的邏輯,這是更好地記錄觸摸軌跡在陣列,記得使用
  2. 後清除它作爲第一個答案,而不是產生的onDraw位圖()在另一事件處理更好,比如點擊一個按鈕

好運

相關問題