Question

To those favoriting this Question, give it an upvote too

I'm using a a class that extends View. In this class I have the onDraw method where I do my drawing.

When I launch my app, everything works as intended except for a little annoying thing.. whenever I press anywhere on my screen, the drawing, which calculates the coordinates of where I touched the screen - and is supposed to draw the image in those exact coordinates, draws them a little bit off of where I originally pressed.

For example, if I tap the screen at x = 150, y = 250, the image will be displayed off by a significant amount of pixels.

To be clear, when I press the screen, I want my image displayed exactly under my finger or in very close proximity to where I tapped it, however, the image is displayed usually 100 or so pixels below where the press occured.

Is there something that I do not know about coordinates when using a canvas to draw?

Here is my code

public class PixelParticle extends View{

    private float locationX;
    private float locationY;
    private int sizeX;
    private int sizeY;
    private Paint color;
    private Rect rect;
    private int paintcolor;
    private float myLocX;
    private float myLocY;


    public PixelParticle(Context context, float locationX, float locationY, int sizeX, int sizeY, int paintcolor) {
        super(context);

        this.locationX = locationX;
        this.locationY = locationY;
        this.sizeX = sizeX;
        this.sizeY = sizeY;
        this.paintcolor = paintcolor;

        rect = new Rect();
        color = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 250, true);
        canvas.drawBitmap(resizedBitmap,locationX ,locationY , null);
    }
}

And the MainActivity..

public class MainActivity extends Activity {

    private int pixelColor;
    private float x;
    private float y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

     @Override
        public boolean onTouchEvent(MotionEvent event) {
            x = event.getX();
            y = event.getY();

            pixelColor = Color.RED;
            PixelParticle pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
            setContentView(pp); 
            return false;
        }
}
Was it helpful?

Solution

The height of the screen includes the title and the notification bar. Make the Activity Full Screen and remove the title will solve the issue. Try this:

PixelParticle...

    public class PixelParticle extends View {

private float locationX;
private float locationY;
private int sizeX;
private int sizeY;
private Paint color;
private Rect rect;
private int paintcolor;
private float myLocX;
private float myLocY;

public PixelParticle(Context context, float locationX, float locationY,
        int sizeX, int sizeY, int paintcolor) {
    super(context);

    this.locationX = locationX;
    this.locationY = locationY;
    this.sizeX = sizeX;
    this.sizeY = sizeY;
    this.paintcolor = paintcolor;

    rect = new Rect();
    color = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 250,
            true);
    canvas.drawBitmap(resizedBitmap, locationX
            - (resizedBitmap.getWidth() / 2),
            locationY - (resizedBitmap.getHeight() / 2), null);

}}

MainActivity..

    public class MainActivity extends Activity {

private int pixelColor;
private float x;
private float y;
PixelParticle pp ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();

        pixelColor = Color.RED;
         pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
        setContentView(pp); 
        return false;
    }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top