【问题标题】:Android: onTouch events happen after scaling animation when touched outsideAndroid:在外部触摸时缩放动画后发生onTouch事件
【发布时间】:2013-01-02 03:01:38
【问题描述】:

我的布局如下所示:

<FrameLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:scaleType="center"/>
</FrameLayout>

“图像”开始时是全屏显示。为“图像”设置了一个 onTouchListener,效果很好。然后我用 ScaleAnimation 缩放“图像”(大约一半大小),我期望发生的是 onTouchListener 只会在“图像”被触摸时拾取,但它仍然会在整个屏幕上触发并且 getX 得到相同值作为 getRawX。

有什么方法可以检测我是否在 onTouch 的图像边界内?我考虑过根据比例值对边界进行硬编码,但肯定有更优雅的解决方案吗?

【问题讨论】:

    标签: android animation touch-event


    【解决方案1】:

    首先,您可以在屏幕上找到图像的宽度和高度,如下所示:

    widthOnScreen = origWidth * scaleFactor
    heightOnScreen = origHeight * scaleFactor
    

    正如你所说的“图像是全屏开始”,origWidth = viewWidthorigHeight = viewHeight 和 scaleFactor 是应用到图像上的总比例,直到最大/最小。

    matrix.getvalues(val);
    

    保持左、上、右、下:

    left=val[2]; 
    right=val[Matrix.MSCALE_X]*actualWidth+val[2]; //actualWidth is image's actual width
    top=val[5];
    bottom=vals[Matrix.MSCALE_Y]*actualHeight+val[5];
    

    然后onTouch(),检查您的event.getX()getY() 是否在这些左、上、右和下边界内。

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多