草庐IT

android - 如何用关键帧控制边距?

coder 2023-12-23 原文

我想先更改 layout_marginStart,而 layout_marginStart 应该在第 60 帧之前更改。但是,随后的关键帧将被忽略,并且两个边距同时设置动画。

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <Transition

        app:constraintSetEnd="@+id/end"
        app:constraintSetStart="@+id/start"

        >
        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@id/scrollable"
            app:touchAnchorSide="top" />
        <KeyFrameSet>
            <KeyAttribute
                app:curveFit="linear"
                app:framePosition="10"
                app:motionTarget="@+id/text">
                <CustomAttribute
                    app:attributeName="layoutMarginStart"
                    app:customDimension="68dip" />
            </KeyAttribute>
            <KeyAttribute
                app:curveFit="linear"
                app:framePosition="60"
                app:motionTarget="@+id/text">
                <CustomAttribute
                    app:attributeName="layoutMarginEnd"
                    app:customDimension="0dip" />
            </KeyAttribute>

        </KeyFrameSet>
        <ConstraintSet android:id="@+id/start">
            <Constraint
                android:id="@+id/text"
                android:layout_width="0dip"
                android:layout_height="40dip"
                android:layout_marginStart="16dip"

                android:layout_marginTop="24dip"
                android:layout_marginEnd="16dip"
                android:layout_marginBottom="12dip"
                android:background="#BEBEBE"
                app:layout_constraintEnd_toEndOf="@+id/parent"
                app:layout_constraintStart_toStartOf="@+id/parent"
                app:layout_constraintTop_toTopOf="parent" />


        </ConstraintSet>

        <ConstraintSet android:id="@+id/end">
            <Constraint
                android:id="@+id/text"
                android:layout_width="0dip"
                android:layout_height="40dip"
                android:layout_marginStart="60dip"
                android:layout_marginTop="24dip"
                android:layout_marginEnd="60dip"
                android:layout_marginBottom="12dip"
                android:background="#BEBEBE"
                app:layout_constraintEnd_toEndOf="@+id/parent"
                app:layout_constraintStart_toStartOf="@+id/parent"
                app:layout_constraintTop_toTopOf="parent" />


        </ConstraintSet>
    </Transition>

</MotionScene>

最佳答案

您的 View 上没有setLayoutMarginStartsetLayoutMarginEnd 方法。 app:attributeName 参数将通过反射工作并搜索这些方法。

实现此目的的一种方法是简单地继承您的 View 并编写您自己的 setter 。

fun setMarginTop(newMarginTop: Int) {
    val lp = layoutParams as? ViewGroup.MarginLayoutParams ?: return
    lp.run { 
        setMargins(leftMargin, newMarginTop, rightMargin, bottomMargin)
    }
    layoutParams = lp
}

或者如果您使用 kotlin-ktx:

fun setMarginTop(newMarginTop: Int) {
    updateLayoutParams<ViewGroup.MarginLayoutParams> {
        updateMargins(top = newMarginTop)
    }
}

关于android - 如何用关键帧控制边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56324146/

有关android - 如何用关键帧控制边距?的更多相关文章

  1. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  2. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  3. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

  4. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

  5. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  6. ruby - Ruby 的 AST 中的 'send' 关键字是什么意思? - 2

    我正在尝试学习Ruby词法分析器和解析器(whitequarkparser)以了解更多有关从Ruby脚本进一步生成机器代码的过程。在解析以下Ruby代码字符串时。defadd(a,b)returna+bendputsadd1,2它导致以下S表达式符号。s(:begin,s(:def,:add,s(:args,s(:arg,:a),s(:arg,:b)),s(:return,s(:send,s(:lvar,:a),:+,s(:lvar,:b)))),s(:send,nil,:puts,s(:send,nil,:add,s(:int,1),s(:int,3))))任何人都可以向我解释生成的

  7. ruby-on-rails - 在 Rails 控制台中使用 asset_path - 2

    在我的Character模型中,我添加了:字符.rbbefore_savedoself.profile_picture_url=asset_path('icon.png')end但是,对于数据库中已存在的所有角色,它们的profile_picture_url为nil。因此,我想进入控制台并遍历所有这些并进行设置。在我试过的控制台中:Character.find_eachdo|c|c.profile_picture_url=asset_path('icon.png')end但这给出了错误:NoMethodError:undefinedmethod`asset_path'formain:O

  8. ruby-on-rails - 带有 Pry 的 Rails 控制台 - 2

    当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

  9. ruby - 将全局 $stdout 重新分配给控制台 - ruby - 2

    我正在尝试将$stdout设置为临时写入一个文件,然后返回到一个文件。test.rb:old_stdout=$stdout$stdout.reopen("mytestfile.out",'w+')puts"thisgoesinmytestfile"$stdout=old_stdoutputs"thisshouldbeontheconsole"$stdout.reopen("mytestfile1.out",'w+')puts"thisgoesinmytestfile1:"$stdout=old_stdoutputs"thisshouldbebackontheconsole"这是输出。r

  10. ruby-on-rails - Ruby 流量控制 : throw an exception, 返回 nil 还是让它失败? - 2

    我在思考流量控制的最佳实践。我应该走哪条路?1)不要检查任何东西并让程序失败(更清晰的代码,自然的错误消息):defself.fetch(feed_id)feed=Feed.find(feed_id)feed.fetchend2)通过返回nil静默失败(但是,“CleanCode”说,你永远不应该返回null):defself.fetch(feed_id)returnunlessfeed_idfeed=Feed.find(feed_id)returnunlessfeedfeed.fetchend3)抛出异常(因为不按id查找feed是异常的):defself.fetch(feed_id

随机推荐