Android 上的 FrameLayout onClick 用于重叠图像

发布于 2024-10-18 09:16:24 字数 1987 浏览 6 评论 0原文

起初我以为这个问题很容易解决,但事实证明这是一个挑战。

场景 一个 FrameLayout 和两个 ImageView,一个在另一个之上。第一个图像有一个翻译动画和一个 onClick 事件。让我们将其转化为实际的东西:框架布局有一个兔子图像和一个布什图像。兔子有一个平移动画,因此它会移出灌木丛。一旦兔子变得可见,用户就可以点击它。不幸的是,这并没有按预期工作。即使兔子不可见(在灌木丛后面),如果用户点击灌木丛,兔子的单击事件也会触发。我尝试为灌木图像添加 onClick 事件(不执行任何操作),但现在只有这个事件会触发,而兔子事件则不会。

代码

动画

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%"
    android:toXDelta="100%"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="25000"
    android:zAdjustment="top" />

布局

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layBackground"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="@drawable/someimage">

                       <ImageView android:id="@+id/imgAfterBush"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                                 android:layout_gravity="bottom|left"
                                 android:layout_marginLeft="50dip"
                                 android:onClick="imgAfterBushOnClick"/>

                      <ImageView android:id="@+id/imgBush"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                                 android:src="@drawable/bush"
                                 android:layout_gravity="bottom|left"
                                />
</FrameLayout>

我希望 Rabbit 图像的 onClick 事件仅在可见时触发。有什么解决办法吗?谢谢。

First I thought that the problem will be very easy to solve, but it proved to be a challenge.

Scenario
One FrameLayout and two ImageViews, one over the other. The first images has a Translate animation and a onClick event. Let's translate this in something practical: the Framelayout has one Rabbit image and a Bush image. The Rabbit has a translate animation so it moves out of the bush. As soon as the rabbit becomes visible, the user can tap on it. Unfortunately this does not work as intended. Even if the rabbit is not visible (being behind the bush) if the user taps on the bush, the click event of the rabbit fires. I tried to add onClick event (that doesn't do anything) for the bush image, but now only this one fires, and the rabbit ones doesn't.

Code

Animation

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%"
    android:toXDelta="100%"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="25000"
    android:zAdjustment="top" />

Layout

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layBackground"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="@drawable/someimage">

                       <ImageView android:id="@+id/imgAfterBush"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                                 android:layout_gravity="bottom|left"
                                 android:layout_marginLeft="50dip"
                                 android:onClick="imgAfterBushOnClick"/>

                      <ImageView android:id="@+id/imgBush"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                                 android:src="@drawable/bush"
                                 android:layout_gravity="bottom|left"
                                />
</FrameLayout>

I want the onClick event of the Rabbit image to fire only when it is visible. Any solutions ? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-10-25 09:16:24

Android 上的动画 < 3.0 仅影响视图的渲染:您设置动画的视图仍处于其原始位置。当动画结束时,您需要自己移动视图(例如通过更改其布局参数)。

Animations on Android < 3.0 only affect the rendering of a View: the view you animate is still at its original position. You need to move the View (by changing its layout parameters for instance) yourself when the animation is over.

尐籹人 2024-10-25 09:16:24

这并不太难,但是您首先需要学习:

  • 创建自定义视图

  • 在画布上绘图

  • 在画布上为图像添加动画

  • 检测触摸事件

这是一个简单的开始,我为类似的问题所做的:如何在画布内使用动画框架?

It is not too difficult, however you need first to learn:

  • creating custom views

  • drawing on canvas

  • animating images on canvas

  • detecting on touch events

Here is a simple one for the start, which I did for a similar question: How can I use the animation framework inside the canvas?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文