在onBackPressed()被覆盖时保留过渡动画

时间:2018-10-26 11:05:05

标签: java android android-transitions onbackpressed

我正在使用以下过渡动画从活动A转到活动B:

    Intent intent = new Intent(this, activityB.class);
    ImageView myImage = view.findViewById(R.id.myImage);
    ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(this, myImage, "myImage");
    startActivity(intent,activityOptions.toBundle());

在activityB上,当我按回时,过渡动画仍在工作。但是在activityB上,我需要覆盖onBackPressed:

    @Override
    public void finish() {
        super.finish();
        Log.d("myTag", "finish: ");
    }

    @Override
    public void onBackPressed() {
        finish();
    }

但是当我再次按下activityB时,不再应用过渡动画。

当onBackPressed被覆盖时,如何保持按下后的过渡动画效果?

1 个答案:

答案 0 :(得分:0)

您可以遵循以下代码:

首先,您使用的是这样的

public class exampleActivity extends Activity implements DrawPolygonCanvas.OnGetBitmapListener {

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

        img = (ImageView) findViewById(R.id.office);

        DrawPolygonCanvas canvas = (DrawPolygonCanvas) findViewById(R.id.DrawPolygonCanvas);
        canvas.setOnGetBitmapListener(this);
    }

    @Override
    public Bitmap onGetBitmap() {
        Bitmap bm = ((BitmapDrawable) img.getDrawable()).getBitmap();
        return bm;
    }
}

现在您可以像这样在onBackPressed()上使用动画了:

Intent intent = new Intent(ActivityFirst.this, ActivitySecond.class);
startActivity(intent);
overridePendingTransition(R.anim.right_in, R.anim.left_out);