2012-06-09 56 views
1

我正在開發用於繪製的Android應用程序。所以,我需要使用MVC模式。我有視圖類應用使用繪圖:Android應用程序中用於繪製的MVC模式

public class PainterView extends View implements DrawingListener { 

    private Painter painter; 

    private Bitmap bitmap; 
    private Paint bitmapPaint; 
    private Path path; 
    private Paint paint; 

    public PainterView(Context context, Painter painter) { 

     super(context); 
     this.painter=painter; 
     this.painter.addDrawingListener(this); 
    } 

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

    @Override 
    protected void onDraw(Canvas canvas) { 

     if (bitmap!=null) { 
      canvas.drawBitmap(bitmap, 0, 0, bitmapPaint); 
      canvas.drawPath(path, paint); 
     } 
    } 

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

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       painter.touchStart(x, y); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       painter.touchMove(x, y); 
       break; 
      case MotionEvent.ACTION_UP: 
       painter.touchUp(); 
       break; 
     } 

     return true; 
    } 

    @Override 
    public void update(Bitmap bitmap, Paint bitmapPaint, Path path, Paint paint) { 

     this.bitmap=bitmap; 
     this.bitmapPaint=bitmapPaint; 
     this.path=path; 
     this.paint=paint; 
     invalidate(); 
    } 
} 

主要活動:

public class MainScreenActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private PainterView mMainView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     Display display = getWindow().getWindowManager().getDefaultDisplay(); 
     Painter painter=new Painter(display.getWidth(), display.getHeight()); 
     mMainView = new PainterView(this, painter); 
     setContentView(mMainView); 
    } 
} 

和畫家類保存了所有的算法(模型)。請,請注意,所有的算法工作良好:

public class Painter { 

    private List<DrawingListener> mDrawingListeners; 

    private static final float TOUCH_TOLERANCE = 4; 
    private static final float MINP = 0.25f; 
    private static final float MAXP = 0.75f; 

    public Paint mPaint; 
    public Bitmap mBitmap; 
    public Canvas mCanvas; 
    public Path mPath; 
    public Paint mBitmapPaint; 

    private float mX, mY; 

    public Painter(int width, int height) { 
     initializeGraphic(width, height); 
    } 

    private void initializeGraphic(int width, int height) { 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xFFFF0000); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 

     mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

     mCanvas.drawRect(new Rect(0, 0, width, height), new Paint(Color.BLACK)); 
    } 

    private void drawingChanged() { 
     notifyDrawingListeners(); 
    } 

    public void touchStart(float x, float y) { 
     Log.e("event", "start"); 
     mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
     drawingChanged(); 
    } 

    public void touchMove(float x, float y) { 
     Log.e("event", "move"); 
     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; 
     } 
     drawingChanged(); 
    } 

    public void touchUp() { 
     Log.e("event", "up"); 
     mPath.lineTo(mX, mY); 
     mCanvas.drawPath(mPath, mPaint); 
     mPath.reset(); 
     drawingChanged(); 
    } 

    public void addDrawingListener(DrawingListener listener) { 
     if (mDrawingListeners==null) { 
      mDrawingListeners=new ArrayList<DrawingListener>(); 
     } 
     mDrawingListeners.add(listener); 
    } 

    public void removeDrawingListener(DrawingListener listener) { 
     mDrawingListeners.remove(listener); 
    } 

    public void notifyDrawingListeners() { 

     for (DrawingListener listener:mDrawingListeners) { 
      listener.update(mBitmap, mBitmapPaint, mPath, mPaint); 
     } 
    } 
} 

但我有一些問題:當我通過屏幕觸摸,然後繪製它的工作原理,但如果我了我的手指從屏幕,然後屏幕上會再次被黑!即該應用程序不保存繪圖的結果。所以,如果我添加這條線到onDraw方法:

@Override 
protected void onDraw(Canvas canvas) { 

    if (bitmap!=null) { 
     canvas.drawBitmap(bitmap, 0, 0, bitmapPaint); 
     canvas.drawPath(path, paint); 
     painter.mBitmap=bitmap; 
     painter.mBitmapPaint=bitmapPaint; 
     painter.mPaint=paint; 
     painter.mPath=path; 
    } 
} 

然後它的工作!但我不明白爲什麼!應用程序使用相同的鏈接,爲什麼我需要在繪製後再次更改它?謝謝。

回答

1

Android默示遵循MVC模式,所以你只需要不打擾。

MVC pattern on Android

+0

但是爲什麼我需要添加這段代碼? – user1445877

+0

這個答案肯定對提問者的需求沒有建設性。告訴他/她不要做某些事情,他們顯然需要做的是適得其反。此外,你所鏈接的問題雖然被接受的答案使這個斷言成爲熱門辯論。事實上,答案中的相反評論被評爲高於實際答案。我會考慮重新發布您的答案以解決OP的實際需求。 –

相關問題