Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shrink a finishing activity to a rectangle

In my Android app, I have a floating Activity. It's started from outside my app using ActivityOptions.makeScaleUpAnimation to scale up from an "originating" rectangle. When my Activity finishes, I'd like it to do the reverse of that animation: i.e. it shrinks back to that rectangle as it fades out.

I know I can get the rectangle with getIntent().getSourceBounds(), and I'd hoped to be able to use overridePendingTransition() when finishing to achieve this effect, but overridePendingTransition() can only accept a fixed XML resource: there doesn't seem to be a way to make that animation depend on the source bounds. Is there something else I can use to achieve this effect?

My app is for API 11+, but as it's a cosmetic effect only, I'd be satisfied with a solution that depends on a later version.

like image 361
Dan Hulme Avatar asked Mar 05 '13 11:03

Dan Hulme


People also ask

What is the activity in Android studio?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you get a response from an activity in Android?

You must call the second activity using the startActivityForResult method. In your second activity, when it is finished, you can execute the setResult method where basically you put the result information. Then, on your first activity, you override the onActivityResult method.

Can we create activity without UI in Android?

Explanation. Generally, every activity is having its UI(Layout). But if a developer wants to create an activity without UI, he can do it.


1 Answers

Based on my last comment, here is the solution which i have tried and it works. You may have to modify to suit your requirements.

Implement a Activity with Transparent background and with no title inside the manifest:

       <activity
        android:name="com.example.backgroundsensor.AnimatedActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="Animated activity" /> 

and let it set the content view with a layout as :

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/transparent"
   tools:ignore="MergeRootFrame" >

    <View
         android:id="@+id/visibleAreaView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:background="@android:color/holo_green_dark" />

 </FrameLayout>

The visibleAreaView is the one you will see on the screen, since activity is transparent. You can set the bounds of the view in OnCreate of the activity().

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

      // set the bounds of the animateView
  }

Also, Override the finish method something like this:

boolean animateFirst=true;
@Override
public void finish() {
     if(animateFirst)
     {
         animateFirst = false;
         loadAnim();

     }else
     {
         super.finish();
     }
}

 public void loadAnim() {

    View v = findViewById(R.id.animateView);
    float x= v.getX() + v.getRight()/2;
    float y = v.getY();
    anim = new ScaleAnimation(1.0f, 0.0f,1.0f, 0.0f, x, y);
    anim.setDuration(300);
    anim.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) {
            findViewById(R.id.animateView).setVisibility(View.GONE);
            AnimatedActivity.this.finish();
        }
    });
    v.startAnimation(anim);

}
like image 153
AjayV Avatar answered Oct 20 '22 00:10

AjayV