java 如何为 Android 上的图像按钮设置动画 setVisibility?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7173890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to animate setVisibility for an imagebutton on Android?
提问by Kalimah
I have a layout that has a listview and 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>
Based on user selection I am using this to hide or show the image button:
根据用户选择,我使用它来隐藏或显示图像按钮:
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton);
myButton.setVisibility(View.GONE);
The code is working fine, however I would like to animate the hide/show process. How do I achieve this effect?
代码工作正常,但是我想为隐藏/显示过程设置动画。我如何实现这种效果?
回答by Fabian
You can use the System animation fade in and fade out.
您可以使用系统动画淡入淡出。
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);
that should work
那应该工作
if you want to make your own animations: Look at Android Resand here is a very good tutorialfor animations.
如果您想制作自己的动画: 查看 Android Res,这里有一个非常好的动画教程。
回答by Kurru
To show the button call this
要显示按钮调用这个
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);
Then to hide the button:
然后隐藏按钮:
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);
回答by Fabian
Yes write your own animation listener and set it to the animation like this :
是的,编写您自己的动画侦听器并将其设置为这样的动画:
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
}
}
Then set it to the animation with :
然后将其设置为动画:
animation.setAnimationListener(new MyAnimationListener(myButton);
Thats it.
而已。
回答by MatheusJardimB
Check this example too (using ObjectAnimator):
也检查这个例子(使用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);
}
});
You can easily switch to fade in effecttoo
您可以轻松地切换到有效淡化太