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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:10:20+00:00 2026-05-23T08:10:20+00:00

I’m new in android. I want to move an image from left to right

  • 0

I’m new in android. I want to move an image from left to right and right to left.When user touch right side image of the image it will move right side.Same thing will happen when user will touch right side of the image.But image will move predefine point across x-axis.

For example: Image will move p1(100,100), p2(150,100), p3(200,100), p4(250,100) across those points sequentially. If user touch left side of p1, it will remain current position.Same thing will occurs at p4. I can move image from p1 to p2 and p2 to p1. when I add p3 and p4 it’s not working as I expected.

Here is my GameView class.

public class GameView extends SurfaceView{
    private Bitmap BG_image;
    private SurfaceHolder holder;
    //private GameLoopThread gameLoopThread;    
    int x;
    int y;
    private int srcX=100;
    private int srcY=100;
    int replaceX=0,areaThreshold=50;
    int distance=50;

    public GameView(Context context) {

        super(context);
        //gameLoopThread = new GameLoopThread(this);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

/*              boolean retry = true;
                gameLoopThread.setRunning(false);

                while (retry) {

                    try {

                        gameLoopThread.join();
                        retry = false;

                    } catch (InterruptedException e) {

                    }
                }*/
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

/*              gameLoopThread.setRunning(true);
                gameLoopThread.start();*/
                Canvas c = holder.lockCanvas(null);
                onDraw(c);
                holder.unlockCanvasAndPost(c);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,

                    int width, int height) {
            }
        });
        BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false);
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
               // x = (int) ev.getX();
               // y = (int) ev.getY();
                updateX(srcX);
                replaceX=(int)ev.getX();
                int StartX=0, EndX=0;
                StartX=replaceX-areaThreshold;
                EndX=replaceX+areaThreshold;
                if(StartX<=100){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=100;
                }
                else if(StartX>100 && StartX<250){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=srcX+distance;
                }
                if(EndX>100 && EndX<250){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=srcX-distance;
                }
                else if(EndX>250){
                    SurfaceHolder holder=getHolder();
                    Canvas myCanvas=holder.lockCanvas(null);
                    onDraw(myCanvas);
                    holder.unlockCanvasAndPost(myCanvas);
                    srcX=250;
                }

        return super.onTouchEvent(ev);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub  
        updateX(srcX);
        Paint paint = new Paint();
        canvas.drawColor(Color.BLACK);
        canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint);
        canvas.drawBitmap(BG_image, srcX, srcY, null);
    }

    private int updateX(int srcX){
        this.srcX =srcX;
        return srcX ;

    }
}

Please review my code and give your valuable advice to solve my problem.
Hopefully, I will get a reply soon!

  • 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-05-23T08:10:21+00:00Added an answer on May 23, 2026 at 8:10 am

    Now, I understand my problem. Thank you very much to give your suggestion.
    Here is my updated code.

        public class GameView extends SurfaceView{
        private Bitmap BG_image;
        private SurfaceHolder holder;
        int x=100;
        private int srcX=100;
        private int srcY=100;
        int distance=50;
    
        public GameView(Context context) {
    
            super(context);
            holder = getHolder();
            holder.addCallback(new SurfaceHolder.Callback() {
    
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
    
                }
    
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
    
                    Canvas c = holder.lockCanvas(null);
                    onDraw(c);
                    holder.unlockCanvasAndPost(c);
                }
    
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format,
    
                        int width, int height) {
                }
            });
            BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
            Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false);
        }
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            // TODO Auto-generated method stub
            x = (int) ev.getX();        
            if(x-srcX>0)
                srcX += distance;
            else if(x-srcX<0)
                srcX -= distance;
    
            if(srcX<=100)
                srcX = 100;
            else if(srcX>250)
                srcX = 250;     
            Log.w("ev.getX : srcX",x+" : "+srcX);
            SurfaceHolder holder=getHolder();
            Canvas myCanvas=holder.lockCanvas(null);
            onDraw(myCanvas);
            holder.unlockCanvasAndPost(myCanvas);
            return super.onTouchEvent(ev);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub  
            Paint paint = new Paint();
            canvas.drawColor(Color.BLACK);
            canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint);
            canvas.drawBitmap(BG_image, srcX-BG_image.getWidth()/2, srcY, null);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
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 French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img

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.