hussi
hussi

Reputation: 77

Resizing a view through animation in Android

I have an activity in which i have a button in parent layout then there is relative layout of height,width (100,100) in this parent layout.What i want is :

  1. when i click button on parent layout.Then relative layout slides to screen from off screen position.
  2. And After coming to screen when i click this relative layout it animates by adjusting its width height to fill_parent
  3. Then after again clicking this relative layout it animates back to its original size say (100,100)

I have achieved Task 1 through simple translate animation.

Task 2 is also achieved here is my code for task 2

slide.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

slide.startAnimation(zoom_in);

My zoom animation for this task is

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale="0.5" android:toXScale="1.0"
   android:fromYScale="0.5" android:toYScale="1.0"
   android:pivotX="0%p" android:pivotY="0%p"
   android:duration="800" />
</set>

What i do is when user click on this relative layout i change the layout param of this layout from 100 to match parent and apply animation and it is working like a charm.

My problem is i don't have any clue how to implement task 3 Any help regarding this will the highly appreciated

BTW i am using following technique to achieve task 3 but nothing achieved :(

slide.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
slide.startAnimation(zoom_exit);

The XML file:

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.5"
    android:toYScale="0.5" >
</scale>

My complete java code

public class MainActivity extends Activity {

Button b,b1;
RelativeLayout slide;
Animation move_in,zoom_out,zoom_exit;
int x=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b=(Button) findViewById(R.id.button1);
    b1=(Button) findViewById(R.id.button2);
    slide=(RelativeLayout) findViewById(R.id.slider);
    move_in = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.move_in);
    zoom_out = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.zoom_enter1);
    zoom_exit = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.test2);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(x == 0){



            slide.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


            slide.startAnimation(zoom_out);

            x=1;}
            else {

                //slide.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));


                    slide.startAnimation(zoom_exit);


                    x=0;


            }

        }
    });

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            /*Intent i2 = new Intent(MainActivity.this, MainActivity1.class);
            startActivity(i2);
            finish();
            overridePendingTransition(R.anim.zoom_enter1, R.anim.zoom_exit);*/

            slide.setVisibility(View.VISIBLE);
            slide.startAnimation(move_in);



        }
    });





}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

My complete xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="64dp"
    android:text="Button" />

<RelativeLayout 

   android:id="@+id/slider"
   android:layout_height="200dp"
   android:layout_width="200dp"
   android:background="#111111"
   android:visibility="gone"
   android:layout_alignParentTop="true"
   >


   <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_centerHorizontal="true"
    android:layout_marginTop="64dp"
    android:text="Button" />


</RelativeLayout> 




</RelativeLayout>

Move in animation

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

Upvotes: 1

Views: 4111

Answers (1)

Ammar ali
Ammar ali

Reputation: 1503

Main class

package com.testanimation;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;

    public class MainActivity extends Activity {
        Button Btn, btn1;
        RelativeLayout slide;
        Animation move_in, zoom_out, zoom_exit;
        int x = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Btn = (Button) findViewById(R.id.button1);
            btn1 = (Button) findViewById(R.id.button2);
            slide = (RelativeLayout) findViewById(R.id.slider);
            move_in = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.move);
            zoom_out = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.zoomin);
            zoom_exit = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.zoomout);

            zoom_exit.setAnimationListener(new AnimationListener() {

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

                }

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

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    slide.setLayoutParams(new RelativeLayout.LayoutParams(400, 410));

                }
            });
            btn1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (x == 0) {

                        slide.setLayoutParams(new RelativeLayout.LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.MATCH_PARENT));

                        slide.startAnimation(zoom_out);

                        x = 1;
                    } else {

                        slide.startAnimation(zoom_exit);

                        x = 0;

                    }

                }
            });

            Btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    slide.setVisibility(View.VISIBLE);
                    slide.startAnimation(move_in);

                }
            });

        }

    }

Move Xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="400"
        android:fromYDelta="-100%"
        android:toYDelta="0%" />

</set>

ZoomOut

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="-5%"
    android:pivotY="-20%"
    android:toXScale="0.6"
    android:toYScale="0.5" >

</scale>

Zoomin

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >

    <scale
        android:duration="800"
        android:fromXScale="0.5"
        android:fromYScale="0.5"
        android:pivotX="0%p"
        android:pivotY="0%p"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

Upvotes: 1

Related Questions