2015-09-27 66 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; 
}