2013-04-17 34 views
0

我有這個視圖可以按照用戶的手指進行繪製。當我希望他可以繪製時,我使用addView將其添加到我的Activity的佈局上,並且當我希望他不能繪製時使用removeView將其刪除。我的問題是,我想讓他畫出的線條出現,但他們失去了! 你有什麼想法我可以做什麼來改變這一點?從佈局刪除視圖時保留行

public class MyView extends View{ 
    private static final float MINP = 0.25f; 
    private static final float MAXP = 0.75f; 



    public MyView(Context c) { 
     super(c); 

     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(Color.BLACK); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(3); 

    } 

    @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) { 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
     invalidate(); 

    } 


    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; 
     } 
    } 
    private void touch_up() { 
     mCanvas.drawPath(mPath, mPaint); 
    } 


    @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; 
    } 
} 

回答

0

我不知道是真的還是假的,但你可以做這樣的事情: 1.Take與圖紙佈局的屏幕截圖。 2.從佈局中刪除圖紙視圖。 3.使用剛剛拍攝的截圖設置佈局的背景。

希望你明白..希望它有效..

+0

我明白,但我希望用戶可以刪除這些行,如果他想隨時。但如果我沒有找到其他的東西,這是個好主意 – user2273777