Android正在使用imageView的错误位置

时间:2014-02-06 11:48:04

标签: java android location draw ontouchevent

我想在一个位置上绘制一个矩形,用我的手指触摸屏幕,但是当我点击按钮进行绘制时,矩形的位置是错误的,右侧和右侧的许多像素。我发布了代码和我的xml布局文件,同时我给出了触摸事件的“触摸事件”和“矩形位置”的图像,它是我在ADV模拟器上捕获的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:

2 个答案:

答案 0 :(得分:0)

我找到了解决方案!画布的宽度和高度与imageView不同,因此当我触摸屏幕时,画布和imageView的点位置不同。修复它给出相同的宽度和高度!

答案 1 :(得分:0)

因此,上述解决方案并没有帮助我解决同一问题,但有趣的是,当我将“ event.getX()”乘以4时,这是完全正确的;这更像是治疗副作用,还需要更多关于原因的研究。