• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

布局动画在第一次运行时不起作用

用户头像
it1352
帮助1

问题说明

我有一个包含项目列表的活动,当您单击一个项目时,我希望该项目的播放控件从屏幕底部向上滑动并变为可见.我已经为滑入和滑出定义了一个动画集,它们可以工作.我已经在我的活动中设置了我的 animationListener 并在一个项目的 onClick 动画中开始了我的幻灯片.我的问题是,第一次运行应用程序时,当我点击一个项目时,onClick 回调被执行,但动画没有发生.第二次单击时,会出现动画中的幻灯片,但不会出现幻灯片.第三次及以后,它按预期工作.这是我的动画集.

I have an Activity with a list of items and when you click on an item, I want playback controls for that item to slide up from the bottom of the screen and become visible. I've defined an animation set for the slide in and the slide out and they work. I've setup my animationListener in my activity and started my slide in animation onClick of an item. My problem is, the first time I run the app, when I click on an item, the onClick callback is executed, but the animation doesn't happen. Second time I click, the slide in animation happens, but not the slide out. Third and subsequent times, it works as expected. Here are my animation sets.

vm_slide_in.xml

vm_slide_in.xml

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
           android:fillAfter="true">
    <translate
        android:fromYDelta="800"
        android:toYDelta="0"
        android:duration="600" />
</set>

vm_slide_out.xml

vm_slide_out.xml

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
           android:fillAfter="true">
    <translate
        android:fromYDelta="0"
        android:toYDelta="800"
        android:duration="600" />
</set>

这是我的活动布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/AppBG">
    <RelativeLayout 
          
        android:       
        android:layout_alignParentTop="true">
        <include layout="@layout/logobar"></include>
    </RelativeLayout>
    <RelativeLayout 
          
        android:      
        android:layout_below="@ id/logo_bar">
        <include layout="@layout/titlebar"></include>"
        <Button  
            android: 
            android:text="@string/vm_speaker_btn_label" 
            android:layout_alignParentLeft="true"
            android:layout_margin="4dp">
        </Button>
        <Button  
            android: 
            android:text="@string/vm_edit_btn_label" 
            android:layout_alignParentRight="true"
            android:layout_margin="4dp">
        </Button>
    </RelativeLayout>
    <ListView
        android: 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@ id/title_bar"/>
    <RelativeLayout
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@ id/footer"
        android:visibility="gone">
        <include layout="@layout/vm_control"/>
    </RelativeLayout>
    <RelativeLayout
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <include layout="@layout/taskbar"/>
    </RelativeLayout>
</RelativeLayout>

这是包含控件的布局

    <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout 
        android: 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/dark_grey">
        <TextView
            android: 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:text="@string/branding"
            android:textColor="@color/white">
        </TextView>
        <TextView  
            android: 
            android:layout_toLeftOf="@ id/vm_progress"
            android:layout_below="@ id/vm_ctl_branding"
            android:layout_marginRight="3dp"
            android:text="0:00">
        </TextView>
        <SeekBar
            android: 
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_below="@ id/vm_ctl_branding"
            android:layout_centerHorizontal="true">
        </SeekBar>
        <TextView  
            android: 
            android:layout_toRightOf="@ id/vm_progress"
            android:layout_below="@ id/vm_ctl_branding"
            android:layout_marginLeft="3dp"
            android:text="-0:00">
        </TextView>
        <LinearLayout
            android: 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@ id/vm_timestamp"
            android:layout_alignRight="@ id/vm_timeleft"
            android:orientation="horizontal"
            android:layout_below="@ id/vm_progress">
            <TextView  
                android:background="@drawable/vm_action_btn_call"
                android: 
                android:layout_marginRight="5dp"
                android:text="@string/vm_action_btn_callback">
            </TextView>
            <TextView  
                android:background="@drawable/vm_action_btn_delete"
                android: 
                android:layout_marginLeft="5dp"
                android:text="@string/vm_action_btn_delete">
            </TextView>
        </LinearLayout>
    </RelativeLayout>
</merge>

从我的 onCreate 方法...

From my onCreate method...

// Handle list item selections
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final Voicemail vmItem = (Voicemail) vmla.getItem(position);
        Toast.makeText(ctx, "Name: "   vmItem.getCallerName()   " Position: "   position, Toast.LENGTH_SHORT)
                .show();
        vVm_controls.startAnimation(mSlideIn);
    }
});

还有我的动画回调...

And my animation callbacks...

@Override
public void onAnimationStart(Animation animation) {
    vm_controls_in = !vm_controls_in;
    if (vm_controls_in) {
        vVm_controls.setVisibility(View.GONE);
    } else {
        vVm_controls.setVisibility(View.VISIBLE);
        // vVm_controls.bringToFront();
    }
}

@Override
public void onAnimationEnd(Animation animation) {
    if (!vm_controls_in) {
        try {
            Thread.sleep(1000);
            vVm_controls.startAnimation(mSlideOut);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

}

正确答案

#1

正如您所发现的,问题是可见性.当视图最初在 xml 文件中设置为消失时,在可见性更改为可见或不可见之前,Android 不会呈现布局.如果您尝试在尚未渲染的视图上设置动画,动画将在没有布局的视图上发生.动画完成后,它会渲染它,然后视图会突然出现而没有动画.它在随后的时间有效,因为即使设置为消失,视图也有布局.

As you discovered, the issue is visibility. When a view is initially set as gone in the xml file, Android will not render the layout until the visibility is changed to visible or invisible. If you attempt to animate on a view that has not been rendered yet, the animation will occur on the view without a layout. After the animation has completed, it will render it and suddenly the view will appear without animation. It works subsequent times because the view has a layout even when set to gone.

关键是将可见性设置为不可见,而不是在 xml 文件中消失,以便呈现但隐藏.当动画第一次出现时,视图会按预期移动.

The key is to set the visibility to invisible instead of gone in the xml file so that it is rendered yet hidden. When the animation occurs the first time the view appear and will move as expected.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangkjiee
系列文章
更多 icon
同类精品
更多 icon
继续加载