2

我有一个包含在 RelativeLayout 中的图像视图。单击 imageview 时,我使用平移动画为整个 RelativeLayout 设置动画以将其向下移动。

当我再次单击图像视图(在它的新位置)时,它应该将其移回,但事实并非如此。但是,如果我单击 imageview 开始的位置,它确实会移动整个“面板”。为什么图像视图不与相对布局一起移动......至少就它的可点击性而言。实际图像在移动,但可点击的位置没有。

这是我的布局xml:

<RelativeLayout
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_width="fill_parent"
    android:layout_height="120px"       
    android:layout_marginTop="-106px"
    android:id="@+id/chatbox"
    android:visibility="invisible">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="106px"
        android:background="#000000"
        android:id="@+id/chattext" />
    <ImageView
        android:layout_width="20px"
        android:layout_height="14px"
        android:id="@+id/chatbubble"
        android:layout_below="@id/chattext"
        android:src="@drawable/chatbubble" />
</RelativeLayout>

编辑:我应该补充一点,我正在使用 Animation.setFillAfter(true) 在动画完成后将面板固定到位。

4

2 回答 2

2

这是因为动画实际上并不影响视图位置,它们只是绘制它们。因此,要在新位置处理点击,您必须在那里放置一些东西(即不可见的 FrameLayout)。或者您可以在动画完成时更改视图边距,以便视图实际上会移动到该位置。

于 2010-08-31T02:45:11.843 回答
0

我遇到了同样的问题,我找到的最佳解决方案是在您的动画中添加一个 AnimationListener 并在该侦听器中自己移动视图,正如 Konstantin Burov 所说:

animation.setFillAfter(false);

AnimationListener animListener = new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        RelativeLayout.LayoutParams relativeLayoutParams = (LayoutParams) yourView.getLayoutParams();

        //To avoid the flicker
        yourView.clearAnimation();

        relativeLayoutParams.setMargins(0, 0, 0, Utils.dpToPx(activity, newPositionDp);
        yourView.setLayoutParams(relativeLayoutParams);
    }
};

animation.setAnimationListener(animListener);

Utils.dpToPx 也可能有用:

public final static int dpToPx(Context context, int dp) {
    return ((int)((dp * context.getResources().getDisplayMetrics().density) + 0.5));
}
于 2013-02-18T16:31:14.887 回答