2014-02-06 35 views
0

我想在一個位置上畫一個矩形給我的手指觸摸屏幕,但是當我點擊按鈕繪製時,矩形的位置是錯誤的,對於很多像素在右邊和上面。我發佈了代碼和我的xml佈局文件,同時給出了此觸摸事件的「觸摸事件」和「矩形位置」的圖像,它是ADV模擬器上我的imageView的捕獲圖像。Android正在繪製錯誤的位置imageView

OnTouch事件:

public boolean onTouch(View view, MotionEvent event) { 

    switch (event.getAction()) 
    {   
     case MotionEvent.ACTION_UP: 
     { 
      x = event.getX(); 
      y = event.getY(); 

      /*int[] img_coordinates = new int[2]; 
      imageView.getLocationOnScreen(img_coordinates); 

      Toast.makeText(getApplicationContext(), String.valueOf(img_coordinates[0]) + " " + String.valueOf(img_coordinates[1]), Toast.LENGTH_LONG).show(); 

      x = x - img_coordinates[0]; 
      y = y - img_coordinates[1];*/ 

      Toast.makeText(getApplicationContext(), String.valueOf(x) + " " + String.valueOf(y), Toast.LENGTH_LONG).show(); 
      imageView.invalidate(); 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: 
     { 
      if (event.getHistorySize() == 0) 
       { 
        x = event.getX(); 
        y = event.getY(); 
       } 

       if (x == 0 && y == 0) 
       { 
        x = event.getX(); 
        y = event.getY(); 
       } 

       canvas.drawLine(x, y, event.getX(), event.getY(), paint); 
       x = event.getX(); 
       y = event.getY(); 
       imageView.invalidate();    
     } 
    }  
    return true; 
} 

事件繪圖:

public void btnMove_Click(View view) 
{ 
    if(orientation == "UP") 
    { 
     canvas.drawLine(x, y, x, y - pixelFoot, paint); 
     Toast.makeText(getApplicationContext(), "Dibujado en " + String.valueOf(x) + " " + String.valueOf(y), Toast.LENGTH_LONG).show(); 
     y -= pixelFoot; 
     imageView.invalidate(); 
    } 
    else if(orientation == "DOWN") 
    { 
     canvas.drawLine(x, y, x, y + pixelFoot, paint); 
     Toast.makeText(getApplicationContext(), "Dibujado en " + String.valueOf(x) + " " + String.valueOf(y), Toast.LENGTH_LONG).show(); 
     y += pixelFoot; 
     imageView.invalidate();   
    } 
    else if(orientation == "LEFT") 
    { 
     canvas.drawLine(x, y, x - pixelFoot, y, paint); 
     Toast.makeText(getApplicationContext(), "Dibujado en " + String.valueOf(x) + " " + String.valueOf(y), Toast.LENGTH_LONG).show(); 
     x -= pixelFoot; 
     imageView.invalidate();   
    } 
    else if(orientation == "RIGHT") 
    { 
     canvas.drawLine(x, y, x + pixelFoot, y, paint); 
     Toast.makeText(getApplicationContext(), "Dibujado en " + String.valueOf(x) + " " + String.valueOf(y), Toast.LENGTH_LONG).show(); 
     x += pixelFoot; 
     imageView.invalidate();   
    } 
} 

XML佈局文件:我的屏幕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 


<ImageView 
    android:id="@+id/imgFingerPaint" 
    android:layout_width="800px" 
    android:layout_height="564px" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/Button01" 
    android:src="@drawable/edittext_rounded_corners" /> 

     <Button 
     android:id="@+id/btnMove" 
     android:layout_width="90px" 
     android:layout_height="90px" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/btnRight" 
     android:layout_toRightOf="@+id/btnRight" 
     android:background="@drawable/edittext_rounded_corners" 
     android:onClick="btnMove_Click" 
     android:text="PASO" /> 

    </RelativeLayout> 

圖片時,我做了一些觸摸事件和一些抽獎活動: Image of my screen when I make some touch events and some draw events:

+0

您的手機對於給定圖像的方向是什麼?另外,什麼是pixelFoot&它來自哪裏? –

+0

不要責怪你的錯誤的機器人:它你在錯誤的位置 – pskink

+0

@dev_feed方向是'風景',pixelFoot是一個變量,我從另一個活動接收並獲得一個步幅的像素距離。 – Imrik

回答

0

我找到了解決方案!畫布的寬度和高度與imageView不同,所以當我觸摸屏幕時,點的位置對於畫布和imageView是不同的。修復它給相同的寬度和高度!