Thursday, October 11, 2018

How to shrink a finishing activity to a rectangle

Leave a Comment

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.

2 Answers

Answers 1

I don think there is a way to scale down the activity window other than the overridePendingTransition call. However this code may help:

Scale up

ActivityOptions opts = ActivityOptions.makeCustomAnimation(getActivity(), R.anim.scale_up, 0); startActivity(new Intent(this, SecondActivity.class),opts.toBundle());

Here is the scale_up animation file:

<set  xmlns:android="http://schemas.android.com/apk/res/android">     <scale     android:fromXScale="0"     android:toXScale="1.0"     android:fromYScale="0"     android:toYScale="1.0"     android:pivotX="50%p"     android:pivotY="50%p"     android:duration="500"/> </set> 

Scale down

Call this code from the exiting activity:

finish();  overridePendingTransition(0, R.anim.scale_down); 

Here is the scale_down animation file:

<set xmlns:android="http://schemas.android.com/apk/res/android">    <scale     android:fromXScale="1.0"     android:toXScale="0"     android:fromYScale="1.0"     android:toYScale="0"     android:pivotX="50%p"     android:pivotY="50%p"     android:duration="250"/> </set> 

You can vary the X and Y scale to get the desired rectangle. Hope this helps.

Answers 2

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);  } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment