【问题标题】:LinearLayout onclick does not translate after TranslateAnimation在 TranslateAnimation 之后,LinearLayout onclick 不翻译
【发布时间】:2012-07-05 07:40:02
【问题描述】:

这是我的问题。我有一个具有 clickable=true 的 LinearLayout 一个 onTouch 事件,以便当触摸 LinearLayout 时,它会滑动 上屏幕。这有效,但之后当 onTouch 事件是 从新位置发射没有任何反应。

步骤:

  1. 我触摸了LinearLayout,它按原样向上移动。
  2. 我再次触摸它,没有任何反应
  3. 我触摸了 LinearLayout 最初所在的屏幕部分,LinearLayout 将按应有的方式切换。

看起来好像视图已移动到新位置,但实际上并没有。

以下是我的 xml 和代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/julyContainer"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">


        <RelativeLayout android:id="@+id/rel01"/> 
        <ImageView />
        </RelativeLayout>

        <ImageView             
            android:id="@+id/shadow"
            android:paddingTop="6dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/header_barshadow"/>

<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_calendar" 
    android:id="@+id/calScroller">


<RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">        

        <LinearLayout android:id="@+id/circleLayout"
                android:orientation="horizontal"
                android:clickable="true"
                android:onClick="@string/circleAction"              
                android:paddingTop="10dip"
                android:paddingLeft="10dip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <Button 
                android:id="@+id/circleCal"
                android:background="@drawable/cal_circle_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="@string/circleAction"/>

                <LinearLayout 
                android:id="@+id/circleLayout01"
                android:orientation="vertical"        
            android:paddingLeft="10dip"
            android:paddingRight="3dip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView/>  
                <TextView/>  
                <TextView/>  

               <LinearLayout android:id="@+id/julylayout2"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:orientation="vertical">

              <TextView/>

               </LinearLayout>

            </LinearLayout>
        </LinearLayout>

       <ImageView android:id="@+id/etch1"
                  android:src="@drawable/etch_calendar"
                  android:paddingTop="15dip"
                  android:paddingBottom="15dip"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/circleLayout"/>                     

      <LinearLayout android:id="@+id/squareLayout"
                android:clickable="true"
                android:onClick="@string/squareAction"              
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="10dip"
                android:layout_below="@id/etch1">
        <Button 
                android:id="@+id/squareCal"
                android:background="@drawable/cal_square_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="@string/squareAction"/>

                <LinearLayout
            android:orientation="vertical"
            android:paddingLeft="10dip"
            android:paddingRight="3dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <TextView/>  
                <TextView/>  
                <TextView/> 

               <LinearLayout android:id="@+id/layout3"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:orientation="vertical">

                    <TextView/>  

               </LinearLayout>
        </LinearLayout>        
    </LinearLayout>        

</RelativeLayout>
</ScrollView>      
</LinearLayout>  

代码:

private void slideUp(View view) {
    Animation slide = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -0.25f);
    slide.setDuration(1000);
    slide.setFillAfter(true);
    slide.setFillEnabled(true);

    view.startAnimation(slide);
}

private void slideDown(View view) {
    Animation slide = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, -0.25f, Animation.RELATIVE_TO_SELF, 0.0f);
    slide.setDuration(1000);
    slide.setFillAfter(true);
    slide.setFillEnabled(true);
    view.startAnimation(slide);
}

新变化:输出新职位

07-05 13:20:22.084: I/System.out(15187): onAnimationStart 0 , 120

07-05 13:20:23.053: I/System.out(15187): onAnimationEnd 0 , 120

private void slideUp(final View view) {

        Animation slide = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -0.75f);
        slide.setDuration(1000);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);
        slide.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                int[] startPosition = new int[2];
                view.getLocationOnScreen(startPosition);
                System.out.println("onAnimationStart " + startPosition[0] + " , " + startPosition[1]);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                final int left = view.getLeft();
                final int top = view.getTop();
                final int right = view.getRight();
                final int bottom = view.getBottom();

                int offset = (int) 0.75;
                view.layout(left, top + offset * top, right, bottom + offset * bottom);

                int[] endPosition = new int[2];
                view.getLocationOnScreen(endPosition);
                System.out.println("onAnimationEnd " + endPosition[0] + " , " + endPosition[1]);

            }

        });


    }

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    这是 android 动画的正常行为。这是因为动画并没有真正移动布局,所以它在显示器上的位置保持不变。如果要将布局重新定位到动画结束的位置,则需要调用yourLayout.layout() 方法并在那里传递4 个参数,这些参数描述了布局的新位置。请记住,layout() 获取与其父级相关的参数。

    查看下面的示例代码

    private AnimationListener slideDownAnimationListener = new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                final int left = view.getLeft();
                final int top = view.getTop();
                final int right = view.getRight();
                final int bottom = view.getBottom();
                view.layout(left, top - 0.25 * top, right, bottom - 0.25 * bottom);
            }
    };
    
    private Animation slideDownAnimation = new TranslateAnimation(
                                         Animation.RELATIVE_TO_SELF, 0.0f,
                                         Animation.RELATIVE_TO_SELF, 0.0f,
                                         Animation.RELATIVE_TO_SELF, -0.25f,
                                         Animation.RELATIVE_TO_SELF, 0.0f
                                         );
    
    private void slideDown(final View view) {
        slide.setDuration(1000);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        slide.setAnimationListener(slideDownAnimationListener);
        view.startAnimation(slideDownAnimation);
    }
    

    【讨论】:

    • 谢谢@vasart,这是有道理的。你能提供一个小例子来说明如何从顶部计算新位置吗?
    • 在我的场景中,我正在为顶级 LinearLayout 设置动画,这会导致我在尝试获取布局参数时出现问题吗?
    • 我做了以下更改,但没有效果。请查看我更新的代码。
    • 抱歉,startAnimationsetAnimationListener 的位置要换了
    • 顺便说一句,感谢您对此的耐心等待。我做了那个改变,但没有效果。但是,我确实对您的布局计算做了一个小改动。由于我们不能在 view.layout 参数中使用双精度值,所以我对一个 int 示例进行了强制转换: int tempVal = (int)0.75;然而,这得出了一个零值。是否有另一种方法来进行计算,以便它可以处理双精度值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多