TranslateAnimation后的LinearLayout的onclick不会翻译TranslateAnimation、LinearLayout、onclick

2023-09-04 05:07:28 作者:万人中央我孤独的漂亮

下面是我的问题。我有一个LinearLayout中,有可点击=如此 一个onTouch事件,从而当的LinearLayout被触摸时,它滑动 在屏幕。这工作,但后来当onTouch事件 从新的位置没有发生发射。

步骤:

在我接触到的LinearLayout,并将其移动起来像它应该。 在我再碰它,什么也没有发生 我触摸屏幕,其中的LinearLayout原本的一部分的LinearLayout将切换像它应该。

看起来好像观点已经移动到一个新的位置,但在现实中却并非如此。

下面是我的XML和放大器; code。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:ID =@ + ID / julyContainer
              机器人:方向=垂直
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =FILL_PARENT>


        < RelativeLayout的机器人:ID =@ + ID / rel01/>
        < ImageView的/>
        < / RelativeLayout的>

        < ImageView的
            机器人:ID =@ + ID /阴影
            机器人:paddingTop =6dip
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ header_barshadow/>

<滚动型
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:背景=@可绘制/ bg_c​​alendar
    机器人:ID =@ + ID / calScroller>


< RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>

        <的LinearLayout机器人:ID =@ + ID / circleLayout
                机器人:方向=横向
                机器人:可点击=真
                机器人:的onClick =@字符串/ circleAction
                机器人:paddingTop =10dip
                机器人:以下属性来=10dip
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT>

                <按钮
                机器人:ID =@ + ID / circleCal
                机器人:背景=@可绘制/ cal_circle_button
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:的onClick =@字符串/ circleAction/>

                <的LinearLayout
                机器人:ID =@ + ID / circleLayout01
                机器人:方向=垂直
            机器人:以下属性来=10dip
            机器人:paddingRight =3dip
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT>

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

               <的LinearLayout机器人:ID =@ + ID / julylayout2
                    机器人:layout_width =match_parent
                    机器人:layout_height =0dp
                    机器人:方向=垂直>

              <的TextView />

               < / LinearLayout中>

            < / LinearLayout中>
        < / LinearLayout中>

       < ImageView的机器人:ID =@ + ID / etch1
                  机器人:SRC =@可绘制/ etch_calendar
                  机器人:paddingTop =15dip
                  机器人:paddingBottom会=15dip
                  机器人:layout_width =WRAP_CONTENT
                  机器人:layout_height =WRAP_CONTENT
                  机器人:layout_below =@ ID / circleLayout/>

      <的LinearLayout机器人:ID =@ + ID / squareLayout
                机器人:可点击=真
                机器人:的onClick =@字符串/ squareAction
                机器人:方向=横向
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:以下属性来=10dip
                机器人:layout_below =@ ID / etch1>
        <按钮
                机器人:ID =@ + ID / squareCal
                机器人:背景=@可绘制/ cal_square_button
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:的onClick =@字符串/ squareAction/>

                <的LinearLayout
            机器人:方向=垂直
            机器人:以下属性来=10dip
            机器人:paddingRight =3dip
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>

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

               <的LinearLayout机器人:ID =@ + ID / layout3
                    机器人:layout_width =match_parent
                    机器人:layout_height =0dp
                    机器人:方向=垂直>

                    <的TextView />

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

< / RelativeLayout的>
< /滚动型>
< / LinearLayout中>
 

code:

 私人无效效果基本show(查看视图){
    动画幻灯片=新TranslateAnimation(
            Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
            0.0,Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,-0.25f);
    slide.setDuration(1000);
    slide.setFillAfter(真正的);
    slide.setFillEnabled(真正的);

    view.startAnimation(幻灯片);
}

私人无效了slideDown(查看视图){
    动画幻灯片=新TranslateAnimation(
            Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
            0.0,Animation.RELATIVE_TO_SELF,-0.25f,Animation.RELATIVE_TO_SELF,0.0);
    slide.setDuration(1000);
    slide.setFillAfter(真正的);
    slide.setFillEnabled(真正的);
    view.startAnimation(幻灯片);
}
 
面试问题 简单的补间动画 看准网

新变化:新职位输出

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

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

 私人无效效果基本show(最终查看视图){

        动画幻灯片=新TranslateAnimation(
                Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
                0.0,Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,-0.75f​​);
        slide.setDuration(1000);
        slide.setFillAfter(真正的);
        slide.setFillEnabled(真正的);
        view.startAnimation(幻灯片);
        slide.setAnimationListener(新AnimationListener(){

            @覆盖
            公共无效onAnimationStart(动画动画){
                INT []指定startPosition =新INT [2];
                view.getLocationOnScreen(指定startPosition);
                的System.out.println(onAnimationStart+指定startPosition [0] +,+指定startPosition [1]);
            }

            @覆盖
            公共无效onAnimationRepeat(动画动画){
            }

            @覆盖
            公共无效onAnimationEnd(动画动画){
                最终诠释左= view.getLeft();
                最终诠释顶级= view.getTop();
                最终诠释权= view.getRight();
                最终诠释底部= view.getBottom();

                INT偏移=(int)的0.75;
                view.layout(左,上+偏移*上,右,下+偏移*底部);

                INT [] endPosition =新INT [2];
                view.getLocationOnScreen(endPosition);
                的System.out.println(onAnimationEnd+ endPosition [0] +,+ endPosition [1]);

            }

        });


    }
 

解决方案

这是Android的动画正常行为。它发生,因为动画并没有真正移动布局,它的显示屏上的位置保持不变。如果你想你的布局重新定位到您的动画结束的地方,你需要调用 yourLayout.layout()的方法,并通过有4个参数,它描述了布局的新位置。请记住,布局()得到PARAMS相对于它的父。

请参阅下面

样品code

 私人AnimationListener slideDownAnimationListener =新AnimationListener(){
        @覆盖
        公共无效onAnimationStart(动画动画){
        }

        @覆盖
        公共无效onAnimationRepeat(动画动画){
        }

        @覆盖
        公共无效onAnimationEnd(动画动画){
            最终诠释左= view.getLeft();
            最终诠释顶级= view.getTop();
            最终诠释权= view.getRight();
            最终诠释底部= view.getBottom();
            view.layout(左,上 -  0.25 *上,右,下 -  0.25 *底部);
        }
};

民营动画slideDownAnimation =新TranslateAnimation(
                                     Animation.RELATIVE_TO_SELF,0.0,
                                     Animation.RELATIVE_TO_SELF,0.0,
                                     Animation.RELATIVE_TO_SELF,-0.25f,
                                     Animation.RELATIVE_TO_SELF,0.0
                                     );

私人无效了slideDown(最终查看视图){
    slide.setDuration(1000);
    slide.setFillAfter(真正的);
    slide.setFillEnabled(真正的);
    slide.setAnimationListener(slideDownAnimationListener);
    view.startAnimation(slideDownAnimation);
}
 

Here is my problem. I have a LinearLayout that has clickable=true for an onTouch Event so that when the LinearLayout is touched, it slides up the screen. This works, but afterward when the onTouch Event is fired from the new location nothing happens.

Steps:

I touch the LinearLayout and it moves up like it should. I touch it again and nothing happens I touch the part of the screen where the LinearLayout was originally the LinearLayout will toggle like it should.

It appears as if the view has moved to a new location but in reality it has not.

Below is my xml & code.

<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>  

CODE:

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

NEW CHANGES: output of new positions

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

            }

        });


    }

解决方案

This is normal behaviour for android animation. It is happening because animation does not really moves the layout so it's position on the display remains the same. If you want to relocate your layout to the place where your animation ends, you need to call yourLayout.layout() method and pass there 4 parameters, which describe layout's new position. Keep in mind that layout() gets params relative to it's parent.

See sample code below

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