我是看了别人的博客学习的,,大致共享动画分为5.0之前和之后,我这里就只讲5.0之后。5.0之后呢系统就有共享动画的api来快捷使用共享动画。我写贴上我的动画:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView mIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIv = (ImageView) findViewById(R.id.iv1);
mIv.setImageResource(R.mipmap.ic_launcher);
mIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.
makeSceneTransitionAnimation(MainActivity.this, mIv, "img");
startActivity(intent, optionsCompat.toBundle());
}
});
}
}
就是一个设置了点击事件跳转界面,现在里面的共享参数设置都是这样写的,我里面的"img"就是共享参数,mIv就是共享控件。XML如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:transitionName="img" />
</LinearLayout>
注意我XML里面的最后一句话:andorid:transitionName = "img",这个是将控件与共享标记关联起来,也可以用代码设置。然后我的第二个界面SecondActivity代码如下:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
findViewById(R.id.iv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
finishAfterTransition();
}
}
activity_sec的XML如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
android:transitionName="img" />
</RelativeLayout>
这里面有几个比较注意的地方:
(1)重写onBackPressed()
(2)点击返回的事件不要直接调用finish();而是调用onBackPressed()
(3)这个activity的主题,如下:
<activity
android:name=".SecondActivity"
android:screenOrientation="portrait"
android:theme="@style/app_theme_transparent"/>
<style name="app_theme_transparent" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
app的主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>