在我的安卓应用程序中,我有一个浮动的Activity
。它是从我的应用程序外部开始的,使用ActivityOptions.makeScaleUpAnimation
从一个“原始”矩形向上扩展。当我的Activity
结束时,我想让它做与动画相反的操作:即,当它淡出时,它会收缩回那个矩形。
我知道我可以用getIntent().getSourceBounds()
获得矩形,并且我希望在完成时能够使用overridePendingTransition()
来实现这个效果,但是overridePendingTransition()
只能接受固定的XML资源:似乎没有一种方法可以使动画依赖于源代码边界。有没有其他我可以用来达到这个效果的东西?
我的应用程序是针对API 11+的,但由于它只是一种美化效果,我会对依赖于更高版本的解决方案感到满意。
发布于 2014-10-07 04:40:15
我认为除了overridePendingTransition调用之外,还有一种方法可以缩小activity窗口。然而,下面的代码可能会有所帮助:
纵向扩展
ActivityOptions opts = ActivityOptions.makeCustomAnimation(getActivity(),R.anim.scale_up,0);startActivity(ActivityOptions(this,SecondActivity.class),opts.toBundle());
以下是scale_up动画文件:
<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>
缩小规模
从正在退出的活动中调用以下代码:
finish();
overridePendingTransition(0, R.anim.scale_down);
以下是scale_down动画文件:
<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>
您可以改变X和Y比例以获得所需的矩形。希望这能有所帮助。
发布于 2014-10-07 13:13:53
基于我的上一条评论,这是我尝试过的解决方案,它是有效的。您可能需要进行修改以满足您的要求。
实现一个具有透明背景并且在清单中没有标题的活动:
<activity
android:name="com.example.backgroundsensor.AnimatedActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="Animated activity" />
并让它将内容视图的布局设置为:
<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>
您将在屏幕上看到visibleAreaView,因为活动是透明的。您可以在activity()的OnCreate中设置视图的边界。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animated_activity);
// set the bounds of the animateView
}
另外,覆盖finish方法,如下所示:
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);
}
https://stackoverflow.com/questions/15222872
复制相似问题