Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate an adding of a view in Android?

I would like to know if there is a simple way to add a view (a button) to a RelativeLayout, with some kind of scale animation. I extended a class from Button and did something like this:

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

Then tried to add this button to a view and it didn't work. Please help!

like image 544
maza23 Avatar asked Sep 15 '11 14:09

maza23


People also ask

How do you animate a view?

Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.

What are the two different types of view animation?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .

Can you animate on Android?

With over a million downloads, Animation Desk is one of the best animation apps for Android that you can try. It has all the things that make an animation app great—a clutter-free user interface, an adequate amount of options, and a handful of ways to export the finished animation.


2 Answers

In your activity, use instead:

parentview.addView(myButton);

Then animate the button with this:

Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_right_in);
animation.setStartOffset(0);
myButton.startAnimation(animation);

This is an example of 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>

In addition, This is a activity play animation function I wrote:

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

You can call this like this:

PlayAnim(R.id.bottombar, (Context) this, R.anim.slide_right_in, 0);

Where:

1st parameter is the id of the view you want to apply the animation on.

2nd paramenter isThe context retrieved inside your activity.

3rd parameter is the desired animation that you put inside your anim resource folder or from android predefined animations.

4rd paremeter is the animation startoffset.

like image 98
Richard Lalancette Avatar answered Oct 01 '22 16:10

Richard Lalancette


I tested your animated button implementation and it works correctly. There must be some other problem. Probably the way you add the button to the layout.

To add your button to the relative layout use code like this.

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

Or you can inflate the button from layout. To do this create layout mybtn.xml containing your button implementation:

<?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"
  />

To add it to your layout call:

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

There might be a problem with proper positioning of your view when you add it to the relative layout. Just add code like this before calling rl.addView(b1) (the code snippet adds new button below someOtherView).

LayoutParams lp = new LayoutParams(b.getLayoutParams());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId());
b.setLayoutParams(lp);
like image 38
Tomik Avatar answered Oct 01 '22 17:10

Tomik