将View Click坐标从世界坐标转换为local?

时间:2012-02-28 09:06:19

标签: android android-layout

我在点击View时这样做我试图获得点击视图的坐标。现在我在世界合作中获得了价值,我想在当地的View坐标中得到它 检查下面的代码

public void objectClick(View objectClicked)
     {

         int [] locationOfViewInWindow=new int[2];
         int Object = objectClicked.getId();
         View img= (View)findViewById(Object);
         objectClicked.getLocationInWindow(locationOfViewInWindow);
        Log.d("Check:","locn in window" + locationOfViewInWindow[0]+ "  "+ locationOfViewInWindow[1]);
        switch (hObject) {


    case R.id.imageView1:   
            case R.id.imageView2:...... 
                    case.R.id.imageview11:
        ImageView drawable= (ImageView)findViewById(objectClicked.getId());
                Drawable d = drawable.getDrawable();

            boolean flag = d.getTransparentRegion().contains(locationOfViewInWindow[0], locationOfViewInWindow[1]);

                Log.d("Check", "Opacity" + flag);


boolean flag is returning me null because the region of transparent it is checking w.r.t to world co-ordinate 

Suggestion is appreciated 

1 个答案:

答案 0 :(得分:0)

您应该为您的对象扩展View类,并使用onTouchEvent获取点击位置:

public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();

switch (eventaction) {
    case MotionEvent.ACTION_DOWN: 
        // finger touches the screen
        break;

    case MotionEvent.ACTION_MOVE:
        // finger moves on the screen
        break;

    case MotionEvent.ACTION_UP:   
        // finger leaves the screen
        break;
}

// tell the system that we handled the event and no further processing is required
return true; 

}

您可以使用event.getX()和event.getY()来获取触摸事件的位置。