Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách tạo hiệu ứng nhấp nháy (blinking) cho TextView trong Android

Giới thiệu

Ví dụ này minh họa cách tạo hiệu ứng nhấp nháy (blink) cho TextView bằng cách chuyển đổi visibility (VISIBLE/INVISIBLE) định kỳ dùng Handler và Thread.

Bước 1: Tạo dự án mới

Android Studio → File → New Project.

Bước 2: Giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:textSize="20dp"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:text="Animation nhấp nháy TextView"
        android:layout_height="wrap_content" />

</RelativeLayout>

Bước 3: Logic blink (src/MainActivity.java)

package com.example.andy.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    private void blinkTextView() {
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int timeToBlink = 1000; // 1 giây
                try { Thread.sleep(timeToBlink); } catch (Exception e) {}
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        TextView txt = findViewById(R.id.text);
                        if (txt.getVisibility() == View.VISIBLE) {
                            txt.setVisibility(View.INVISIBLE);
                        } else {
                            txt.setVisibility(View.VISIBLE);
                        }
                        blinkTextView(); // Đệ quy để lặp vô hạn
                    }
                });
            }
        }).start();
    }
}

Giải thích cơ chế

  • Tạo Thread nền để sleep(1000) không chặn UI thread
  • Dùng Handler.post() để quay về UI thread thay đổi visibility
  • Gọi đệ quy blinkTextView() để lặp vô hạn
  • Chu kỳ: 1 giây hiện, 1 giây ẩn = 2 giây/chu kỳ
⚠️ Cải tiến hiện đại: Nên dùng ValueAnimator hoặc ObjectAnimator thay vì Thread+Handler để tránh leak, dễ dừng, và mượt hơn:
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f);
animator.setDuration(500);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();

Chạy ứng dụng

Chạy app → TextView nhấp nháy liên tục: hiện 1 giây → ẩn 1 giây → lặp lại.