【问题标题】:Move Button Smoothly in the screen - Android在屏幕中平滑移动按钮 - Android
【发布时间】:2017-05-21 19:46:02
【问题描述】:

我已经关注了这个答案How to make a Button randomly Move,它可以按我的意愿工作,但是按钮会出现并从屏幕上消失。

我想这样做:h ttp://jsfiddle.net/Xw29r/15/

我在 android 中没有找到任何与此相关的内容。

【问题讨论】:

    标签: java android


    【解决方案1】:

    使用settranslate 创建一个XML 动画,并将此XML 文件放入/res/anim 文件夹中。

    这是一个例子

    //sequestial_move.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true"
        android:interpolator="@android:anim/linear_interpolator" >
    
        <!-- Use startOffset to give delay between animations -->
        <!-- Move -->
        <translate
            android:duration="800"
            android:fillAfter="true"
            android:fromXDelta="0%p"
            android:startOffset="300"
            android:toXDelta="75%p" />
        <translate
            android:duration="800"
            android:fillAfter="true"
            android:fromYDelta="0%p"
            android:startOffset="1100"
            android:toYDelta="70%p" />
        <translate
            android:duration="800"
            android:fillAfter="true"
            android:fromXDelta="0%p"
            android:startOffset="1900"
            android:toXDelta="-75%p" />
        <translate
            android:duration="800"
            android:fillAfter="true"
            android:fromYDelta="0%p"
            android:startOffset="2700"
            android:toYDelta="-70%p" />
    
    </set>
    

    使用:

    XML加载动画并使用.startAnimation(..)方法使用动画

        ............
        ...................
    
        TextView txtMessage = (TextView) findViewById(R.id.txtMessage);
        Button btnStart = (Button) findViewById(R.id.btnStart);
    
        // load the animation
        Animation animSequestialMove = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.sequestial_move);
    
        // button click event
        btnStart.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                txtMessage.setVisibility(View.VISIBLE);
    
                // start the animation
                txtMessage.startAnimation(animSequestialMove);
            }
        });
    
        .............
        .....................
    

    这是一个很好的tutorial,关于不同类型的Animations

    希望对你有帮助~

    【讨论】:

    • 完美运行!你对按钮永不停止移动有什么建议吗?
    • 试试这个:animSequestialMove.setDuration(10000); animSequestialMove.setRepeatCount(-1); animSequestialMove.setRepeatMode(Animation.REVERSE); animSequestialMove.setInterpolator(new LinearInterpolator());
    • 这是一个关于重复动画的答案:stackoverflow.com/questions/7281276/…
    【解决方案2】:

    使用本教程

    Apply animation on Button

    【讨论】:

    • 教程很棒,但是按钮在屏幕上没有移动
    • 查看第一个按钮动画(anim_translate.xml),这是你需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2018-09-27
    • 2018-10-18
    相关资源
    最近更新 更多