FrameLayout onClick for Android上的重叠图像

时间:2011-02-22 17:19:52

标签: android onclick android-framelayout

首先,我认为这个问题很容易解决,但事实证明这是一个挑战。

方案 一个FrameLayout和两个ImageView,一个在另一个上。第一个图像具有翻译动画和onClick事件。让我们将其转化为实用的东西:Framelayout有一个Rabbit图像和一个Bush图像。兔子有一个翻译动画,所以它移出丛林。一旦兔子变得可见,用户就可以点击它。不幸的是,这不符合预期。即使兔子看不到(在灌木丛后面),如果用户敲击灌木丛,兔子的咔哒声就会发生。我试图为丛林图像添加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事件只有在可见时才会触发。有解决方案吗谢谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

这不是太难,但是你需要先学习:

  • 创建自定义视图

  • 在画布上绘图

  • 在画布上制作动画图像

  • 检测触摸事件

这是一个简单的开始,我为类似的问题做了:How can I use the animation framework inside the canvas?