0

我想在屏幕上移动图像,我可以这样做,但不正确。图像向下很好,我希望它在移动到屏幕底部后开始向另一个方向向上移动。

这是我尝试过的。在下面的代码中,margenMaXX是屏幕的宽度,是屏幕margenmaxy的高度

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
Thread myThread = new Thread(new UpdateThread());
        myThread.start();

public class UpdateThread implements Runnable {

            @Override
            public void run() {
                //... code to manipulate position
                while (i<margenMaxX){
                    if(j<margenmaxy) {
                    try {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                /*mDrawable.setBounds(i, j ,i+ width, i+ height);
                                 mDrawable.draw(cc);
                                 invalidate();*/
                            }
                        });
                       Thread.sleep(200);
                        i=i+10;
                        j=j+10;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if(j>=margenmaxy-height){
                    try {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                /*mDrawable.setBounds(i, j ,i+ width, i+ height);
                                 mDrawable.draw(cc);
                                 invalidate();*/
                            }
                        });
                       Thread.sleep(200);
                        i=i-10;
                        j=j-10;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                }               
                }
            }

public class AnimatedView extends ImageView {



        public AnimatedView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub


            mDrawable =  new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xffffAC23);


        }
         protected void onDraw(final Canvas cc) {
            final Context context = null;

            mDrawable.setBounds(i, j ,i+ width, i+ height);
            mDrawable.draw(cc);
            invalidate();
                    }
    }

更新1:

使用此代码,球在撞击地面后会向上移动并移动到另一侧。现在,我希望球在击中右边界时回来。我为此做了编码,但它没有回来。我的最终目标是开发一个球必须来自左侧或右侧的游戏。它必须撞到地面并朝相反的方向走,撞到墙上然后回来。只要比赛还在进行,球就必须完成这项工作。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);
    Thread myThread = new Thread(new UpdateThread());
        myThread.start();

    public class UpdateThread implements Runnable {
    boolean mMoveDown=false;
    boolean mMoveUp = false;
        @Override
        public void run() {
            while(!mMoveUp) {
                // Move the image down and right.
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {

                        }
                    });
                   Thread.sleep(200);
                    i=i+10;
                    j=j+10;

                // Try posting a runnable to the UI thread to update the view.


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }if(j >= margenmaxy)
                {
                    // Change to moving up phase.
                    mMoveUp = true;
              }

            }

            while(mMoveUp){
                try {

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                    }
                });
                Thread.sleep(200);
                i=i + 10;
                j=j - 10;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } if(i >= margenMaxX)
                {
                    // Change to moving up phase.
                    mMoveDown = true;
              } 
        }while(mMoveDown){
            try {

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                    }
                });
                Thread.sleep(200);
                i=i - 10;
                j=j + 10;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        }
}

public class AnimatedView extends ImageView {



        public AnimatedView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub


            mDrawable =  new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xffffAC23);


        }
         protected void onDraw(final Canvas cc) {
            final Context context = null;

            mDrawable.setBounds(i, j ,i+ width, j+ height);
            mDrawable.draw(cc);
            invalidate();
        }
}
4

1 回答 1

0

此代码有两个重叠的条件语句。但是,仅当第一个失败时才检查第二个。第二个条件是:

(j >= margenmaxy - height)

这自动意味着 (j < margenmaxy) 因为 margenmaxy 和 height 都是正数。当您检查这样的条件时:

if(j<margenmaxy) {
    // Do downward animation.

} else if(j>=margenmaxy-height){
    // Do upward animation.
}

所发生的情况是,在图像向下移动的阶段会发生您所期望的情况。但是,一旦您尝试将图像向上移动,(j < margenmaxy) 条件就会再次满足,并且代码会尝试将图像向下移动。将其向下移动的那一刻,第一个条件不再有效。由于您在循环中具有此构造,因此会导致“弹跳”行为。

要解决这个问题,您需要更改逻辑。一种简单的方法是让布尔值保持动画的状态。这个状态变量会根据图像是否到达屏幕底部而改变。

public class UpdateThread implements Runnable {

    // Variable to store the animation state.
    boolean mMoveUp = false;
    @Override
    public void run() {

        //... code to manipulate position
        while (i<margenMaxX) {
            // IF the animation is in the moving down phase
            if(!mMoveUp) {
                // Move the image down and right.
                i += 10;
                j += 10;

                // Try posting a runnable to the UI thread to update the view.
                try {
                    runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                            /*mDrawable.setBounds(i, j ,i+ width, i+ height);
                             mDrawable.draw(cc);
                             invalidate();*/
                    }
                    });
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } 
            // We're moving upwards.
            else {
                // Move up and left
                i -= 10;
                j -= 10;

                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            /*mDrawable.setBounds(i, j ,i+ width, i+ height);
                             mDrawable.draw(cc);
                             invalidate();*/
                        }
                    });
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } // Catch!
            } // Else! 

            // Check to see if we've hit the bottom of the screen
            if(j >= margenmaxy - height)
            {
                  // Change to moving up phase.
                  mMoveUp = true;
            }
            // Note, you can use an altered form of this to switch back and forth
            // between moving up and down.
        } // while(i < margenmaxx)!               
    } // run()!
} // UpdateThread!

作为补充说明,由于您正在左右移动球,因此您最终也可能会触及这些边界。您确实应该对此进行更好的检查。作为第二点,这可能不是在 Android 中完成移动图像的最佳方式。最后,更通用的解决方案是将 Android属性动画与您想要的插值器一起使用。如果您只想这样做一次,View Animation 也可能就足够了。

于 2014-03-24T12:39:45.780 回答