Animation using Thread

The activity class itself is implementing runnable to make it a thread.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Fading Out"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:context=".MainActivity"/>

</LinearLayout>

package com.mbcc.animation;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements Runnable {

    private static int PERIOD = 2000;
    private TextView textView = null;
    private boolean fadingOut = true;

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

        textView = (TextView) findViewById(R.id.text);
    }

    @Override
    public void onStart() {
        super.onStart();

        run();
    }

    @Override
    public void onStop() {
        textView.removeCallbacks(this);

        super.onStop();
    }

    @Override
    public void run() {
        if (fadingOut) {
            textView.animate().alpha(0).setDuration(PERIOD);
            textView.setText("Fading Out");
        } else {
            textView.animate().alpha(1).setDuration(PERIOD);
            textView.setText("Coming Back");
        }

        fadingOut = !fadingOut;

        textView.postDelayed(this, PERIOD);
    }
}

results matching ""

    No results matching ""