问题
I am using zoom in animation for a view in my app. When the user clicks on a button the next activity zooms in from that button. This is what I have achieved using the code give below.
Since the in the video mentioned above, the code that was shown only worked for jellybean and above I had to use the code given below.
Next_Activity.java (The activity with zoom in animation)
Bundle b = getIntent().getExtras(); // Bundle passed from previous activity to this activity
x = b.getInt("X"); //b is button in previous activity
y = b.getInt("Y"); //b is button in previous activity
xh = b.getInt("XH"); //b is button in previous activity
yh = b.getInt("YH"); //b is button in previous activity
AnimationSet set = new AnimationSet(true);
width = display.getWidth();
height = display.getHeight();
Animation scale = new ScaleAnimation( (float)xh/width, 1f, (float)yh/height , 1f, x, y);
Animation alpha = new AlphaAnimation(.75f, 1f);
set.addAnimation(scale);
set.addAnimation(alpha);
set.setDuration(300);
main.startAnimation(set); //main is rootLayout of this activity
Main_Activity(The Activity with button)
Bundle bundle = new Bundle();
int[] xy = new int[2];
button.getLocationOnScreen(xy);
bundle.putInt("X", xy[0] + (b.getWidth() / 2));
bundle.putInt("Y", xy[1] + (b.getHeight() / 2));
bundle.putInt("XH", b.getWidth());
bundle.putInt("YH", b.getHeight());
Intent startnextactivity = new Intent(Table_main.this,Next_Activity.class);
startnextactivity.putExtras(bb);
startActivity(startnextactivity);
Now, my question is how do I reverse this animation? I mean when I click on the button, the activity zooms in from that button. So how do I zoom out the activity to the same button when the back button is pressed?
@Override
public void onBackPressed()
{
Animation scale = new ScaleAnimation( (float)xh/width, 1f, (float)yh/height , 1f, x, y);
// What is the zoom out animation of the above line??
}
回答1:
@Override
public void onBackPressed() {
Bundle b = getIntent().getExtras();
x = b.getInt("X");
y = b.getInt("Y");
xh = b.getInt("XH");
yh = b.getInt("YH");
AnimationSet set = new AnimationSet(true);
width = display.getWidth();
height = display.getHeight();
Animation scale = new ScaleAnimation((1f, (float) xh/width, 1f, (flaot) yh/height, x, y);
Animation alpha = new AlphaAnimation(.75f, 1f);
set.addAnimation(scale);
set.addAnimation(alpha);
set.setDuration(300);
main.startAnimation(set);
}
来源:https://stackoverflow.com/questions/22057925/zoom-out-animation-android