簡體   English   中英

如何為Android上的圖像按鈕設置setVisibility動畫?

[英]How to animate setVisibility for an imagebutton on Android?

我有一個包含listview和imagebutton的布局:

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

代碼工作正常,但我想設置hide / show進程的動畫。 我該如何達到這個效果?

您可以使用系統動畫淡入和淡出。

 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 Res這是一個非常好的動畫教程

要顯示按鈕,請調用此方法

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

是編寫自己的動畫偵聽器並將其設置為動畫,如下所示:

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

而已。

也檢查這個例子(使用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);
    }
});

您也可以輕松切換到淡入效果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM