简体   繁体   English

如何在Android的框架布局中获得图像视图的左上角?

[英]How to get left and top of image view in frame layout for android?

I need to get left an top of an image view which is in frame layout while selecting the spinner. 选择微调框时,需要在框架布局中保留图像视图的顶部。 This is my code. 这是我的代码。

      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); 

    }
 }
    }); 

When i use like this its works finely. 当我这样使用时,它的效果很好。

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

But when i use intent in else if part it doesn't get X and Y values. 但是当我在其他部分使用intent时,它不会获得X和Y值。 The methods return as zero. 方法返回零。 How can i resolve this? 我该如何解决? Can anybody tell me? 有人可以告诉我吗? Thanks in advance. 提前致谢。

Here is a sample code which i used to get the co-ordinates of a view :::: 这是我用来获取视图::::坐标的示例代码。

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;
}

And I used the method this way:::::: 我以这种方式使用了该方法:::::::

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

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

Note ::: I had to get the middle point of teh imageview which i touched.Hope it helps. 注意:::我必须得到我碰过的imageview的中间点。希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM