Android 如何将收尾活动缩小为矩形
在我的Android应用程序中,我有一个浮动的Android 如何将收尾活动缩小为矩形,android,android-activity,android-animation,Android,Android Activity,Android Animation,在我的Android应用程序中,我有一个浮动的活动。它从我的应用程序外部开始,使用从“原始”矩形放大。当我的活动完成时,我希望它做与动画相反的操作:也就是说,当它淡出时,它收缩回那个矩形 我知道我可以使用getIntent().getSourceBounds()获得矩形,我希望能够在完成时使用它来实现此效果,但是overridePendingTransition()只能接受固定的XML资源:似乎没有办法使动画依赖于源边界。我是否可以使用其他方法来实现此效果 我的应用程序适用于API 11+,但由
活动
。它从我的应用程序外部开始,使用从“原始”矩形放大。当我的活动
完成时,我希望它做与动画相反的操作:也就是说,当它淡出时,它收缩回那个矩形
我知道我可以使用getIntent().getSourceBounds()
获得矩形,我希望能够在完成时使用它来实现此效果,但是overridePendingTransition()
只能接受固定的XML资源:似乎没有办法使动画依赖于源边界。我是否可以使用其他方法来实现此效果
我的应用程序适用于API 11+,但由于它只是一种表面效果,我对依赖于更高版本的解决方案感到满意。我认为除了overridePendingTransition调用之外,没有其他方法可以缩小活动窗口。但是,此代码可能有助于: 放大 ActivityOptions opts=ActivityOptions.makeCustomAnimation(getActivity(),R.anim.scale_up,0); startActivity(新意图(this,SecondActivity.class),opts.toBundle()) 以下是缩放动画文件:
<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>
<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>
以下是缩放动画文件:
<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>
<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比例以获得所需的矩形。希望这能有所帮助。根据我最后的评论,这里是我尝试过的解决方案,它很有效。您可能需要修改以满足您的要求 使用透明背景和清单中没有标题实施活动:
<activity
android:name="com.example.backgroundsensor.AnimatedActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="Animated activity" />
另外,重写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);
}
你有没有用过这个?我自己来对付它。谢谢你尝试过将活动显示为对话框()吗?@dacongy这有什么帮助?我以为你想将活动显示为矩形,而不是全屏。你能从用户的角度解释一下你需要的效果吗?假设我是你的应用程序的用户,我应该看到什么?@dacongy该活动已经是非全屏的,带有有色背景。启动活动时,您会看到它从源矩形(由启动活动在
Intent
中设置)向上扩展,直到达到正确的大小。我想要的是,当您离开活动时,它会缩小到相同的矩形,然后完全消失。上述代码适用于api 16及以上版本,但您可以尝试使用支持库中的ActivityOptions Compat。正如我在问题中所说,我无法在XML文件中设置矩形,因为它来自意图的源边界。我相信对于你的情况,你可以有一个没有标题的透明活动,并且在其中有视图/片段,它将根据你的需要进行动画制作。
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);
}