单击ListView中的复选框后,如何使按钮栏从底部滑入?

时间:2010-12-24 12:22:04

标签: android animation slide

我有一个自定义listadapter的列表视图,它使用复选框和一些文本视图填充列表视图。当用户选择一个复选框时,我需要一个按钮栏从屏幕底部滑入视图并坐在那里。我已经制作了按钮栏,可以通过将其可见性更改为“已消失”和“可见”来使其在屏幕上显示和消失,但它不会通过滑入和滑出效果执行这些操作。我如何让它做那些动画?

2 个答案:

答案 0 :(得分:25)

您想使用Animation xml resources

这是一个动画xml的示例,它将对象从屏幕底部“滑动”到布局中的位置:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="100%p" android:toYDelta="0%p" android:duration="300"/>
</set>

您将把它放在res / anim文件夹中,然后使用此java代码为您的视图设置动画:

slideUpIn = AnimationUtils.loadAnimation(this, R.anim.slide_up_in);
yourButtonBarView.startAnimation(slideUpIn);

您需要将startAnimation调用放在获取CheckBox已被检查的回调的位置。

答案 1 :(得分:2)

下面是运行时代码实现,如果您出于其他目的需要进行相应修改。

RelativeLayout rl = new RelativeLayout(this);
ImageButton btnBar = new ImageButton(this);

RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 80);

btnParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

btnBar.setLayoutParams(btnParams);
btnBar.setBackgroundColor(Color.RED); // test with red background

TranslateAnimation a = new TranslateAnimation(
                           Animation.RELATIVE_TO_PARENT, 0,
                           Animation.RELATIVE_TO_PARENT, 0,
                           Animation.RELATIVE_TO_PARENT, (float)100, 
                           Animation.RELATIVE_TO_PARENT, (float)0);

a.setDuration(1000);
btnBar.startAnimation(a); // add animation while start

rl.addView(btnBar);
setContentView(rl);
相关问题