0

在选择微调器时,我需要离开处于框架布局中的图像视图的顶部。这是我的代码。

      location_spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
 String scr;

  @Override
  public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position1, long id) 
  {


     selscr = location_spinner.getSelectedItem().toString();
     src_position = position1;
     System.out.println("selscr--->"+selscr);
    System.out.println("src_position--->"+src_position);


    floor_code=selscr.substring(selscr.length() - 1);
    System.out.println("The selscr"+floor_code);


  }

    @Override
    public void onNothingSelected(AdapterView<?> arg0)
    {
        // TODO Auto-generated method stub

    }  
   });



   get_location_bt.setOnClickListener(new View.OnClickListener() 
  {

    @Override
    public void onClick(View arg0) 
    {
    if(src_position==0)//selscr.equalsIgnoreCase("-Select-") && seldes.equalsIgnoreCase("-Select-"))
    {
        ground_image.setBackgroundResource(R.drawable.gray);
    }

    else if(floor_code.equalsIgnoreCase("G"))
    {


        ground_frame2.setVisibility(View.INVISIBLE);


        new_rad.setVisibility(View.INVISIBLE);
         System.out.println("new_rad before id"+new_rad.getId());
         new_rad = ver.get(src_position-1);
       //new_rad.setBackgroundResource(R.drawable.location_icon);
         new_rad.setVisibility(View.VISIBLE); 
         System.out.println("new_rad after id"+new_rad.getId());
         int x_value = new_rad.getLeft();
         int y_value = new_rad.getTop();
         System.out.println("x_value + y_value---->"+x_value+y_value);
         ground_frame2.setLeft((int) (1500/3.0-x_value));
         ground_frame2.setTop((int)(1119/3.0-y_value));             
         ground_frame2.setVisibility(View.VISIBLE); 

    }
 }
    }); 

当我像这样使用它时,它工作得很好。

  Intent gound_fr = new Intent(LocateStore.this,GroundLoc.class);
  gound_fr.putExtra("src",src_position);

但是当我在 else if 部分中使用意图时,它不会得到 X 和 Y 值。这些方法返回为零。我该如何解决这个问题?谁能告诉我?提前致谢。

4

1 回答 1

1

这是我用来获取视图坐标的示例代码 ::::

public  Rect locateView(View view) {
    Rect loc = new Rect();
    int[] location = new int[2];
    if (view == null) {

        return loc;
    }
        view.getLocationOnScreen(location);

    loc.left = location[0];
    loc.top = location[1];
    loc.right = loc.left + view.getWidth();
    loc.bottom = loc.top + view.getHeight();
    return loc;
}

我以这种方式使用该方法::::::

      Rect r = TVGridUtils.locateView(activity.findViewById(R.id.imageview));                 

            float touchX=r.left+((r.right-r.left)/2);
            float touchY=(r.bottom);

注意::: 我必须得到我触摸的图像视图的中间点。希望它有所帮助。

于 2013-09-23T13:49:00.497 回答