2010-08-30 53 views
2

我有一个包含在RelativeLayout中的imageview。当点击图像视图时,我用翻译动画为整个RelativeLayout设置动画,将其向下移动。clickable imageview location change with animation - android

当我再次单击图像视图(在它的新位置)时,应该将它移回,但它不会。但是,如果我点击imageview开始的位置,它会将整个“面板”移回。为什么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(真)在动画完成后举行的地方面板。

回答

2

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

+0

这就是我害怕。谢谢(你的)信息。 – Kyle 2010-08-31 14:40:30

0

我有同样的问题,我找到了最好的解决办法是将AnimationListener添加到您的动画和移动自己的观点在听者康斯坦丁波罗夫说:

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)); 
}