I've got problem with getting coordinates of view. I've got three custom views placed in LinearLayout. When I touch any of them I've got wrong coordinates. For example second view is placed in the center, but getLocationOnScreen() returns x = 0. This is impossible. And this coordinates always differs from values that i can get through event.getX() and event.getY().
Please, could you tell me, what is the problem?
Results (through Log.w)
First view: onTouch_coords: 0, 50
Second view: onTouch_coords: 0, 98
Third view: onTouch_coords: 0, 146
This is gui xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.remaller.android.DragAndDropBasicsInheriting.DragableView
android:id="@+id/DragableView1"
android:layout_height="wrap_content"
android:src = "@drawable/icon" android:layout_width="wrap_content"/>
<com.remaller.android.DragAndDropBasicsInheriting.DragableView
android:id="@+id/DragableView2"
android:layout_height="wrap_content"
android:src = "@drawable/icon" android:layout_width="match_parent"/>
<com.remaller.android.DragAndDropBasicsInheriting.DragableView
android:id="@+id/DragableView3"
android:src = "@drawable/icon" android:layout_width="wrap_content" android:layout_height="match_parent"/>
</LinearLayout>
This is source code for my DragableView:
public class DragableView extends ImageView
{
private void init()
{
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
int this_coords[] = {0,0};
this.getLocationOnScreen(this_coords);
Log.w("daxh","onTouch_coords: "+this_coords[0]+", "+this_coords[1]);
switch(action)
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
default:
break;
}
return super.onTouchEvent(event);
}
public DragableView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public DragableView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public Dra开发者_JAVA技巧gableView(Context context)
{
super(context);
init();
}
}
This is the code for activity:
public class DragAndDropBasicsInheritingActivity extends Activity
{
public static final int CREATE = 0x00000001;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.draganddropbasicsinheritingactivity_gui);
}
}
this.getLocationOnScreen(this_coords);
it always returns the location of this
View, a.k.a DragableView
. method specification here..
As this View is your main view, it would definitely show x=0
event.getX()
gave you the coordinate x of where your performed the event like touch,drag...
That's two different methods to two Objects.
精彩评论