Habibur Rahman
Habibur Rahman

Reputation: 88

How I can move an image from left to right and right to left in android?

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!

Upvotes: 3

Views: 7004

Answers (2)

Chathura Wijesinghe
Chathura Wijesinghe

Reputation: 3349

This is very easy with Android TranslateAnimation

ImageView img_animation = (ImageView) findViewById(R.id.img_animation);

TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
            0.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
    animation.setDuration(5000);  // animation duration 
    animation.setRepeatCount(5);  // animation repeat count
    animation.setRepeatMode(2);   // repeat animation (left to right, right to left )
    //animation.setFillAfter(true);      

    img_animation.startAnimation(animation);  // start animation 

find more details from here move an image from left to right and right to left in android

Upvotes: 5

Habibur Rahman
Habibur Rahman

Reputation: 88

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

Upvotes: 0

Related Questions