2014-03-24 167 views
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(); 
     } 
} 
+0

嗨,看起来好像视图可能会在y方向相反需要发生之前在x方向上离开屏幕。此外,(j> = margenmaxy - 高度)包括(j batbrat

+0

@batbrat嗨,球不会离开屏幕它从上到下都很好,但我想要当它到达下来的位置,它必须向另一个方向向上 –

+0

所以它一直向下移动,直到它离开屏幕?图像底部接触屏幕底部时停止移动......? – batbrat

回答

0

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

(j >= margenmaxy - height) 

这意味着自动 (j < margenmaxy) 因为margenmaxy,和高度均为阳性。当您检查这样的条件:

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 Property Animation以及所需的插补器。如果你只想做一次,View Animation也可以。

+0

我试试这段代码并且让你知道 –

+0

你真的应该看看Property Animation ... :) – batbrat

+0

感谢你给这段代码但是球没有移动 –

相关问题