我有一个带有两个的应用程序activities
,其中第二个用作图像“选择器”并向第一个提供一些信息,以便ImageView
可以相应地更新。
为此,我activity
通过调用第二个startActivityForResult()
,并ImageView
根据收到的信息更新onActivityResult()
。
当我尝试为选取器和主元素之间的共享元素设置动画时,问题就出现了Activity
:看起来过渡“快照”是第一个Activity
,即使ImageView
在恢复后立即更新Activity
,过渡动画也会“闪烁”一个旧的View
显示当前版本之前的版本。
这是我为说明我在说什么而制作的示例应用程序:
无缝实现此动画的最佳方式是什么?是否应该使用另一种方法?
这是用于示例的代码:
主要活动
public class MainActivity extends AppCompatActivity {
ImageView mainImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Main Activity");
mainImageView = (ImageView) findViewById(R.id.imageView);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PickerActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(MainActivity.this, mainImageView, "shape_transition");
startActivityForResult(intent, 1, options.toBundle());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
int shapeNumber = intent.getIntExtra("shape_number", -1);
switch (shapeNumber) {
case 1:
mainImageView.setImageResource(R.drawable.blue);
break;
case 2:
mainImageView.setImageResource(R.drawable.green);
break;
default:
//something else
break;
}
}
}
选择器活动:
public class PickerActivity extends AppCompatActivity {
ImageView blueImageView;
ImageView greenImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker);
getSupportActionBar().setTitle("Picker Activity");
blueImageView = (ImageView) findViewById(R.id.imageView2);
blueImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
blueImageView.setTransitionName("shape_transition");
Intent intent = new Intent(PickerActivity.this, MainActivity.class);
intent.putExtra("shape_number", 1);
setResult(RESULT_OK, intent);
finishAfterTransition();
}
});
greenImageView = (ImageView) findViewById(R.id.imageView3);
greenImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
greenImageView.setTransitionName("shape_transition");
Intent intent = new Intent(PickerActivity.this, MainActivity.class);
intent.putExtra("shape_number", 2);
setResult(RESULT_OK, intent);
finishAfterTransition();
}
});
}
}
主要活动 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="demonstration.sharedelementproblemexample.MainActivity">
<ImageView
android:layout_width="128dp"
android:layout_height="128dp"
android:id="@+id/imageView"
android:transitionName="shape_transition"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:src="@drawable/red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true" />
</RelativeLayout>
选择器活动 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="demonstration.sharedelementproblemexample.PickerActivity">
<ImageView
android:layout_width="128dp"
android:layout_height="128dp"
android:id="@+id/imageView2"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="@drawable/blue" />
<ImageView
android:layout_width="128dp"
android:layout_height="128dp"
android:id="@+id/imageView3"
android:src="@drawable/green"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/imageView2" />
</RelativeLayout>