android studio:MotionLayout过渡未在屏幕中以编程方式启动

ghhaqwfi  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(112)

我有一张欧洲最北部的Map,我想做的是从程序上开始一个特定的过渡。我总共创建了6个过渡,因为我有7个国家。例如,trans 1:立陶宛-〉拉脱维亚,trans 2:拉脱维亚-〉爱沙尼亚,trans 3:爱沙尼亚-〉芬兰等,所以技术上,不需要用户触摸或滑动屏幕,甚至把任何按钮来启动这个动画,我想只是开始一个特定的过渡,让我们说过渡trans 3。
我已成功创建了过渡和约束集,
这是一个布局文件,imageView“main”将在其中进行动画处理:(imageView 3是Map)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/motion_layout"

    app:motionDebug="SHOW_ALL"
    app:layoutDescription="@xml/activity_map2_scene"
    tools:context=".Map2">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/dok2" />

    <ImageView
        android:id="@+id/main"
        android:layout_width="30dp"
        android:layout_height="29dp"
        app:layout_constraintBottom_toBottomOf="@+id/imageView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.818"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.469"
        app:srcCompat="@drawable/maincircle" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/bt_play2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PLAY"
        android:background="@drawable/custom_buttom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.903" />

</androidx.constraintlayout.motion.widget.MotionLayout>

这是源文件

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        android:id="@+id/trans1"
        motion:constraintSetEnd="@+id/latvia"
        motion:constraintSetStart="@id/lithuania"
        motion:pathMotionArc="startHorizontal"
        
        motion:duration="3000">
    </Transition>
    <Transition
        android:id="@+id/trans2"
        motion:constraintSetStart="@+id/latvia"
        motion:constraintSetEnd="@+id/estonia"
        
        motion:pathMotionArc="startHorizontal"
        motion:duration="3657" >
        <KeyFrameSet >
            <KeyPosition
                motion:motionTarget="@+id/main"
                motion:framePosition="50"
                motion:keyPositionType="deltaRelative"
                motion:percentX="5"
                motion:pathMotionArc="startVertical" />
            <KeyAttribute
                motion:motionTarget="@+id/main"
                motion:framePosition="27"
                android:translationZ="6dp" />

        </KeyFrameSet>
    </Transition>
    <Transition
        android:id="@+id/trans3"
        motion:constraintSetStart="@+id/estonia"
        motion:constraintSetEnd="@+id/finland"
        
        motion:duration="2729">
        <KeyFrameSet>
            <KeyPosition
                motion:motionTarget="@+id/main"
                motion:framePosition="50"
                motion:keyPositionType="deltaRelative"
                motion:percentX="-5" />
        </KeyFrameSet>
    </Transition>
    <Transition
        android:id="@+id/trans4"
        motion:constraintSetStart="@+id/finland"
        motion:constraintSetEnd="@+id/sweden"
        
        motion:pathMotionArc="startVertical"
        motion:duration="2652" >
        <KeyFrameSet>
            <KeyPosition
                motion:motionTarget="@+id/main"
                motion:framePosition="90"
                motion:keyPositionType="deltaRelative"
                motion:pathMotionArc="startHorizontal"
                motion:percentY="-0.6"
                motion:percentX="0.7" />

        </KeyFrameSet>
    </Transition>
    <Transition
        android:id="@+id/trans5"
        motion:constraintSetStart="@+id/sweden"
        motion:constraintSetEnd="@+id/denmark"
        
        motion:duration="2265" >
        <KeyFrameSet>
            <KeyPosition
                motion:motionTarget="@+id/main"
                motion:framePosition="50"
                motion:keyPositionType="deltaRelative"
                motion:percentX="-0.1"
                motion:percentY="0.75" />
        </KeyFrameSet>
    </Transition>
    <Transition
        android:id="@+id/trans6"
        motion:constraintSetStart="@+id/denmark"
        motion:constraintSetEnd="@+id/norway"
        
        motion:duration="3000" >
        <KeyFrameSet>
            <KeyPosition
                motion:motionTarget="@+id/main"
                motion:framePosition="50"
                motion:keyPositionType="deltaRelative"
                motion:percentX="1.25"
                motion:percentY="0.5" />
        </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/lithuania">
        <Constraint
            motion:layout_constraintVertical_bias="0.746"
            android:layout_height="18dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.898"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="18dp"
            android:id="@+id/main" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/latvia">
        <Constraint
            motion:layout_constraintVertical_bias="0.659"
            android:layout_height="19dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.833"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="22dp"
            android:id="@+id/main" />

    </ConstraintSet>
    <ConstraintSet
        android:id="@+id/estonia"
         >
        <Constraint
            motion:layout_constraintVertical_bias="0.557"
            android:layout_height="24dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.838"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="21dp"
            android:id="@+id/main" />

    </ConstraintSet>

    <ConstraintSet android:id="@+id/finland" >
        <Constraint
            motion:layout_constraintVertical_bias="0.492"
            android:layout_height="29dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.842"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="30dp"
            android:id="@+id/main" />
    </ConstraintSet>
    <ConstraintSet android:id="@+id/sweden" >
        <Constraint
            motion:layout_constraintVertical_bias="0.559"
            android:layout_height="29dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.559"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="30dp"
            android:id="@+id/main" />
    </ConstraintSet>
    <ConstraintSet android:id="@+id/denmark" >
        <Constraint
            motion:layout_constraintVertical_bias="0.746"
            android:layout_height="29dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.325"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="30dp"
            android:id="@+id/main" />
    </ConstraintSet>
    <ConstraintSet android:id="@+id/norway" >
        <Constraint
            motion:layout_constraintVertical_bias="0.556"
            android:layout_height="29dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="@+id/imageView3"
            motion:layout_constraintHorizontal_bias="0.267"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="30dp"
            android:id="@+id/main" />
    </ConstraintSet>

</MotionScene>

这是我想让过渡开始的场景

package com.example.discovereurope2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.motion.widget.MotionLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Map2 extends AppCompatActivity {
    private Button play_button;
    private Region region;
    private MotionLayout motionLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map2);
        getSupportActionBar().hide();

        play_button = findViewById(R.id.bt_play2);
        String n = getIntent().getStringExtra("country");
        region = (Region) getIntent().getSerializableExtra("RegionClass");

        motionLayout = findViewById(R.id.motion_layout) ;
        motionLayout.setTransition(R.id.trans3);
        motionLayout.animate();

        play_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Map2.this, Questions.class);
                String cCountry = String.valueOf(region.getCurrentCountryID());
                intent.putExtra("countryID",cCountry);
                intent.putExtra("RegionClass", region);
                startActivity(intent);
            }
        });
    }
}

正如你所看到的,我得到了motionLayout,我调用了这个函数来启动我想要的特定过渡,但是什么也没有发生。我怎么才能启动一个特定的过渡呢?比如说trans 3?这就是我想要的,我不需要onClick或onSwipe或者类似的东西,也不需要autoTransition。我试着到处找东西,但是几乎每个人都使用onClick或onSwipe。

dkqlctbz

dkqlctbz1#

您位于attachToWindow之前,因此可以使用的命令数量有限。

motionLayout.setTransition(R.id.trans3);
motionLayout.setProgress(0)

您可能需要具有属性motion:autoTransition="animateToEnd"的特定过渡
典型的使用案例是固定启动转换。
.animate()是视图上用于获取“ViewPropertyAnimator”的方法,它与MotionLayout无关。

相关问题