方法在android中返回默认值/初始值

时间:2015-09-27 06:07:16

标签: java android methods return-value

嗨我根据我的图像视图得到了坐标值。所以,现在我正在尝试全局使用这些坐标值,但我的方法返回零(默认/初始)值,但它在方法内部打印正确的值。请帮帮我..

代码: 的 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" />

1 个答案:

答案 0 :(得分: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;
}