如何在Android上为ImageButton设置setVisibility动画?

6

我有一个布局,其中包含一个列表视图和一个图像按钮:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="4px"
    android:background="@color/bgColor">

    <ImageButton
    android:id="@+id/imageButton"
    android:src="@drawable/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="0dp"
    android:layout_alignParentBottom="true"/>

    <ListView
    android:layout_above="@+id/imageButton" 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:background = "@drawable/list_bg"
    android:divider="@drawable/grade"
    android:dividerHeight ="1px"
    android:cacheColorHint="#00000000"
    android:fastScrollEnabled= "true"
    android:scrollingCache ="true"
    android:smoothScrollbar="false"
    >
   </ListView>
</RelativeLayout>

根据用户的选择,我使用这个来隐藏或显示图像按钮:

       ImageButton myButton = (ImageButton) findViewById(R.id.imageButton);
        myButton.setVisibility(View.GONE);

代码运行正常,但我想添加动画来实现隐藏/显示的效果。如何实现这个效果?
4个回答

31
你可以使用系统动画淡入淡出效果。
 Animation animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out);
 Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
myButton.setAnimation(animFadeOut)
//if necessary then call:
myButton.setVisibility(View.GONE);

那应该可以工作

如果你想制作自己的动画: 查看Android资源,并且这里有一个非常好的教程关于动画。


当我使用您的代码 myButton.setVisibility(View.GONE); 时,动画不起作用。否则,如果我不使用这行代码,按钮会淡出并再次出现,而我希望按钮消失。 - Kalimah
2
使用AnimationListener,在listeners的onAnimationEnd()方法上设置setVisiblity()-调用。 - pgsandstrom

16

调用此函数以显示按钮:

AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1.0f);
fade_in.setDuration(500);
fade_in.setAnimationListener(new AnimationListener()
{
    public void onAnimationStart(Animation arg0)
    {
    }
    public void onAnimationRepeat(Animation arg0)
    {
    }

    public void onAnimationEnd(Animation arg0)
    {
        myButton.setVisibility(View.VISIBLE);
    }
});
myButton.startAnimation(fade_in);

然后隐藏按钮:

AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
fade_out.setDuration(upcoming_animation_time);
fade_out.setAnimationListener(new AnimationListener()
{
    public void onAnimationStart(Animation arg0)
    {
    }
    public void onAnimationRepeat(Animation arg0)
    {
    }

    public void onAnimationEnd(Animation arg0)
    {
        myButton.setVisibility(View.GONE);
    }
});
myButton.startAnimation(fade_out);

7

是的,编写自己的动画监听器并将其设置为动画,如下所示:

public class MyAnimationListener implements AnimationListener {
    private ImageButton mImgButton;

    public MyAnimationListener(ImageButton imgButton) {
        mImgButton = imgButton;
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        mImgButton.setVisibility(View.GONE);

    }

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

    }

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

    }

}

然后使用以下命令将其设置为动画:
animation.setAnimationListener(new MyAnimationListener(myButton);

这就是了。


谢谢。所有的回答都很好,只要提供清晰实现代码,我就会接受你的答案。 - Kalimah

1

也可以参考这个例子(使用 ObjectAnimator):

ObjectAnimator animator = ObjectAnimator.ofFloat(myButton, View.ALPHA, 1f, 0f);
animator.setDuration(300); //ms
animator.start();
animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        myButton.setVisibility(GONE);
    }
});

您可以轻松地切换到淡入效果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接