문제

I want to draw a rectangle on a position give it by my finger touching on the screen, but when I click on the button to draw, the rectangle's location is wrong, to many pixels on the right and up. I post the code and my xml layout file, also I give and Image of the "touch event" and the "rectangle location" of this touch event's, it is a capture of my imageView on the ADV emulator.

OnTouch Event:

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

Event for drawing:

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 Layout File:

<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: Image of my screen when I make some touch events and some draw events:

도움이 되었습니까?

해결책 2

I found the solution! The canvas had a different width and height than the imageView, so the point location when I touch the screen was different for the canvas and for the imageView. Fix it giving the same width and height!

다른 팁

So the above solutions did not help me with the same issue however interestingly enough when I multiplied the "event.getX()" by 4 it was exactly correct; this is more like treating a side affect and more research is needed as to why.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top