这是我的项目基本结构:
我有以下3个活动 1)MainActivity 2)CalculateActivity 3)SharedActivity
我只是想在我的CalculateActivity和我的SharedActivity之间添加共享视图动画
当我仅将onClick侦听器添加到CalculateActivity的布局中时,但是当我尝试通过此代码添加动画时,效果很好:
Intent intent = new Intent(CalculateActivity.this, SharedActivity.class);
Pair<View, String> p1 = Pair.create((View)imageView1, "profile");
Pair<View, String> p2 = Pair.create((View)textView2, "textView7");
Pair<View, String> p3 = Pair.create((View)textView, "textView8");
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(CalculateActivity.this, p1, p2, p3);
startActivity(intent,options.toBundle());
我的应用程序关闭了!!
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.dateofbirth">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CalculateActivity" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
<activity android:name=".SharedActivity"></activity>
</application>
CalculateActivity的XML文件
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_image"
android:gravity="center"
android:orientation="vertical"
>
<android.support.constraint.ConstraintLayout
android:transitionName="card_layout_transition"
android:id="@+id/constraintLayout"
android:layout_width="357dp"
android:layout_height="119dp"
android:background="@drawable/card_layout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:transitionName="@string/blue_button"
android:id="@+id/imageView"
android:layout_width="79dp"
android:layout_height="73dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/circle_image" />
<TextView
android:transitionName="text_transition"
android:id="@+id/textView7"
android:layout_width="229dp"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:layout_marginBottom="12dp"
android:fontFamily="@font/nunito_extralight"
android:text="Hey You've get something!"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:transitionName="text_transition2"
android:id="@+id/textView8"
android:layout_width="223dp"
android:layout_height="39dp"
android:layout_marginBottom="26dp"
android:text="@string/lorem_ipsum_dolor_sit_amet_consectetur_adipiscing_elit_se"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView7"
app:layout_constraintStart_toStartOf="@+id/textView7"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
</android.support.constraint.ConstraintLayout>
SharedActivity的XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_image"
android:id="@+id/parent"
tools:context=".SharedActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/parent1"
android:layout_width="391dp"
android:layout_height="355dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:background="@drawable/card_layout"
android:transitionName="card_layout_transition">
<ImageView
android:id="@+id/image_id"
android:layout_width="151dp"
android:layout_height="162dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:src="@drawable/circle_image"
android:transitionName="@string/blue_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.44"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="247dp"
android:layout_height="21dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Hey you've Got something !"
android:textColor="#707070"
android:textSize="15sp"
android:transitionName="text_transition"
app:fontFamily="@font/nunito_extralight"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.595" />
<TextView
android:textColor="@color/textcolor"
android:id="@+id/textView3"
android:layout_width="261dp"
android:layout_height="73dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="44dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing"
android:transitionName="text_transition2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.563"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
这是我的Logcat
com.google.android.apps.gsa.shared.l.c: Failed to connect: 16
at com.google.android.apps.gsa.shared.l.a.b(SourceFile:42)
at com.google.android.apps.gsa.staticplugins.z.c.bvq(SourceFile:12)
at com.google.android.apps.gsa.clockwork.b.a(Unknown Source)
at com.google.android.apps.gsa.staticplugins.s.b.a.a(SourceFile:2)
at com.google.android.apps.gsa.staticplugins.s.b.d.call(Unknown Source)
at com.google.android.libraries.gsa.runner.a.a.c(SourceFile:16)
at com.google.android.libraries.gsa.runner.a.e.bjz(Unknown Source)
at com.google.android.apps.gsa.shared.logger.e.a.a.a.a(SourceFile:55)
at com.google.android.apps.gsa.shared.logger.e.a.a.g.bjz(Unknown Source)
at com.google.common.r.a.ct.eZX(SourceFile:9)
at com.google.common.r.a.bq.run(SourceFile:5)
at com.google.common.r.a.cs.run(SourceFile:9)
at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(Unknown Source)
at com.google.android.apps.gsa.shared.util.concurrent.b.bs.run(SourceFile:3)
at com.google.android.apps.gsa.shared.util.concurrent.b.bs.run(SourceFile:3)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
at com.google.android.apps.gsa.shared.util.concurrent.b.o.run(SourceFile:6)
12-16 16:15:31.446 19642-19647/? E/Sensors: sns_smr_util.c(106):Unable to initialize service 256 with QCCI, timed out (10000 ms)
12-16 16:15:31.447 19642-19647/? E/Sensors: sns_sam_mr.c(1012):Unable to initialize service 0 with QCCI
12-16 16:15:31.447 19642-19647/? E/Sensors: sns_sam_mr.c(1107):Unable to acquire service handle 0
12-16 16:15:31.449 19642-19645/? E/Sensors: sns_reg_la.c(91):reg_read: reg file not open
12-16 16:15:31.449 19642-19647/? E/Sensors: mag_cal.c(1609):mag_cal_sam_init: Unknown UUID(1) 00000000-0000-0000
12-16 16:15:31.449 19642-19645/? E/Sensors: sns_reg.c(291):Received error from sns_reg_read/write 5
12-16 16:15:31.449 19642-19647/? E/Sensors: mag_cal.c(1614):mag_cal_sam_init: Unknown UUID(2) 0000-000000000000
12-16 16:15:31.449 19642-19647/? E/Sensors: sns_sam.c(4433):Failed to register algorithm service 16 with SAM
12-16 16:15:31.450 19642-19645/? E/Sensors: sns_reg_la.c(91):reg_read: reg file not open
12-16 16:15:31.450 19642-19645/? E/Sensors: sns_reg.c(291):Received error from sns_reg_read/write 5
12-16 16:15:31.450 19642-19645/? E/Sensors: sns_reg_la.c(91):reg_read: reg file not open
12-16 16:15:31.450 19642-19645/? E/Sensors: sns_reg.c(291):Received error from sns_reg_read/write 5
12-16 16:15:31.451 19642-19645/? E/Sensors: sns_reg_la.c(91):reg_read: reg file not open
12-16 16:15:31.451 19642-19645/? E/Sensors: sns_reg.c(291):Received error from sns_reg_read/write 5
12-16 16:15:31.452 19642-19645/? E/Sensors: sns_reg_la.c(91):reg_read: reg file not open
12-16 16:15:31.452 19642-19645/? E/Sensors: sns_reg.c(291):Received error from sns_reg_read/write 5
12-16 16:15:31.453 19642-19645/? E/Sensors: sns_reg_la.c(91):reg_read: reg file not open
答案 0 :(得分:3)
您可以使用以下代码实现动画:
bottom_down.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1500"
android:fromYDelta="5"
android:toYDelta="90%" />
</set>
bottom_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="90%"
android:toYDelta="0" />
</set>
nothing.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromYDelta="0%p"
android:toYDelta="0%p" />
开始第二活动:
Intent intent = new Intent(CalculateActivity.this, SharedActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.bottom_up, R.anim.nothing);
第二次活动结束时:
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.nothing, R.anim.bottom_down);
}
您可以根据自己的喜好更改xml动画