{"id":454,"date":"2025-02-18T15:02:18","date_gmt":"2025-02-18T07:02:18","guid":{"rendered":"https:\/\/trantor.ink\/?p=454"},"modified":"2025-02-18T15:02:18","modified_gmt":"2025-02-18T07:02:18","slug":"motionlayout","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=454","title":{"rendered":"MotionLayout"},"content":{"rendered":"\n<p>\u8981\u5728 ConstraintLayout \u4e2d\u5b9e\u73b0\u5e73\u6ed1\u7684\u52a8\u753b\u79fb\u52a8\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 MotionLayout\uff0c\u5b83\u662f ConstraintLayout \u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u89c6\u56fe\u7684\u5e73\u6ed1\u52a8\u753b\u8fc7\u6e21\u3002\u901a\u8fc7 MotionLayout\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u7ea6\u675f\u3001\u6dfb\u52a0\u52a8\u753b\u548c\u8bbe\u7f6e\u76ee\u6807\u4f4d\u7f6e\u6765\u5b9e\u73b0\u5e73\u6ed1\u8fc7\u6e21\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n\n\n\n<p>1. <strong>\u4f7f\u7528 MotionLayout \u4f5c\u4e3a\u6839\u5e03\u5c40<\/strong>\u3002<\/p>\n\n\n\n<p>2. <strong>\u521b\u5efa motion_scene \u6587\u4ef6\u6765\u5b9a\u4e49\u52a8\u753b<\/strong>\u3002<\/p>\n\n\n\n<p>3. <strong>\u5728 MotionLayout \u4e2d\u8bbe\u7f6e\u76ee\u6807\u7ea6\u675f<\/strong>\u3002<\/p>\n\n\n\n<p>4. <strong>\u89e6\u53d1\u52a8\u753b<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<p><strong>1. \u521b\u5efa MotionLayout \u4f5c\u4e3a\u6839\u5e03\u5c40<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u6839\u5e03\u5c40\u662f MotionLayout\uff0c\u800c\u4e0d\u662f\u4f20\u7edf\u7684 ConstraintLayout\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;com.google.android.material.motionlayout.MotionLayout\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    android:id=\"@+id\/motionLayout\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    app:layoutDescription=\"@xml\/motion_scene\">\n\n    &lt;TextView\n        android:id=\"@+id\/viewToMove\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Hello World\"\n        android:textSize=\"24sp\" \/>\n\n&lt;\/com.google.android.material.motionlayout.MotionLayout><\/pre>\n\n\n\n<p><strong>2. \u521b\u5efa motion_scene.xml \u6587\u4ef6<\/strong><\/p>\n\n\n\n<p>motion_scene.xml \u5b9a\u4e49\u4e86 MotionLayout \u4e2d\u7684\u52a8\u753b\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u8fd9\u4e2a\u6587\u4ef6\u5e76\u5c06\u5b83\u653e\u5728 res\/xml\/ \u76ee\u5f55\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\n&lt;MotionScene xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\">\n\n    &lt;Transition\n        app:constraintSetStart=\"@id\/start\"\n        app:constraintSetEnd=\"@id\/end\"\n        app:duration=\"1000\">\n\n        &lt;OnClick\n            app:targetId=\"@id\/viewToMove\"\n            app:clickAction=\"rotate\"\/>\n\n    &lt;\/Transition>\n\n    &lt;ConstraintSet android:id=\"@+id\/start\">\n        &lt;Constraint\n            android:id=\"@id\/viewToMove\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            app:layout_constraintTop_toTopOf=\"parent\"\n            app:layout_constraintStart_toStartOf=\"parent\" \/>\n    &lt;\/ConstraintSet>\n\n    &lt;ConstraintSet android:id=\"@+id\/end\">\n        &lt;Constraint\n            android:id=\"@id\/viewToMove\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            app:layout_constraintTop_toTopOf=\"parent\"\n            app:layout_constraintStart_toStartOf=\"parent\"\n            app:layout_constraintTop_toTopOf=\"parent\"\n            app:layout_constraintStart_toStartOf=\"parent\"\n            app:layout_constraintBottom_toBottomOf=\"parent\"\n            app:layout_constraintEnd_toEndOf=\"parent\"\/>\n    &lt;\/ConstraintSet>\n&lt;\/MotionScene><\/pre>\n\n\n\n<p><strong>3. \u89e6\u53d1\u52a8\u753b<\/strong><\/p>\n\n\n\n<p>\u5728\u4f60\u7684 Activity \u6216 Fragment \u4e2d\u89e6\u53d1\u52a8\u753b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val motionLayout = findViewById&lt;MotionLayout>(R.id.motionLayout)\nmotionLayout.transitionToEnd() \/\/ \u8fd9\u4f1a\u5c06\u89c6\u56fe\u79fb\u52a8\u5230\u5b9a\u4e49\u7684\u7ec8\u70b9<\/pre>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<p>\u2022 MotionLayout \u901a\u8fc7\u5b9a\u4e49 motion_scene.xml \u6587\u4ef6\u6765\u63cf\u8ff0\u4ece start \u7ea6\u675f\u96c6\u5230 end \u7ea6\u675f\u96c6\u7684\u5e73\u6ed1\u8fc7\u6e21\u52a8\u753b\u3002<\/p>\n\n\n\n<p>\u2022 app:duration=&#8221;1000&#8243; \u8bbe\u7f6e\u52a8\u753b\u6301\u7eed\u65f6\u95f4\u4e3a 1000 \u6beb\u79d2\uff08\u5373 1 \u79d2\uff09\u3002<\/p>\n\n\n\n<p>\u2022 \u4f7f\u7528 transitionToEnd() \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u542f\u52a8\u52a8\u753b\uff0c\u5e73\u6ed1\u5730\u5c06\u89c6\u56fe\u79fb\u52a8\u5230\u76ee\u6807\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p><strong>\u5176\u4ed6\u52a8\u753b\uff1a<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5728\u70b9\u51fb\u6216\u5176\u4ed6\u64cd\u4f5c\u65f6\u89e6\u53d1\u52a8\u753b\uff0c\u53ef\u4ee5\u901a\u8fc7 OnClick \u4e8b\u4ef6\u6765\u8bbe\u7f6e\u76ee\u6807\u89c6\u56fe\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;OnClick\n    app:targetId=\"@id\/viewToMove\"\n    app:clickAction=\"rotate\"\/><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u66f4\u591a\u7684\u52a8\u753b\u7c7b\u578b\uff0c\u5982\u65cb\u8f6c\u3001\u5e73\u79fb\u3001\u7f29\u653e\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5728 ConstraintLayout \u4e2d\u5b9e\u73b0\u5e73\u6ed1\u7684\u52a8\u753b\u79fb\u52a8\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 MotionLayout\uff0c\u5b83\u662f ConstraintLayout \u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u89c6\u56fe\u7684\u5e73\u6ed1\u52a8\u753b\u8fc7\u6e21\u3002\u901a\u8fc7 MotionLayout\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u7ea6\u675f\u3001\u6dfb\u52a0\u52a8\u753b\u548c\u8bbe\u7f6e\u76ee\u6807\u4f4d\u7f6e\u6765\u5b9e\u73b0\u5e73\u6ed1\u8fc7\u6e21\u3002 \u6b65\u9aa4\uff1a 1. \u4f7f\u7528 MotionLayout \u4f5c\u4e3a\u6839\u5e03\u5c40\u3002 2. \u521b\u5efa motion_scene \u6587\u4ef6\u6765\u5b9a\u4e49\u52a8\u753b\u3002 3. \u5728 MotionLayout \u4e2d\u8bbe\u7f6e\u76ee\u6807\u7ea6\u675f\u3002 4. \u89e6\u53d1\u52a8\u753b\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a 1. \u521b\u5efa MotionLayout \u4f5c\u4e3a\u6839\u5e03\u5c40 \u9996\u5148\uff0c\u786e\u4fdd\u6839\u5e03\u5c40\u662f MotionLayout\uff0c\u800c\u4e0d\u662f\u4f20\u7edf\u7684 ConstraintLayout\u3002 2. \u521b\u5efa motion_scene.xml \u6587\u4ef6 motion_scene.xml \u5b9a\u4e49\u4e86 MotionLayout \u4e2d\u7684\u52a8\u753b\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u8fd9\u4e2a\u6587\u4ef6\u5e76\u5c06\u5b83\u653e\u5728 res\/xml\/ \u76ee\u5f55\u4e0b\u3002 3. \u89e6\u53d1\u52a8\u753b \u5728\u4f60\u7684 Activity \u6216 Fragment \u4e2d\u89e6\u53d1\u52a8\u753b\u3002 \u89e3\u91ca\uff1a \u2022 MotionLayout \u901a\u8fc7\u5b9a\u4e49 motion_scene.xml \u6587\u4ef6\u6765\u63cf\u8ff0\u4ece start \u7ea6\u675f\u96c6\u5230 end \u7ea6\u675f\u96c6\u7684\u5e73\u6ed1\u8fc7\u6e21\u52a8\u753b\u3002 \u2022 app:duration=&#8221;1000&#8243; \u8bbe\u7f6e\u52a8\u753b\u6301\u7eed\u65f6\u95f4\u4e3a 1000 \u6beb\u79d2\uff08\u5373 1 \u79d2\uff09\u3002 \u2022 \u4f7f\u7528 transitionToEnd() \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u542f\u52a8\u52a8\u753b\uff0c\u5e73\u6ed1\u5730\u5c06\u89c6\u56fe\u79fb\u52a8\u5230\u76ee\u6807\u4f4d\u7f6e\u3002 \u5176\u4ed6\u52a8\u753b\uff1a \u5982\u679c\u4f60\u60f3\u8981\u5728\u70b9\u51fb\u6216\u5176\u4ed6\u64cd\u4f5c\u65f6\u89e6\u53d1\u52a8\u753b\uff0c\u53ef\u4ee5\u901a\u8fc7 OnClick \u4e8b\u4ef6\u6765\u8bbe\u7f6e\u76ee\u6807\u89c6\u56fe\u3002 \u8fd9\u91cc\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u66f4\u591a\u7684\u52a8\u753b\u7c7b\u578b\uff0c\u5982\u65cb\u8f6c\u3001\u5e73\u79fb\u3001\u7f29\u653e\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-454","post","type-post","status-publish","format-standard","hentry","category-appdev"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=454"}],"version-history":[{"count":1,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions\/455"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}