2015-09-27 97 views
0

嗨,我根據我的圖像視圖獲取座標值。所以,現在我試圖在全局使用這些座標值,但是我的方法返回了零(默認/初始值),但是它在方法內部打印了正確的值。請幫我..在android中返回默認值/初始值的方法

代碼: MainActivity.java

public class StackActivity extends ActionBarActivity { 

ImageView firstDot, middleDot, lastDot; 

int distaceHoriz; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stack); 

    firstDot = (ImageView) findViewById(R.id.img_dot_starting); 
    middleDot = (ImageView) findViewById(R.id.img_dot_middle); 
    lastDot = (ImageView) findViewById(R.id.img_dot_last); 

    distance(firstDot); 
    Log.e("distaceHorizFirst", distaceHoriz+""); 

    distance(middleDot); 
    Log.e("distaceHorizMiddle", distaceHoriz+""); 

    distance(lastDot); 
    Log.e("distaceHorizLast", distaceHoriz+""); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.stack, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public int distance(final View view) { 


    view.getViewTreeObserver().addOnGlobalLayoutListener(
      new OnGlobalLayoutListener() { 


       @Override 
       public void onGlobalLayout() { 

        view.getViewTreeObserver() 
          .removeGlobalOnLayoutListener(this); 

        int[] locations = new int[2]; 
        view.getLocationOnScreen(locations); 
        distaceHoriz = locations[0]; 
        // int vertDistanceToLastDot = locations[1]; 

        Log.e("distaceHoriz:", ""+ distaceHoriz); 

       } 
      }); 
    return distaceHoriz; 

} 
} 

MainLayout.xml

<ImageView 
    android:id="@+id/img_dot_starting" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginTop="42dp" 
    android:layout_alignParentLeft="true" 
    android:src="@drawable/icon_round" /> 

<ImageView 
    android:id="@+id/img_dot_middle" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginTop="42dp" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/icon_round" /> 

<ImageView 
    android:id="@+id/img_dot_last" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginTop="42dp" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/icon_round" /> 
+0

嗯,這是因爲「OnGlobalLayoutListener」的異步特性。 'onGlobalLayout()'的內容不會立即執行,而是在不久的將來某個時候執行。然而,您的'distance()'方法同步運行,並立即返回一個默認值(因爲實際賦值的代碼尚未執行)。我認爲你錯誤地認爲通過讓聽衆進入你自己的方法,它的回調就會立即執行。事實並非如此。 –

回答

0

嘗試使用addonPreDrawListener

public int distance(final View view) { 
int distaceHoriz; 
    ViewTreeObserver vto = view.getViewTreeObserver(); 
     vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
      public boolean onPreDraw() { 
       view.getViewTreeObserver().removeOnPreDrawListener(this); 
       int[] locations = new int[2]; 
        view.getLocationOnScreen(locations); 
        distaceHoriz = locations[0]; 
        // int vertDistanceToLastDot = locations[1]; 

        Log.e("distaceHoriz:", ""+ distaceHoriz); 
      } 
     }); 
return distaceHoriz; 
}