Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android动画:背景颜色随时间变化_Android_Animation_Background_Android Relativelayout - Fatal编程技术网

Android动画:背景颜色随时间变化

Android动画:背景颜色随时间变化,android,animation,background,android-relativelayout,Android,Animation,Background,Android Relativelayout,我需要一些动画方面的帮助 我正在开发一个简单的游戏,用户可以在其中解决数学问题。每轮有60秒,我现在用倒数计时器检查时间。我想更改背景颜色,而不是保留时间的文本表示。例如,请检查一个链接 现在我有了蓝色的相对主义版面,我想根据剩余的时间改变这个版面的颜色(或者只是向左移动)。当游戏刚开始时,整个布局将是蓝色的,30秒后,一半的布局应该是深色的,这应该从示例gif中可以清楚地看到 重要提示:用户可以使用“超级力量”来增加回合时间。当用户只有10秒结束时,他可以使用“超级力量”并添加+30秒,这将

我需要一些动画方面的帮助

我正在开发一个简单的游戏,用户可以在其中解决数学问题。每轮有60秒,我现在用倒数计时器检查时间。我想更改背景颜色,而不是保留时间的文本表示。例如,请检查一个链接

现在我有了蓝色的相对主义版面,我想根据剩余的时间改变这个版面的颜色(或者只是向左移动)。当游戏刚开始时,整个布局将是蓝色的,30秒后,一半的布局应该是深色的,这应该从示例gif中可以清楚地看到

重要提示:用户可以使用“超级力量”来增加回合时间。当用户只有10秒结束时,他可以使用“超级力量”并添加+30秒,这将影响背景动画


你能帮我怎么创造这个效果吗

这只是一个想法,您可以使用两个相互重叠的独立背景/布局来实现这一点

使用倒计时,可以使用ObjectAnimator水平平移其中一个背景,并为平移值传递时间。这将允许背景根据时间是增加还是减少而向任意方向移动。

给你

我已经实现了从左到右改变视图背景颜色的代码。我做了10秒。以后您可以根据您的要求增加

我已经创建了另一个视图,其宽度和高度将与父视图匹配。同样为了获得视图的动画,我使用了“动画”类,其中使用了“TranslateAnimation”API来转换视图

同样在活动清单中,为了获得关于优先级的第一个视图,我使用了“提升”属性

检查下面的代码

  • activity_main.xml
  • `

    `

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context="com.example.golevr.changebackgroundcolor.MainActivity"
        android:id="@+id/constraint">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:elevation="10dp"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view"
            android:elevation="0dp"></View>
    
    </android.support.constraint.ConstraintLayout>
    
    public class MainActivity extends AppCompatActivity {
    
        ConstraintLayout constraintLayout;
        View view;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            constraintLayout = (ConstraintLayout) findViewById(R.id.constraint);
            view = (View)findViewById(R.id.view);
    
            view.startAnimation(inFromLeftAnimation());
            constraintLayout.bringToFront();
    
        }
    
        private Animation inFromLeftAnimation() {
    
            Animation inFromRight = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, -1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            view.setBackgroundColor(Color.BLUE);
            inFromRight.setDuration(10000);
            inFromRight.setInterpolator(new AccelerateInterpolator());
            return inFromRight;
        }
    
    }