2014-02-21 43 views
0

在我的应用程序活动中,我有8个按钮,1个textView,2个相对布局和1个线性布局(父级)。
获取布局重量属性的屏幕坐标 - android

这是什么样子:

这里的结构:

<线性布局>
......... <相对布局1 .....重量= 0.5>
................. <按钮1 >
................. <按钮2>
................. <按钮3>
。 ................ <按钮4>
................. < TextView“触摸按钮”>
。 ........ < \相对布局>

......... <相对布局2 .....重量= 0.5>
................. <按钮5>
................. <按钮6>
.... ............. <按钮7>
................. <按钮8>
........ 。< \相对布局>
<线性布局>

正如你可能已经从标题猜到了,我试图让所有的8个按钮的坐标和检测如果我的手指在屏幕上移动时遇到它们中的任何一个。

我能够使用下面给出的代码获得按钮的坐标,但它不能正常工作。根据我的代码,按钮应该改变他们的背景颜色为红色,一旦他们被触摸或悬停。但是现在发生的事情是,按钮5,6,7和8不能识别我的触摸,1,2,3,4会识别它,但它们也会改变它们的平行按钮的颜色。例如,如果我触摸B8,则什么也没有发生,但是如果我触摸B4,它会随B8一起变色。

为了更好地解释我在下面放置了一张图片。此图片代表当我触摸到B4发生了什么:

这究竟是为什么?可能是什么概念?请帮我解决它。

这是我的代码: Main_Activity。java的

public class MainActivity extends Activity { 

MyButton b1, b2, b3, b4,b5,b6,b7,b8; 

private TextView buttonIndicator; 
private LinearLayout touchview; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    buttonIndicator = (TextView) findViewById(R.id.textView); 
    touchview = (LinearLayout) findViewById(R.id.linearlayout); 

    b1 = (MyButton) findViewById(R.id.button1); 
    b2 = (MyButton) findViewById(R.id.button2); 
    b3 = (MyButton) findViewById(R.id.button3); 
    b4 = (MyButton) findViewById(R.id.button4); 
    b5 = (MyButton) findViewById(R.id.button5); 
    b6 = (MyButton) findViewById(R.id.button6); 
    b7 = (MyButton) findViewById(R.id.button7); 
    b8 = (MyButton) findViewById(R.id.button8); 

    RelativeLayout touch1 = (RelativeLayout) findViewById(R.id.touchview1); 
    RelativeLayout touch2 = (RelativeLayout) findViewById(R.id.touchview2); 
    touch1.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      return false; // To pass touch event to linear layout parent 
     } 
    }); 
    touch2.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      return false; // To pass touch event to linear layout parent 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    touchview.setOnTouchListener(new View.OnTouchListener() { 

     private boolean isInside = false; 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      int x = (int) event.getX(); 
      int y = (int) event.getY(); 

      if (isPointWithin(x, y, b1.getLeft(), b1.getRight(), b1.getTop(),b1.getBottom())) { 
       b1.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 1"); 
      } 

      if (isPointWithin(x, y, b2.getLeft(), b2.getRight(), b2.getTop(),b2.getBottom())) { 
       b2.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 2"); 
      } 

      if (isPointWithin(x, y, b3.getLeft(), b3.getRight(), b3.getTop(),b3.getBottom())) { 
       b3.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 3"); 
      } 

      if (isPointWithin(x, y, b4.getLeft(), b4.getRight(), b4.getTop(),b4.getBottom())) { 
       b4.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 4"); 
      } 

      if (isPointWithin(x, y, b5.getLeft(), b5.getRight(), b5.getTop(),b5.getBottom())) { 
       b5.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 5"); 
      } 

      if (isPointWithin(x, y, b6.getLeft(), b6.getRight(), b6.getTop(),b6.getBottom())) { 
       b6.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 6"); 
      } 

      if (isPointWithin(x, y, b7.getLeft(), b7.getRight(), b7.getTop(),b7.getBottom())) { 
       b7.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 7"); 
      } 

      if (isPointWithin(x, y, b8.getLeft(), b8.getRight(), b8.getTop(),b8.getBottom())) { 
       b8.setBackgroundColor(Color.RED); 
       buttonIndicator.setText("Button 8"); 
      } 

      return true; 
     } 

    }); 

    } 

    //The boolean below determines the exact position of the finger on the screen 
    static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) { 
    return (x <= x2 && x >= x1 && y <= y2 && y >= y1); 
    } 
} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:weightSum="1.0" 
android:background="#ffffff" > 

<RelativeLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/touchview1" 
    android:layout_weight="0.5"> 

<com.example.coordinates.MyButton 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:text="B1" 
    android:layout_marginBottom="10dp" 
    android:textColor="#000000" /> 

<com.example.coordinates.MyButton 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/button1" 
    android:layout_marginBottom="10dp" 
    android:text="B2" 
    android:textColor="#000000" /> 

<com.example.coordinates.MyButton 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/button2" 
    android:text="B3" 
    android:layout_marginBottom="10dp" 
    android:textColor="#000000" /> 

<com.example.coordinates.MyButton 
    android:id="@+id/button4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/button3" 
    android:text="B4" 
    android:textColor="#000000" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Touch a button" 
     android:layout_marginLeft="10dp" 
     android:layout_marginBottom="10dp" 
     android:id="@+id/textView" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/touchview2" 
    android:layout_weight="0.5"> 


    <com.example.coordinates.MyButton 
     android:id="@+id/button5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="B5" 
     android:layout_marginBottom="10dp" 
     android:textColor="#000000" /> 

    <com.example.coordinates.MyButton 
     android:id="@+id/button6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button5" 
     android:layout_marginBottom="10dp" 
     android:text="B6" 
     android:textColor="#000000" /> 

    <com.example.coordinates.MyButton 
     android:id="@+id/button7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button6" 
     android:text="B7" 
     android:layout_marginBottom="10dp" 
     android:textColor="#000000" /> 

    <com.example.coordinates.MyButton 
     android:id="@+id/button8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button7" 
     android:text="B8" 
     android:textColor="#000000" /> 

    </RelativeLayout> 

</LinearLayout> 

MyButton.java

public class MyButton extends Button { 
//I am using cutom Button to avoid ontouch for buttons and return false 
public MyButton(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
} 

public MyButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
} 

public MyButton(Context context) { 
    super(context); 
    // // TODO Auto-generated constructor stub 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // TODO Auto-generated method stub 
    // return super.onTouchEvent(event); 
    return false; 
} 

} 

我已经尽我所能来解释我的问题。请帮帮我!
非常感谢大家。

编辑:我被建议使用View.getLocationOnScreen()。任何人都可以请解释我如何获得(x1和x2)&(y1和y2)坐标在这种方法的帮助下?我认为下面的图片更好地解释了它!

+0

为什么不直接在按钮上尝试onCLickListener或onTouchlistener并在事件执行时更改其背景?如果你的要求超过这个,请原谅我。 –

+0

@Atul,是的,我的要求远不止于此。没问题。请帮我找一个解决方案! –

+0

看看这是否有帮助。 http://stackoverflow.com/questions/3619693/getting-views-coordinates-relative-to-the-root-layout –

回答

1

getLeft()返回父窗口的左边界。你也应该递归地添加所有父母的getLeft()。

解释方式不同:b4和b8对getLeft()调用返回相同的数字。如果你希望自己的绝对的屏幕坐标,看到

How to get the absolute coordinates of a view

int pos[] = new int[2]; 
button.getLocationOnScreen(pos); 
int x1 = pos[0], y1 = pos[1]; 
int x2 = x1 + button.getWidth(); 
int y2 = y1 + button.getHeight(); 
+0

所以我应该使用'View.getLocationOnScreen();'?但是,我如何获得所有4个坐标?你能否参考我的问题编写代码? –

+0

请检查编辑!谢谢! –

+1

对不起,不早回答,正在睡觉:)。无论如何:getLocationOnScreen接受两个整数的数组,分别接收x和y坐标。所以你得到X1,Y1。得到X2,Y2,只计算X2 = X1 + getWidth()和Y2 = Y1 + getHeight() – velis

0

检查这个代码

private boolean checkInterSection(View view, int rawX, int raxY) { 
int[] location = new int[2]; 
view.getLocationOnScreen(location); 
int x = location[0]; 
int y = location[1]; 
int width = view.getWidth(); 
int height = view.getHeight(); 
//Check the intersection of point with rectangle achieved 
return (!(rawX < x || rawY > x + width || rawY < y || rawY > y + height)); } 

for(int i = 0; i < touchview.getChildCount(); i++){ 
if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){ 
    if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){ 
     ((Button)touchview.getChildAt(i)).setBackgroundColor(Color.BLUE);// Type casting may not be required 
    }else{ 
     ((Button)touchview.getChildAt(i)).setBackgroundColor(Color.WHITE); 
    } 
    break; 
} } 

this链接。并查看@Rajesh CP发表的答案。我想这就是你需要的希望,这可以帮助你。

+0

我很困惑。你能否参照我的按钮编写代码并发布?请?谢谢! –

+0

请检查编辑谢谢! –

+0

检查我编辑的答案。 –