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 8847013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:07:15+00:00 2026-06-14T12:07:15+00:00

I would like to draw an image (as a family picture i.e.) inside another

  • 0

I would like to draw an image (as a family picture i.e.) inside another image (as a frame).

I’m using ImageView to handle this. I can drag my background image around, but the View didn’t draw the front image again.

This is my code for loading two images. mFrontImage is the ‘frame’ and mBackImage is the ‘background’ which we will drag. These lines of code have no issue.

// Create a new bitmap scaled from original bitmap
mFrontImage = Bitmap.createBitmap(bmpTemp, 0, 0, fw, fh, fmatrix, true);

mCanvas = new Canvas(mFrontImage);
mPaint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_OVER));
mCanvas.drawBitmap(mFrontImage, 0, 0, mPaint);
mCanvas.drawBitmap(mBackImage, 0, 0, mPaint);

mImageV = (ImageView) this.findViewById(R.id.image_view);
mImageV.setImageBitmap(mFrontImage);
mImageV.setOnTouchListener(this);

And these are code to handle touch movement:

case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
_moving = true;
break;

case MotionEvent.ACTION_MOVE:
if (_moving)
{
    dx = event.getX() - downx;
    dy = event.getY() - downy;
    downx = event.getX();
    downy = event.getY();
    x += dx;
    y += dy;

    mCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
    mCanvas.drawBitmap(mFrontImage, 0, 0, mPaint);
    mCanvas.drawBitmap(mBackImage, x, y, mPaint);
    mImageEdit.invalidate();
}
break;

case MotionEvent.ACTION_UP:
_moving = false;
break;

The line drawColor will erase the canvas then drawBitmap(mBackImage ~) but not drawBitmap(mFrontImage ~).

What I want to achieve is drawing both mFrontImage at 0, 0 and mBackImage at new position x, y.

  • 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-14T12:07:15+00:00Added an answer on June 14, 2026 at 12:07 pm

    I think there’s no way other than create new class implement SurfaceView. I did try and succeed. Here’s my sample code for those who wanted to try similar way.

    This code is combined from many sources over internet and myself implementation. You can load both images to the view, then resize/move the background image. I did try rotate but still not finish it yet.

    class MyCanvasView extends SurfaceView implements SurfaceHolder.Callback {
        private DrawingThread _thread;
        private Bitmap _frontbmp;
        private Bitmap _backbmp;
        private Matrix _matrix;
        float x, y, downx, downy, oldx, oldy;
        float dx, dy, bw, bh;
        boolean _moving = false;
        ScaleGestureDetector sgdetector;
        float scalefactor = 1.0f;
        float oldscalefactor = 1.0f;
    
        public MyCanvasView(Context context) {
            super(context);
            x = oldx = 0;
            y = oldy = 0;
            getHolder().addCallback(this);
            _thread = new DrawingThread(getHolder(), this);
            setFocusable(true);
        }
    
        public void initView(Bitmap front, Bitmap back){
            _matrix = new Matrix();
            sgdetector = new ScaleGestureDetector(getContext(), new ScaleListener());
            _frontbmp = front;
            _backbmp = back;
            bw = _backbmp.getWidth();
            bh = _backbmp.getHeight();
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
                int action = event.getAction();
                sgdetector.onTouchEvent(event);
                switch (action & MotionEvent.ACTION_MASK)
                {
                    case MotionEvent.ACTION_DOWN:
                        downx = event.getX();
                        downy = event.getY();
                        if((downx > oldx && downx < (oldx + bw)) && (downy > oldy && downy < (oldy + bh)))
                        {
                            _moving = true;
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (_moving && !sgdetector.isInProgress())
                        {
                            dx = event.getX() - downx;
                            dy = event.getY() - downy;
                            downx = event.getX();
                            downy = event.getY();
                            oldx = x;
                            oldy = y;
                            x += dx;
                            y += dy;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        _moving = false;
                        oldx = x;
                        oldy = y;
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        _moving = false;
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    default:
                        break;
                }
                return true;
            }
        }
    
        @Override
        public void onDraw(Canvas canvas) {
            if (_backbmp != null && _frontbmp != null){
                canvas.drawColor(Color.WHITE);
                _matrix.postTranslate(x - oldx, y - oldy);
                _matrix.postScale(scalefactor/oldscalefactor, scalefactor/oldscalefactor, x + bw/2, y + bh/2);
                canvas.drawBitmap(_backbmp, _matrix, null);
                oldscalefactor = scalefactor;
                canvas.drawBitmap(_frontbmp, 0, 0, null);
            }
    
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
            _thread.setRunning(true);
            _thread.start();
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            // we have to tell thread to shut down & wait for it to finish, or else
            // it might touch the Surface after we return and explode
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    
        class DrawingThread extends Thread {
            private SurfaceHolder _surfaceHolder;
            private MyCanvasView _panel;
            private boolean _run = false;
    
            public DrawingThread(SurfaceHolder surfaceHolder, MyCanvasView panel) {
                _surfaceHolder = surfaceHolder;
                _panel = panel;
            }
    
            public void setRunning(boolean run) {
                _run = run;
            }
    
            public SurfaceHolder getSurfaceHolder() {
                return _surfaceHolder;
            }
    
            @Override
            public void run() {
                Canvas c;
                while (_run) {
                    c = null;
                    try {
                        c = _surfaceHolder.lockCanvas(null);
                        synchronized (_surfaceHolder) {
                            _panel.onDraw(c);
                        }
                    } finally {
                        if (c != null) {
                            _surfaceHolder.unlockCanvasAndPost(c);
                        }
                    }
                }
            }
        }
    
        class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                oldx = x;
                oldy = y;
                scalefactor *= detector.getScaleFactor();
                // Don't let the object get too small or too large.
                scalefactor = Math.max(0.1f, Math.min(scalefactor, 5.0f));
                return true;
            }
        }
    }
    

    Hope someone will find this useful.

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

Sidebar

Related Questions

Instead of setting image using 'background' property, I would like to draw the image
I draw an image in OnRender method of my custom FrameworkElement. I would like
I would like to draw text on a canvas using the Canvas.drawText call for
I would like to draw a line or bar graph using HTML5 canvas. I
I would like to draw a box using SAS/GRAPH with a color gradient (say
I have to design a GUI using Qt. I would like to draw multiple
I would like to write an image labeling tool using PyQT4: load a number
I'm displaying a histogram for a grayscale image and I would like to draw
I would like to draw an image opened with the HTML5 File API on
Using Delphi 2010 I would like to copy a PNG image to the clipboard

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.