文字动画和背景变化 [英] Text Animation and Background change

查看:104
本文介绍了文字动画和背景变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入困境,需要帮助。

I'm stuck and need help.

如何在图片。我希望在应用启动后1秒钟背景和文本的颜色自动平滑地更改。并且要像一个周期。同时,图标会推动

How can I add animations like on the picture. I want the color of the background and text to be smoothly automatically changed 1 sec after the app is launched. And to be like a cycle. Meanwhile an icon is impulsing.

推荐答案

在这里,你去了。

我对更改背景布局和文本的颜色。目前,我是为随机颜色制作的,如果您需要预定义的颜色,请告诉我。以下是我的代码

I did the code to change color of background layout and text. Currently I did for random colors, if you need pre-defined colors, please let me know. Below is my code


  1. activity_main.xml

`

<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.background_animation.MainActivity"
    android:id="@+id/constraint">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

`


  1. MainActivity.java

`

public class MainActivity extends AppCompatActivity {

            // Move your declarations here if you intend on using them after the onCreate() method
            ConstraintLayout layout;
            TextView textView;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Inflate your objects
                layout = (ConstraintLayout) findViewById(R.id.constraint);
                textView = (TextView) findViewById(R.id.textView);

                // We change the color to RED for the first time as the program loads
                layout.setBackgroundColor(Color.RED);

                // We change the color to GREEN for the first time as the program loads
                textView.setTextColor(Color.GREEN);

                // Create the timer object which will run the desired operation on a schedule or at a given time
                Timer timer = new Timer();

                // Create a task which the timer will execute.  This should be an implementation of the TimerTask interface.
                // I have created an inner class below which fits the bill.
                MyTimer myTimer = new MyTimer();

                // We schedule the timer task to run after 1000 ms and continue to run every 1000 ms.
                timer.schedule(myTimer,1000,1000);
            }

            // An inner class which is an implementation of the TImerTask interface to be used by the Timer.
            class MyTimer extends TimerTask{
                @Override
                public void run() {

                    // This runs in a background thread.
                    // We cannot call the UI from this thread, so we must call the main UI thread and pass a runnable
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Random rand = new Random();

                            // The random generator creates values between [0,256) for use as RGB values used below to create a random color
                            // We call the RelativeLayout object and we change the color.  The first parameter in argb() is the alpha.
                            layout.setBackgroundColor(Color.argb(255, rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)));
                            textView.setTextColor(Color.argb(222,rand.nextInt(222),rand.nextInt(222),rand.nextInt(222)));
                        }
                    });
                }
            }
        }

`

这是您想要的吗?

这篇关于文字动画和背景变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆