Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:26:34+00:00 2026-06-05T12:26:34+00:00

I’m developing the Android application for drawing. So, I NEED to use MVC pattern

  • 0

I’m developing the Android application for drawing. So, I NEED to use MVC pattern for it. I have View class which the application use for drawing:

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();
    }
}

Main activity:

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

And Painter class which keeps all algorithms (Model). Please, note, all algorithms work good:

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

But I have some problems: when I touch by screen and draw then it works, but if I up my finger from screen then the screen will be black again! I.e. the application doesn’t save the result of drawing. So, if I add this lines into onDraw method:

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

then it works! But I don’t understand why! The application uses the same links, why I need to change it again after drawing? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:26:37+00:00Added an answer on June 5, 2026 at 12:26 pm

    Android implicity follows MVC pattern, so you just need not bother.

    MVC pattern on Android

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
Thanks in advance for your help. I have a need within an application to
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.