2015-11-07 29 views
1

對於一個小項目,我必須編寫一個Android應用程序,用手指在屏幕上畫一條線。目前我使用這段代碼Android Studio | Java ---用手指畫一個位圖

´ 
public class MainActivity extends Activity { 

    DrawingView dv ; 
    private Paint  mPaint; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dv = new DrawingView(this); 
    setContentView(dv); 
    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(Color.GREEN); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(12); 
} 

public class DrawingView extends View { 

    public int width; 
    public int height; 
    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    private Paint mBitmapPaint; 
    Context context; 
    private Paint circlePaint; 
    private Path circlePath; 

    public DrawingView(Context c) { 
    super(c); 
    context=c; 
    mPath = new Path(); 
    mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
    circlePaint = new Paint(); 
    circlePath = new Path(); 
    circlePaint.setAntiAlias(true); 
    circlePaint.setColor(Color.BLUE); 
    circlePaint.setStyle(Paint.Style.STROKE); 
    circlePaint.setStrokeJoin(Paint.Join.MITER); 
    circlePaint.setStrokeWidth(4f); 


    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 

    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    mCanvas = new Canvas(mBitmap); 

    } 
    @Override 
    protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

    canvas.drawPath(mPath, mPaint); 

    canvas.drawPath(circlePath, circlePaint); 
    } 

    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

    private void touch_start(float x, float y) { 
    mPath.reset(); 
    mPath.moveTo(x, y); 
    mX = x; 
    mY = y; 
    } 
    private void touch_move(float x, float y) { 
    float dx = Math.abs(x - mX); 
    float dy = Math.abs(y - mY); 
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
     mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
     mX = x; 
     mY = y; 

     circlePath.reset(); 
     circlePath.addCircle(mX, mY, 30, Path.Direction.CW); 
    } 
    } 
    private void touch_up() { 
    mPath.lineTo(mX, mY); 
    circlePath.reset(); 
    // commit the path to our offscreen 
    mCanvas.drawPath(mPath, mPaint); 
    // kill this so we don't double draw 
    mPath.reset(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
    float x = event.getX(); 
    float y = event.getY(); 

    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      touch_start(x, y); 
      invalidate(); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      touch_move(x, y); 
      invalidate(); 
      break; 
     case MotionEvent.ACTION_UP: 
      touch_up(); 
      invalidate(); 
      break; 
    } 
    return true; 
    } 
    } 

}

有了這個,我能夠利用我的屏幕上的行,但我不知道怎麼畫線轉換爲位圖。我認爲我理解錯了,但我認爲「mBitmap」只是一個空的位圖,「圖片」保存在「mCanvas」中。

那麼有沒有什麼辦法來轉換位圖中的畫線?

+0

是的,有一個解決方案。 –

+0

看看這個:http://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android –

回答

0

您無需爲此創建mBitmapmCanvas。您只需撥打View.getDrawingCache()即可獲取View的內容。 More info here

但是,如果您打算繪製自己的位圖,那麼您需要有一個mBitmapmCanvas。按照下面的步驟來實現:

  1. 移動下方進線構造,因爲你想要的位圖和畫布創作只有一次。

    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    mCanvas = new Canvas(mBitmap); 
    
  2. 在你onDraw()方法,使用mCanvas繪製,而不是canvas。最後將mBitmap繪製到視圖的畫布上。類似下面:

    @Override 
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas); 
    
        // This will use the mCanvas and draw the `path` in the mBitmap. 
        mCanvas.drawPath(mPath, mPaint); 
        mCanvas.drawPath(circlePath, circlePaint); 
    
        // Now once everything is drawn on mBitmap, we will draw the contents of this mBitmap onto the underlying bitmap of the View via `canvas`. 
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
    } 
    

一定要記住,每個視圖將具有底層位圖(跨越共享)。您在onDraw()中獲得的canvas將幫助您在底層位圖中繪製東西。如果你想繪製自己的位圖,那麼你需要創建一個位圖和一個畫布,並使用你創建的畫布進行繪製,而不是在onDraw()中提供給你的畫布。