0

我想喲做一個for循環的重複塊,但是,他們的一些價值觀有所不同的xy位置,我如何使他們,使我仍然可以使用for循環,並以某種方式管理不同內部的for循環。這裏XY位置是我的代碼:如何在android中爲以下代碼製作動態循環?

m_views.add(hm7); 

     HeatMapElementView hm8 = (HeatMapElementView)view.findViewById(R.id.hmEle_08); 
     hm8.setTag(new Integer[]{Integer.valueOf(4),Integer.valueOf(2)}); 
     hm8.setOnClickListener(
       new OnClickListener(){ 
        @Override 
        public void onClick(View vw){ 
         if(isAnimating) 
          return; 

         RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)m_hoverCraft.getLayoutParams(); 

         //int xColumn = ((Integer[])vw.getTag())[0].intValue(); 
         relParams.leftMargin = ((int)vw.getX())+((int)vw.getWidth()) - 450; 
         int yRow = ((Integer[])vw.getTag())[1].intValue(); 
         relParams.topMargin = ((int)(vw.getHeight()*yRow) - (vw.getHeight()/2) - 162); 

         m_hoverCraft.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
         m_hoverCraft.setTag(new float[]{1.0f,0.5f}); 
         Animation anim = new ScaleAnimation(
           0f, 1f, // Start and end values for the X axis scaling 
           0f, 1f, // Start and end values for the Y axis scaling 
           Animation.RELATIVE_TO_SELF, 1.0f, // Pivot point of X scaling 
           Animation.RELATIVE_TO_SELF, 0.5f); 
         showHoverView(vw,anim); 
        } 
       } 
     ); 
     //hm8.setOnHoverListener(heatMapHover); 
     m_views.add(hm8); 

     HeatMapElementView hm9 = (HeatMapElementView)view.findViewById(R.id.hmEle_09); 
     hm9.setTag(new Integer[]{Integer.valueOf(1),Integer.valueOf(3)}); 
     hm9.setOnClickListener(
       new OnClickListener(){ 
        @Override 
        public void onClick(View vw){ 
         if(isAnimating) 
          return; 

         RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)m_hoverCraft.getLayoutParams(); 

         //int xColumn = ((Integer[])vw.getTag())[0].intValue(); 
         relParams.leftMargin = (int)vw.getX(); 

         int yRow = ((Integer[])vw.getTag())[1].intValue(); 
         relParams.topMargin = ((int)(vw.getHeight()*yRow) - (vw.getHeight()/2) - 162); 

         m_hoverCraft.setTag(new float[]{0.0f,0.5f}); 
         m_hoverCraft.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT); 
         Animation anim = new ScaleAnimation(
           0f, 1f, // Start and end values for the X axis scaling 
           0f, 1f, // Start and end values for the Y axis scaling 
           Animation.RELATIVE_TO_SELF, 0.0f, // Pivot point of X scaling 
           Animation.RELATIVE_TO_SELF, 0.5f); 
         showHoverView(vw,anim); 
        } 
       } 
     ); 
     //hm9.setOnHoverListener(heatMapHover); 
     m_views.add(hm9); 

     HeatMapElementView hm10 = (HeatMapElementView)view.findViewById(R.id.hmEle_10); 
     hm10.setTag(new Integer[]{Integer.valueOf(2),Integer.valueOf(3)}); 
     hm10.setOnClickListener(
       new OnClickListener(){ 
        @Override 
        public void onClick(View vw){ 
         if(isAnimating) 
          return; 

         RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)m_hoverCraft.getLayoutParams(); 

         //int xColumn = ((Integer[])vw.getTag())[0].intValue(); 
         relParams.leftMargin = (int)vw.getX() + (int)vw.getWidth()/2 - 225; 

         int yRow = ((Integer[])vw.getTag())[1].intValue(); 
         relParams.topMargin = ((int)(vw.getHeight()*yRow) - (vw.getHeight()/2) - 162); 

         m_hoverCraft.setTag(new float[]{0.5f,0.5f}); 
         m_hoverCraft.setGravity(Gravity.CENTER); 
         Animation anim = new ScaleAnimation(
           0f, 1f, // Start and end values for the X axis scaling 
           0f, 1f, // Start and end values for the Y axis scaling 
           Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling 
           Animation.RELATIVE_TO_SELF, 0.5f); 
         showHoverView(vw,anim); 
        } 
       } 
     ); 

回答

0

只是包裝區塊的一成簡單的loop..you例如可以創建一個包含所有變量的對象,並創建這些對象的數組。 或者你可以創建漂浮物陣列(或其他任何東西)......像這樣

int params[] = {1, 2, 3, 4, 5}; 


for (int i = 0; i < 4; i++) { 

    //your code 
    // . 
    // . 
    // . 
    someFunctionWithParameter(params[i]); 
    // . 
    // . 
    //. 

}