如何在安卓中为一个视图添加动画效果?

19

我想知道是否有一种简单的方法可以添加一个视图(一个按钮)到RelativeLayout中,并带有某种比例动画。 我从Button扩展了一个类,做了这样的事情:

    public class MyButton extends Button {

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
        anim.setDuration(1000);
        anim.setFillAfter(true);
        this.startAnimation(anim);
    }

然后尝试将此按钮添加到视图中,但无法正常工作。请帮忙!

4个回答

19

在你的活动中,改用以下方法:

parentview.addView(myButton);
然后使用以下代码使按钮实现动画效果:
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_right_in);
animation.setStartOffset(0);
myButton.startAnimation(animation);

这是slide_right_in.xml的示例

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

另外,这是我编写的一个动画播放函数:

public Animation PlayAnim( int viewid, Context Con, int animationid, int StartOffset )
{
    View v = findViewById(viewid);

    if( v != null )
    {
        Animation animation = AnimationUtils.loadAnimation(Con, animationid  );
        animation.setStartOffset(StartOffset);
        v.startAnimation(animation);

        return animation;
    }
    return null;
}
你可以这样调用它:
PlayAnim(R.id.bottombar, (Context) this, R.anim.slide_right_in, 0);

含义:

第一个参数是您要应用动画的视图的id。

第二个参数是在您的活动中检索到的上下文。

第三个参数是您放置在anim资源文件夹中或从Android预定义动画中获取的所需动画。

第四个参数是动画开始偏移量。


嗨,我说的是编程,不是XML,而且这对我没用,你在哪里添加动画代码? - maza23
1
抱歉,您在我的示例中希望看到哪一部分没有用代码完成? - Richard Lalancette
5
抱歉,我没找到你提到想要用编程完成所有这些事情的地方。 - Richard Lalancette
你可以在这里使用任何动画,Paulo。不一定要使用slide_bottom_in。我已经附上了另一个例子。 - Richard Lalancette

7
我测试了你的动画按钮实现,它可以正常工作。可能存在其他问题,很可能是你将按钮添加到布局的方式不正确。
要将你的按钮添加到相对布局中,请使用以下代码。
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
MyButton b1 = new MyButton(Main.this);
b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(b1);

或者您可以通过布局来充气(button)。为此,请创建布局mybtn.xml,其中包含您的按钮实现:

<?xml version="1.0" encoding="utf-8"?>
<PACKAGE_OF_MYBUTTON_HERE.MyButton
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />

要将其添加到您的布局中,请调用:

RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
Button b = (Button)getLayoutInflater().inflate(R.layout.mybtn, rl, false);
rl.addView(b);

当你把视图添加到相对布局时,可能会出现位置不正确的问题。在调用rl.addView(b1)之前添加以下代码即可解决问题 (这段代码将在someOtherView下面添加新按钮)。

LayoutParams lp = new LayoutParams(b.getLayoutParams());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId());
b.setLayoutParams(lp);

0

在添加视图之前,您可以尝试将此代码添加到您的代码中。我猜想,这个代码对于任何视图更改都会起作用。在我的情况下,是通过动画切换了2个视图。

TransitionManager.beginDelayedTransition(layoutlocation);//layoutlocation is parent layout(In my case relative layout) of the view which you gonna add.

希望它能正常工作。我花了两天时间让它运行起来。

0

并不总是需要使用动画类来实现真正的动画效果。我们可以使用handler在将视图添加到布局时提供延迟,如下所示。

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        Animation fadeanimation = new AlphaAnimation(0,1);
        fadeanimation.setDuration(position*60+100);
        child.setAnimation(fadeanimation);
        linearLayout.addView(child);
    }
}, position*60);

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