0

I have scaled the image to fit the imageview, so now I want to get the rect of imageview so that I can make 1 count for every finger drag made only inside the imageview and not outside. What I have done can be seen below but it is not working because it isn't showing any detection when I drag my finger up and down in the imageview, if it did it should increment 1 for every drag but nothing happens. What am I doing wrong?

Inside onCreate of the main activity:

redShape = (ImageView) findViewById(R.id.image1);
scaleImage(redShape, 300);

Inside Motion Event, Action down:

  rect = new Rect(redShape.getLeft(),redShape.getTop(),redShape.getRight(),redShape.getBottom());

Inside Motion Event, Action move:

    if ((posY > redShape.getTop() || posY <redShape.getBottom()) && rect.contains(redShape.getLeft() + posX, redShape.getTop() + posY)){

Inside the above if statement I have some code that increments 1 for every drag made in a vertical motion (up and down).

EDIT:

This is what I've done but for some reason like before it still detects a drag in the area above the image.

In the onCreate of main activity:

     redShape = (ImageView) findViewById(R.id.image1);

     drawable2 = getResources().getDrawable(R.drawable.red_shape);
     int h = drawable2.getIntrinsicHeight(); 
     int w = drawable2.getIntrinsicWidth();  

     Matrix m = redShape.getImageMatrix();
     RectF drawableRect = new RectF(0, 0, w, h);
     RectF viewRect = new RectF(0, 0, redShape.getWidth(), redShape.getHeight());
     m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL);
     redShape.setImageMatrix(m);

EDIT 2:

 m = redShape.getImageMatrix();

Have the following code in either action down (oldX,oldY touch points) or move (posX,posY touch points):

 float[] touchPoint = new float[] {oldX, oldY};
 m.mapPoints(touchPoint);
Kaal
  • 111
  • 1
  • 10
  • you dont need to scale the `Bitmap`, `ImageView` draws your `Bitmap` so that it fits the `ImageView` depending on `scaleType`, also use `ImageView`'s `Matrix` to do mapping between view<->image points – pskink Aug 09 '15 at 11:57
  • Thank you, how do I use ImageView's matrix to do the mapping between the view and image points? Would it be ok if you provide some code please. – Kaal Aug 09 '15 at 12:05
  • read the Matrix API, in particular map* methods – pskink Aug 09 '15 at 12:07
  • Thank you, could you please check my edit and advise please on where I could be going wrong. – Kaal Aug 09 '15 at 12:27
  • use getImageMatrix() then call mapPoints() with point (0, 0) and see the result of mapping, that should clarify how Matrix works, you can also use mapRect() – pskink Aug 09 '15 at 12:31
  • Thank you, for the map points, should I get the x and y from the action down oldX, oldY or the x and y from the action move, the current positions which are posY and posX? – Kaal Aug 09 '15 at 12:50
  • did you call mapPoints ? – pskink Aug 09 '15 at 13:45
  • See edit 2 for clarification. – Kaal Aug 09 '15 at 14:08

0 Answers0