0

A finger drag in a vertical motion (up/down) should only be detected inside the image of the imageview, not outside. However, in the area above the image it detects the drag. No where else is a drag detected, nor on the left, right or the bottom but only in the area directly above the image. It works inside the image but also outside above it, which it shouldn't do. I've tried a number of things to resolve this but nothing has worked. Can someone please help?

So, in my onCreate in my activity I have the method that was created to scale the image:

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

In Motion event, action down:

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

In action move:

 if (rect.contains(posX, posY)){

In the if statement above I just have the code that counts whenever a drag is made.

EDIT 1:

     drawableHeight = drawable2.getIntrinsicHeight(); 
     drawableWidth = drawable2.getIntrinsicWidth(); 

     drawableRect = new RectF(0, 0, drawableWidth, drawableHeight);
     m.mapRect(drawableRect);

Do I do this next (by the way, redShape is the imageview):

     redShape.setImageMatrix(m);

In rect.contains(posX,posY) do I change that to drawableRect.contains(posX,posY)?

EDIT 2:

    public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);


        Log.i(" test 3 ", "width : " + redShape.getWidth() + "height: " + redShape.getHeight());



    }
Kaal
  • 111
  • 1
  • 10
  • "`I've scaled my image to match the imageview`" wrong, ImageView draws the Bitmap/Drawable with different sizes depending on scaleType, no need for "image scaling" – pskink Aug 12 '15 at 14:46
  • yea you're right, removed that and I get the exact same result. It still detects a drag above the image. I don't understand why it's fine anywhere else around the image except for the top. – Kaal Aug 12 '15 at 14:48
  • ImageView internally uses a Matrix, for ex if your bitmap is 100x100 and your VimageView is 400x400 your Matrix is set in such way that point [0,0] is mapped to [0,0] and [100,100] is mapped to [400,400], you can use mapPoints to do the point(s) mapping – pskink Aug 12 '15 at 14:53
  • I'm having a bit of a hard time trying to use map points and map the coordinates of the image to the imageview, would it be ok if you showed me an example of how to use it? – Kaal Aug 12 '15 at 16:03
  • try Matrix#mapRect where rect is (0, 0, bitmap_width, bitmap_height) – pskink Aug 12 '15 at 16:12
  • Check my edit please, I've done the above and am not sure about the next step. – Kaal Aug 12 '15 at 16:31
  • did you try to inspect `drawableRect` ? log it? – pskink Aug 12 '15 at 16:33
  • yea I get: rectF(0.0,0.0,251.0,293.0) – Kaal Aug 12 '15 at 16:41
  • so your Bitmap is mapped to that RectF – pskink Aug 12 '15 at 16:44
  • But now it's even worse, it doesn't detect the touch inside the image at all, it only detects a touch on the left-top of the screen, above the image. – Kaal Aug 12 '15 at 17:00
  • whats the size of your Bitmap, size of your ImageView and ImageView's scaleType? – pskink Aug 12 '15 at 17:01
  • image size: width: 251 Height: 293 ImageView size: width: 0 Height: 0, How do you get imageview's scaleType? Also, is it odd that the width and height of imageview is 0? – Kaal Aug 12 '15 at 17:09
  • ImageView size: width: 0 Height: 0, ? so ImageView size is ZERO pixels x ZERO pixels ? use ViewTreeObserver to get the size after layout phase and do the mapping then or do the mapping in touch listener – pskink Aug 12 '15 at 17:12
  • See my edit 2, I did that outside of onCreate and I got the actual width and height of the imageview which is 251, 293 just like the image size? – Kaal Aug 12 '15 at 17:32
  • override onSizeChanged if you have custom ImageView, and call getImageMatrix there and mapRect – pskink Aug 12 '15 at 17:41
  • and what, did it work? – pskink Aug 13 '15 at 04:57
  • I'm trying to use onSizeChanged but I think I might be doing it wrong, I haven't used that before so it's a bit confusing. Could you show me an example of how I can implement it. Also, how can I call getImageMatrix from within the custom imageview class because the class extends imageview so wouldn't that be done outside this class and inside the main activity? – Kaal Aug 13 '15 at 09:46
  • Is there a way without having custom imageview class? – Kaal Aug 13 '15 at 09:56
  • sure: just get the matrix in your touch listener, you will be sure that matrix is already set and valid – pskink Aug 13 '15 at 10:09

0 Answers0