2014-03-24 168 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

感謝你給這段代碼但是球沒有移動 –

相關問題